Use the right asprintf since we don't support truly broken platforms right now.

This commit is contained in:
Nicholas Marriott 2011-03-04 23:39:41 +00:00
parent 4cb976b408
commit 34bb013b92

View File

@ -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> * Copyright (c) 2006 Nicholas Marriott <nicm@users.sourceforge.net>
@ -28,13 +28,13 @@
#include "tmux.h" #include "tmux.h"
int int
asprintf(char **ret, const char *format, ...) asprintf(char **ret, const char *fmt, ...)
{ {
va_list ap; va_list ap;
int n; int n;
va_start(ap, format); va_start(ap, fmt);
n = vasprintf(ret, format, ap); n = vasprintf(ret, fmt, ap);
va_end(ap); va_end(ap);
return (n); return (n);
@ -43,28 +43,20 @@ asprintf(char **ret, const char *format, ...)
int int
vasprintf(char **ret, const char *fmt, va_list ap) vasprintf(char **ret, const char *fmt, va_list ap)
{ {
va_list aq; int n;
size_t len;
char *buf;
int n;
len = 64; if ((n = vsnprintf(NULL, 0, fmt, ap)) < 0)
buf = xmalloc(len); goto error;
for (;;) { *ret = xmalloc(n + 1);
va_copy(aq, ap); if ((n = vsnprintf(*ret, n + 1, fmt, ap)) < 0) {
n = vsnprintf(buf, len, fmt, aq); xfree(*ret);
va_end(aq); goto error;
if (n != -1) {
*ret = buf;
return (n);
}
if (len > SIZE_MAX / 2) {
xfree(buf);
return (-1);
}
len *= 2;
} }
return (n);
error:
*ret = NULL;
return (-1);
} }