Added asnprintf and vasnprintf
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@1633 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
@@ -110,11 +110,21 @@ int asprintf (char **ret, const char *format, ...)
|
|||||||
__attribute__ ((format (printf, 2, 3)));
|
__attribute__ ((format (printf, 2, 3)));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef HAVE_VSNPRINTF
|
#ifndef HAVE_VASPRINTF
|
||||||
int vasprintf (char **ret, const char *format, va_list ap)
|
int vasprintf (char **ret, const char *format, va_list ap)
|
||||||
__attribute__((format (printf, 2, 0)));
|
__attribute__((format (printf, 2, 0)));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef HAVE_ASNPRINTF
|
||||||
|
int asnprintf (char **ret, size_t max_sz, const char *format, ...)
|
||||||
|
__attribute__ ((format (printf, 3, 4)));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef HAVE_VASNPRINTF
|
||||||
|
int vasnprintf (char **ret, size_t max_sz, const char *format, va_list ap)
|
||||||
|
__attribute__((format (printf, 3, 0)));
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef HAVE_STRDUP
|
#ifndef HAVE_STRDUP
|
||||||
char * strdup(const char *old);
|
char * strdup(const char *old);
|
||||||
#endif
|
#endif
|
||||||
|
@@ -110,11 +110,21 @@ int asprintf (char **ret, const char *format, ...)
|
|||||||
__attribute__ ((format (printf, 2, 3)));
|
__attribute__ ((format (printf, 2, 3)));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef HAVE_VSNPRINTF
|
#ifndef HAVE_VASPRINTF
|
||||||
int vasprintf (char **ret, const char *format, va_list ap)
|
int vasprintf (char **ret, const char *format, va_list ap)
|
||||||
__attribute__((format (printf, 2, 0)));
|
__attribute__((format (printf, 2, 0)));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef HAVE_ASNPRINTF
|
||||||
|
int asnprintf (char **ret, size_t max_sz, const char *format, ...)
|
||||||
|
__attribute__ ((format (printf, 3, 4)));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef HAVE_VASNPRINTF
|
||||||
|
int vasnprintf (char **ret, size_t max_sz, const char *format, va_list ap)
|
||||||
|
__attribute__((format (printf, 3, 0)));
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef HAVE_STRDUP
|
#ifndef HAVE_STRDUP
|
||||||
char * strdup(const char *old);
|
char * strdup(const char *old);
|
||||||
#endif
|
#endif
|
||||||
|
@@ -45,6 +45,7 @@ RCSID("$Id$");
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <roken.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Common state
|
* Common state
|
||||||
@@ -55,6 +56,7 @@ struct state {
|
|||||||
char *s;
|
char *s;
|
||||||
char *theend;
|
char *theend;
|
||||||
size_t sz;
|
size_t sz;
|
||||||
|
size_t max_sz;
|
||||||
int (*append_char)(struct state *, char);
|
int (*append_char)(struct state *, char);
|
||||||
int (*reserve)(struct state *, size_t);
|
int (*reserve)(struct state *, size_t);
|
||||||
/* XXX - methods */
|
/* XXX - methods */
|
||||||
@@ -131,15 +133,54 @@ asprintf (char **ret, const char *format, ...)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef HAVE_ASNPRINTF
|
||||||
|
int
|
||||||
|
asnprintf (char **ret, size_t max_sz, const char *format, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
int val;
|
||||||
|
|
||||||
|
va_start(args, format);
|
||||||
|
val = vasnprintf (ret, max_sz, format, args);
|
||||||
|
|
||||||
|
/* XXX - more paranoia */
|
||||||
|
{
|
||||||
|
int ret2;
|
||||||
|
char *tmp;
|
||||||
|
tmp = malloc (val + 1);
|
||||||
|
if (tmp == NULL)
|
||||||
|
abort ();
|
||||||
|
|
||||||
|
ret2 = vsprintf (tmp, format, args);
|
||||||
|
if (val != ret2 || strcmp(*ret, tmp))
|
||||||
|
abort ();
|
||||||
|
free (tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
va_end(args);
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef HAVE_VASPRINTF
|
#ifndef HAVE_VASPRINTF
|
||||||
int
|
int
|
||||||
vasprintf (char **ret, const char *format, va_list args)
|
vasprintf (char **ret, const char *format, va_list args)
|
||||||
|
{
|
||||||
|
return vasnprintf (ret, 0, format, args);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef HAVE_VASNPRINTF
|
||||||
|
int
|
||||||
|
vasnprintf (char **ret, size_t max_sz, const char *format, va_list args)
|
||||||
{
|
{
|
||||||
int st;
|
int st;
|
||||||
size_t len;
|
size_t len;
|
||||||
struct state state;
|
struct state state;
|
||||||
|
|
||||||
state.sz = 1;
|
state.max_sz = max_sz;
|
||||||
|
state.sz = min(1, max_sz);
|
||||||
state.str = malloc(state.sz);
|
state.str = malloc(state.sz);
|
||||||
if (state.str == NULL) {
|
if (state.str == NULL) {
|
||||||
*ret = NULL;
|
*ret = NULL;
|
||||||
@@ -175,6 +216,7 @@ vsnprintf (char *str, size_t sz, const char *format, va_list args)
|
|||||||
struct state state;
|
struct state state;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
state.max_sz = 0;
|
||||||
state.sz = sz;
|
state.sz = sz;
|
||||||
state.str = str;
|
state.str = str;
|
||||||
state.s = str;
|
state.s = str;
|
||||||
@@ -215,7 +257,10 @@ as_reserve (struct state *state, size_t n)
|
|||||||
while (state->s + n > state->theend) {
|
while (state->s + n > state->theend) {
|
||||||
int off = state->s - state->str;
|
int off = state->s - state->str;
|
||||||
|
|
||||||
state->sz *= 2;
|
if (state->max_sz && state->sz >= state->max_sz)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
state->sz = min(state->max_sz, state->sz*2);
|
||||||
state->str = realloc (state->str, state->sz);
|
state->str = realloc (state->str, state->sz);
|
||||||
if (state->str == NULL)
|
if (state->str == NULL)
|
||||||
return 1;
|
return 1;
|
||||||
@@ -241,7 +286,6 @@ append_number (struct state *state,
|
|||||||
unsigned long num, unsigned base, char *rep,
|
unsigned long num, unsigned base, char *rep,
|
||||||
int width, int zerop, int minusp)
|
int width, int zerop, int minusp)
|
||||||
{
|
{
|
||||||
char *beg;
|
|
||||||
int i, len;
|
int i, len;
|
||||||
|
|
||||||
len = 0;
|
len = 0;
|
||||||
|
Reference in New Issue
Block a user