diff options
author | Nicholas Noll <nbnoll@eml.cc> | 2021-10-15 16:18:02 -0700 |
---|---|---|
committer | Nicholas Noll <nbnoll@eml.cc> | 2021-10-15 16:18:02 -0700 |
commit | bf03074e346b004659196b6c17eee04dbffd3ac2 (patch) | |
tree | 7200db30f1ef7e3661091552932eb304bd4ce9c6 /sys/cmd/rc/util.c | |
parent | 566d54fe549286895fdef8aa9f385686405dd290 (diff) |
feat(rc): working prototype of input->compile->print loop
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'; +} |