Skip to content

Commit

Permalink
Move asprintf() implementation to compat.c
Browse files Browse the repository at this point in the history
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed Apr 19, 2012
1 parent a51e233 commit e7afe3b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 73 deletions.
74 changes: 74 additions & 0 deletions compat.c
Expand Up @@ -37,3 +37,77 @@ time_t openconnect__time(time_t *t)
}
#endif

#ifndef HAVE_ASPRINTF
#include <stdarg.h>
#include <errno.h>

static int oc_vasprintf(char **strp, const char *fmt, va_list ap)
{
va_list ap2;
char *res = NULL;
int len = 160, len2;
int ret = 0;
int errno_save = -ENOMEM;

res = malloc(160);
if (!res)
goto err;

/* Use a copy of 'ap', preserving it in case we need to retry into
a larger buffer. 160 characters should be sufficient for most
strings in openconnect. */
#ifdef HAVE_VA_COPY
va_copy(ap2, ap);
#elif defined (HAVE___VA_COPY)
__va_copy(ap2, ap);
#else
#error No va_copy()!
// You could try this.
ap2 = ap;
// Or this
*ap2 = *ap;
#endif
len = vsnprintf(res, 160, fmt, ap2);
va_end(ap2);

if (len < 0) {
printf_err:
errno_save = errno;
free(res);
res = NULL;
goto err;
}
if (len >=0 && len < 160)
goto out;

free(res);
res = malloc(len+1);
if (!res)
goto err;

len2 = vsnprintf(res, len+1, fmt, ap);
if (len2 < 0 || len2 > len)
goto printf_err;

ret = 0;
goto out;

err:
errno = errno_save;
ret = -1;
out:
*strp = res;
return ret;
}

int openconnect__asprintf(char **strp, const char *fmt, ...)
{
va_list ap;
int ret;

va_start(ap, fmt);
ret = oc_vasprintf(strp, fmt, ap);
va_end(ap);
return ret;
}
#endif
73 changes: 0 additions & 73 deletions http.c
Expand Up @@ -546,79 +546,6 @@ static char *openconnect__strcasestr(const char *haystack, const char *needle)
#define strcasestr openconnect__strcasestr
#endif

#ifndef HAVE_ASPRINTF
#include <stdarg.h>

static int oc_vasprintf(char **strp, const char *fmt, va_list ap)
{
va_list ap2;
char *res = NULL;
int len = 160, len2;
int ret = 0;
int errno_save = -ENOMEM;

res = malloc(160);
if (!res)
goto err;

/* Use a copy of 'ap', preserving it in case we need to retry into
a larger buffer. 160 characters should be sufficient for most
strings in openconnect. */
#ifdef HAVE_VA_COPY
va_copy(ap2, ap);
#elif defined (HAVE___VA_COPY)
__va_copy(ap2, ap);
#else
#error No va_copy()!
// You could try this.
ap2 = ap;
// Or this
*ap2 = *ap;
#endif
len = vsnprintf(res, 160, fmt, ap2);
va_end(ap2);

if (len < 0) {
printf_err:
errno_save = errno;
free(res);
res = NULL;
goto err;
}
if (len >=0 && len < 160)
goto out;

free(res);
res = malloc(len+1);
if (!res)
goto err;

len2 = vsnprintf(res, len+1, fmt, ap);
if (len2 < 0 || len2 > len)
goto printf_err;

ret = 0;
goto out;

err:
errno = errno_save;
ret = -1;
out:
*strp = res;
return ret;
}

int openconnect__asprintf(char **strp, const char *fmt, ...)
{
va_list ap;
int ret;

va_start(ap, fmt);
ret = oc_vasprintf(strp, fmt, ap);
va_end(ap);
return ret;
}
#endif

int internal_parse_url(char *url, char **res_proto, char **res_host,
int *res_port, char **res_path, int default_port)
Expand Down

0 comments on commit e7afe3b

Please sign in to comment.