diff options
author | Pantelis Antoniou <pantelis.antoniou@konsulko.com> | 2016-05-24 20:50:35 +0300 |
---|---|---|
committer | David Gibson <david@gibson.dropbear.id.au> | 2016-05-25 14:55:17 +1000 |
commit | 9dc404958e9c91f33f75450f69b690a5e676af04 (patch) | |
tree | 7c487292f6b8f2c2cd0fc6db67d37332f75ac6b1 /util.c | |
parent | beef80b8b55f32e5d3338ac13429382336e38ead (diff) | |
download | dtc-9dc404958e9c91f33f75450f69b690a5e676af04.zip dtc-9dc404958e9c91f33f75450f69b690a5e676af04.tar.gz dtc-9dc404958e9c91f33f75450f69b690a5e676af04.tar.bz2 |
util: Add xasprintf portable asprintf variant
Include a portable asprintf variant that works on any C99
conforming platform.
Signed-off-by: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 30 |
1 files changed, 30 insertions, 0 deletions
@@ -46,6 +46,36 @@ char *xstrdup(const char *s) return d; } +/* based in part from (3) vsnprintf */ +int xasprintf(char **strp, const char *fmt, ...) +{ + int n, size = 128; /* start with 128 bytes */ + char *p; + va_list ap; + + /* initial pointer is NULL making the fist realloc to be malloc */ + p = NULL; + while (1) { + p = xrealloc(p, size); + + /* Try to print in the allocated space. */ + va_start(ap, fmt); + n = vsnprintf(p, size, fmt, ap); + va_end(ap); + + /* If that worked, return the string. */ + if (n > -1 && n < size) + break; + /* Else try again with more space. */ + if (n > -1) /* glibc 2.1 */ + size = n + 1; /* precisely what is needed */ + else /* glibc 2.0 */ + size *= 2; /* twice the old size */ + } + *strp = p; + return strlen(p); +} + char *join_path(const char *path, const char *name) { int lenp = strlen(path); |