diff options
author | Nicholas Marriott <nicholas.marriott@gmail.com> | 2011-03-04 23:39:41 +0000 |
---|---|---|
committer | Nicholas Marriott <nicholas.marriott@gmail.com> | 2011-03-04 23:39:41 +0000 |
commit | 34bb013b92a61233cc9feed7866a660e22693265 (patch) | |
tree | 629704de943a8d41ea6f4b6ff48840da567761e5 | |
parent | 4cb976b4087f99e9c5ad946a90dfac89d7a20cc6 (diff) | |
download | rtmux-34bb013b92a61233cc9feed7866a660e22693265.tar.gz rtmux-34bb013b92a61233cc9feed7866a660e22693265.tar.bz2 rtmux-34bb013b92a61233cc9feed7866a660e22693265.zip |
Use the right asprintf since we don't support truly broken platforms right now.
-rw-r--r-- | compat/asprintf.c | 40 |
1 files changed, 16 insertions, 24 deletions
diff --git a/compat/asprintf.c b/compat/asprintf.c index d7c6ec99..9bd576e8 100644 --- a/compat/asprintf.c +++ b/compat/asprintf.c @@ -1,4 +1,4 @@ -/* $Id: asprintf.c,v 1.6 2011-01-21 20:03:18 nicm Exp $ */ +/* $Id: asprintf.c,v 1.7 2011-03-04 23:39:41 nicm Exp $ */ /* * Copyright (c) 2006 Nicholas Marriott <nicm@users.sourceforge.net> @@ -28,13 +28,13 @@ #include "tmux.h" int -asprintf(char **ret, const char *format, ...) +asprintf(char **ret, const char *fmt, ...) { va_list ap; int n; - va_start(ap, format); - n = vasprintf(ret, format, ap); + va_start(ap, fmt); + n = vasprintf(ret, fmt, ap); va_end(ap); return (n); @@ -43,28 +43,20 @@ asprintf(char **ret, const char *format, ...) int vasprintf(char **ret, const char *fmt, va_list ap) { - va_list aq; - size_t len; - char *buf; - int n; + int n; - len = 64; - buf = xmalloc(len); + if ((n = vsnprintf(NULL, 0, fmt, ap)) < 0) + goto error; - for (;;) { - va_copy(aq, ap); - n = vsnprintf(buf, len, fmt, aq); - va_end(aq); + *ret = xmalloc(n + 1); + if ((n = vsnprintf(*ret, n + 1, fmt, ap)) < 0) { + xfree(*ret); + goto error; + } - if (n != -1) { - *ret = buf; - return (n); - } + return (n); - if (len > SIZE_MAX / 2) { - xfree(buf); - return (-1); - } - len *= 2; - } +error: + *ret = NULL; + return (-1); } |