From 13da42dedf0305f7f47dfc0f6d00ca5cb169493d Mon Sep 17 00:00:00 2001 From: Justin Bedo Date: Mon, 19 Nov 2012 11:15:39 +1100 Subject: Initial import --- string.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 string.c (limited to 'string.c') 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 +#include +#include + +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; +} -- cgit v1.2.3