diff options
author | Peter Eisentraut <peter_e@gmx.net> | 2013-10-13 00:09:18 -0400 |
---|---|---|
committer | Peter Eisentraut <peter_e@gmx.net> | 2013-10-13 00:09:18 -0400 |
commit | 5b6d08cd2992922b667564a49f19580f11676050 (patch) | |
tree | 4104a4255eeb88e78da71477b5f7b129f9a1b599 /src/backend/utils/mmgr/mcxt.c | |
parent | a53dee43fe585e673658b01e7354892dcede957e (diff) |
Add use of asprintf()
Add asprintf(), pg_asprintf(), and psprintf() to simplify string
allocation and composition. Replacement implementations taken from
NetBSD.
Reviewed-by: Álvaro Herrera <alvherre@2ndquadrant.com>
Reviewed-by: Asif Naeem <anaeem.it@gmail.com>
Diffstat (limited to 'src/backend/utils/mmgr/mcxt.c')
-rw-r--r-- | src/backend/utils/mmgr/mcxt.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c index 9574fd3c7a3..b7beb130ea3 100644 --- a/src/backend/utils/mmgr/mcxt.c +++ b/src/backend/utils/mmgr/mcxt.c @@ -852,3 +852,52 @@ pnstrdup(const char *in, Size len) out[len] = '\0'; return out; } + +/* + * asprintf()-like functions around palloc, adapted from + * http://ftp.netbsd.org/pub/pkgsrc/current/pkgsrc/pkgtools/libnbcompat/files/asprintf.c + */ + +char * +psprintf(const char *format, ...) +{ + va_list ap; + char *retval; + + va_start(ap, format); + retval = pvsprintf(format, ap); + va_end(ap); + + return retval; +} + +char * +pvsprintf(const char *format, va_list ap) +{ + char *buf, *new_buf; + size_t len; + int retval; + va_list ap2; + + len = 128; + buf = palloc(len); + + va_copy(ap2, ap); + retval = vsnprintf(buf, len, format, ap); + Assert(retval >= 0); + + if (retval < len) + { + new_buf = repalloc(buf, retval + 1); + va_end(ap2); + return new_buf; + } + + len = (size_t)retval + 1; + pfree(buf); + buf = palloc(len); + retval = vsnprintf(buf, len, format, ap2); + va_end(ap2); + Assert(retval == len - 1); + return buf; +} |