diff options
Diffstat (limited to 'sys/cmd/rc/util.c')
-rw-r--r-- | sys/cmd/rc/util.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/sys/cmd/rc/util.c b/sys/cmd/rc/util.c new file mode 100644 index 0000000..b0be788 --- /dev/null +++ b/sys/cmd/rc/util.c @@ -0,0 +1,65 @@ +#include "rc.h" + +void +fatal(char *msg, ...) +{ + va_list args; + vfprintf(stderr, msg, args); + va_end(args); + + abort(); +} + +void* +emalloc(uintptr n) +{ + void *p; + if(!(p = malloc(n))) + fatal("out of memory: can't allocate %d bytes", n); + + memset(p, 0, n); + return p; +} + +void* +erealloc(void *p, uintptr n) +{ + void *r; + if(!(r = realloc(p,n))) + fatal("out of memory: can't reallocate %d bytes", n); + + return r; +} + +void +efree(void *p) +{ + if(p) + free(p); + // TODO: log the double free +} + + +char *bp; + +static +void +iacvt(int n) +{ + if(n<0){ + *bp++='-'; + n=-n; /* doesn't work for n==-inf */ + } + if(n/10) + iacvt(n/10); + + *bp++=n%10+'0'; +} + +void +itoa(char *s, long n) +{ + bp = s; + iacvt(n); + *bp='\0'; +} |