diff options
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/string.c b/string.c new file mode 100644 index 0000000..d8a6429 --- /dev/null +++ b/string.c @@ -0,0 +1,42 @@ +#include<u.h> +#include<libc.h> +#include<mheap.h> + +const char * +hprint(char *fmt, ...) +{ + char *buf, *hbuf; + int len, n; + va_list args; + + /* Initial buffer */ + len = 512; + buf = nil; + + do{ + len <<= 1; + buf = realloc(buf, len); + if(buf == nil) + sysfatal("hsprint: %r"); + + va_start(args, fmt); + n = vsnprint(buf, len, fmt, args) + 1; + va_end(args); + }while(n == len); + + hbuf = halloc(n); + memmove(hbuf, buf, n); + free(buf); + + return hbuf; +} + +const char * +hstrdup(char *s) +{ + char *u; + + u = halloc(strlen(s) + 1); + memmove(u, s, strlen(s) + 1); + return u; +} |