Blame lib/snprintf.c

Packit 022b05
/*
Packit 022b05
 * Copyright (c) 1995-1999 Kungliga Tekniska Högskolan
Packit 022b05
 * (Royal Institute of Technology, Stockholm, Sweden).
Packit 022b05
 * All rights reserved.
Packit 022b05
 * 
Packit 022b05
 * Redistribution and use in source and binary forms, with or without
Packit 022b05
 * modification, are permitted provided that the following conditions
Packit 022b05
 * are met:
Packit 022b05
 * 
Packit 022b05
 * 1. Redistributions of source code must retain the above copyright
Packit 022b05
 *    notice, this list of conditions and the following disclaimer.
Packit 022b05
 * 
Packit 022b05
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 022b05
 *    notice, this list of conditions and the following disclaimer in the
Packit 022b05
 *    documentation and/or other materials provided with the distribution.
Packit 022b05
 * 
Packit 022b05
 * 3. Neither the name of the Institute nor the names of its contributors
Packit 022b05
 *    may be used to endorse or promote products derived from this software
Packit 022b05
 *    without specific prior written permission.
Packit 022b05
 * 
Packit 022b05
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
Packit 022b05
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 022b05
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 022b05
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
Packit 022b05
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 022b05
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 022b05
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 022b05
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 022b05
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 022b05
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 022b05
 * SUCH DAMAGE.
Packit 022b05
 */
Packit 022b05
Packit 022b05
/* $Id: snprintf.c 1429 2002-07-23 23:12:20Z strauss $ */
Packit 022b05
Packit 022b05
#include <config.h>
Packit 022b05
Packit 022b05
#include <stdio.h>
Packit 022b05
#include <stdarg.h>
Packit 022b05
#include <stdlib.h>
Packit 022b05
#include <string.h>
Packit 022b05
#include <ctype.h>
Packit 022b05
#include <sys/types.h>
Packit 022b05
Packit 022b05
#ifdef HAVE_DMALLOC_H
Packit 022b05
#include <dmalloc.h>
Packit 022b05
#endif
Packit 022b05
Packit 022b05
#include "snprintf.h"
Packit 022b05
Packit 022b05
#ifndef MIN
Packit 022b05
#define MIN(a, b)       ((a) < (b) ? (a) : (b))
Packit 022b05
#define MAX(a, b)       ((a) < (b) ? (b) : (a))
Packit 022b05
#endif
Packit 022b05
Packit 022b05
Packit 022b05
Packit 022b05
enum format_flags {
Packit 022b05
    minus_flag     =  1,
Packit 022b05
    plus_flag      =  2,
Packit 022b05
    space_flag     =  4,
Packit 022b05
    alternate_flag =  8,
Packit 022b05
    zero_flag      = 16
Packit 022b05
};
Packit 022b05
Packit 022b05
/*
Packit 022b05
 * Common state
Packit 022b05
 */
Packit 022b05
Packit 022b05
struct state {
Packit 022b05
  unsigned char *str;
Packit 022b05
  unsigned char *s;
Packit 022b05
  unsigned char *theend;
Packit 022b05
  size_t sz;
Packit 022b05
  size_t max_sz;
Packit 022b05
  int (*append_char)(struct state *, unsigned char);
Packit 022b05
  int (*reserve)(struct state *, size_t);
Packit 022b05
  /* XXX - methods */
Packit 022b05
};
Packit 022b05
Packit 022b05
#ifndef HAVE_VSNPRINTF
Packit 022b05
static int
Packit 022b05
sn_reserve (struct state *state, size_t n)
Packit 022b05
{
Packit 022b05
  return state->s + n > state->theend;
Packit 022b05
}
Packit 022b05
Packit 022b05
static int
Packit 022b05
sn_append_char (struct state *state, unsigned char c)
Packit 022b05
{
Packit 022b05
  if (sn_reserve (state, 1)) {
Packit 022b05
    return 1;
Packit 022b05
  } else {
Packit 022b05
    *state->s++ = c;
Packit 022b05
    return 0;
Packit 022b05
  }
Packit 022b05
}
Packit 022b05
#endif
Packit 022b05
Packit 022b05
static int
Packit 022b05
as_reserve (struct state *state, size_t n)
Packit 022b05
{
Packit 022b05
  if (state->s + n > state->theend) {
Packit 022b05
    int off = state->s - state->str;
Packit 022b05
    unsigned char *tmp;
Packit 022b05
Packit 022b05
    if (state->max_sz && state->sz >= state->max_sz)
Packit 022b05
      return 1;
Packit 022b05
Packit 022b05
    state->sz = MAX(state->sz * 2, state->sz + n);
Packit 022b05
    if (state->max_sz)
Packit 022b05
      state->sz = MIN(state->sz, state->max_sz);
Packit 022b05
    tmp = realloc (state->str, state->sz);
Packit 022b05
    if (tmp == NULL)
Packit 022b05
      return 1;
Packit 022b05
    state->str = tmp;
Packit 022b05
    state->s = state->str + off;
Packit 022b05
    state->theend = state->str + state->sz - 1;
Packit 022b05
  }
Packit 022b05
  return 0;
Packit 022b05
}
Packit 022b05
Packit 022b05
static int
Packit 022b05
as_append_char (struct state *state, unsigned char c)
Packit 022b05
{
Packit 022b05
  if(as_reserve (state, 1))
Packit 022b05
    return 1;
Packit 022b05
  else {
Packit 022b05
    *state->s++ = c;
Packit 022b05
    return 0;
Packit 022b05
  }
Packit 022b05
}
Packit 022b05
Packit 022b05
static int
Packit 022b05
append_number(struct state *state,
Packit 022b05
	      unsigned long num, unsigned base, char *rep,
Packit 022b05
	      int width, int prec, int flags, int minusp)
Packit 022b05
{
Packit 022b05
  int len = 0;
Packit 022b05
  int i;
Packit 022b05
Packit 022b05
  /* given precision, ignore zero flag */
Packit 022b05
  if(prec != -1)
Packit 022b05
    flags &= ~zero_flag;
Packit 022b05
  else
Packit 022b05
    prec = 1;
Packit 022b05
  /* zero value with zero precision -> "" */
Packit 022b05
  if(prec == 0 && num == 0)
Packit 022b05
    return 0;
Packit 022b05
  do{
Packit 022b05
    if((*state->append_char)(state, rep[num % base]))
Packit 022b05
      return 1;
Packit 022b05
    len++;
Packit 022b05
    num /= base;
Packit 022b05
  }while(num);
Packit 022b05
  prec -= len;
Packit 022b05
  /* pad with prec zeros */
Packit 022b05
  while(prec-- > 0){
Packit 022b05
    if((*state->append_char)(state, '0'))
Packit 022b05
      return 1;
Packit 022b05
    len++;
Packit 022b05
  }
Packit 022b05
  /* add length of alternate prefix (added later) to len */
Packit 022b05
  if(flags & alternate_flag && (base == 16 || base == 8))
Packit 022b05
    len += base / 8;
Packit 022b05
  /* pad with zeros */
Packit 022b05
  if(flags & zero_flag){
Packit 022b05
    width -= len;
Packit 022b05
    if(minusp || (flags & space_flag) || (flags & plus_flag))
Packit 022b05
      width--;
Packit 022b05
    while(width-- > 0){
Packit 022b05
      if((*state->append_char)(state, '0'))
Packit 022b05
	return 1;
Packit 022b05
      len++;
Packit 022b05
    }
Packit 022b05
  }
Packit 022b05
  /* add alternate prefix */
Packit 022b05
  if(flags & alternate_flag && (base == 16 || base == 8)){
Packit 022b05
    if(base == 16)
Packit 022b05
      if((*state->append_char)(state, rep[10] + 23)) /* XXX */
Packit 022b05
	return 1;
Packit 022b05
    if((*state->append_char)(state, '0'))
Packit 022b05
      return 1;
Packit 022b05
  }
Packit 022b05
  /* add sign */
Packit 022b05
  if(minusp){
Packit 022b05
    if((*state->append_char)(state, '-'))
Packit 022b05
      return 1;
Packit 022b05
    len++;
Packit 022b05
  } else if(flags & plus_flag) {
Packit 022b05
    if((*state->append_char)(state, '+'))
Packit 022b05
      return 1;
Packit 022b05
    len++;
Packit 022b05
  } else if(flags & space_flag) {
Packit 022b05
    if((*state->append_char)(state, ' '))
Packit 022b05
      return 1;
Packit 022b05
    len++;
Packit 022b05
  }
Packit 022b05
  if(flags & minus_flag)
Packit 022b05
    /* swap before padding with spaces */
Packit 022b05
    for(i = 0; i < len / 2; i++){
Packit 022b05
      char c = state->s[-i-1];
Packit 022b05
      state->s[-i-1] = state->s[-len+i];
Packit 022b05
      state->s[-len+i] = c;
Packit 022b05
    }
Packit 022b05
  width -= len;
Packit 022b05
  while(width-- > 0){
Packit 022b05
    if((*state->append_char)(state,  ' '))
Packit 022b05
      return 1;
Packit 022b05
    len++;
Packit 022b05
  }
Packit 022b05
  if(!(flags & minus_flag))
Packit 022b05
    /* swap after padding with spaces */
Packit 022b05
    for(i = 0; i < len / 2; i++){
Packit 022b05
      char c = state->s[-i-1];
Packit 022b05
      state->s[-i-1] = state->s[-len+i];
Packit 022b05
      state->s[-len+i] = c;
Packit 022b05
    }
Packit 022b05
    
Packit 022b05
  return 0;
Packit 022b05
}
Packit 022b05
Packit 022b05
static int
Packit 022b05
append_string (struct state *state,
Packit 022b05
	       unsigned char *arg,
Packit 022b05
	       int width,
Packit 022b05
	       int prec,
Packit 022b05
	       int flags)
Packit 022b05
{
Packit 022b05
  if(prec != -1)
Packit 022b05
    width -= prec;
Packit 022b05
  else
Packit 022b05
    width -= strlen((char *)arg);
Packit 022b05
  if(!(flags & minus_flag))
Packit 022b05
    while(width-- > 0)
Packit 022b05
      if((*state->append_char) (state, ' '))
Packit 022b05
	return 1;
Packit 022b05
  if (prec != -1) {
Packit 022b05
    while (*arg && prec--)
Packit 022b05
      if ((*state->append_char) (state, *arg++))
Packit 022b05
	return 1;
Packit 022b05
  } else {
Packit 022b05
    while (*arg)
Packit 022b05
      if ((*state->append_char) (state, *arg++))
Packit 022b05
	return 1;
Packit 022b05
  }
Packit 022b05
  if(flags & minus_flag)
Packit 022b05
    while(width-- > 0)
Packit 022b05
      if((*state->append_char) (state, ' '))
Packit 022b05
	return 1;
Packit 022b05
  return 0;
Packit 022b05
}
Packit 022b05
Packit 022b05
static int
Packit 022b05
append_char(struct state *state,
Packit 022b05
	    unsigned char arg,
Packit 022b05
	    int width,
Packit 022b05
	    int flags)
Packit 022b05
{
Packit 022b05
  while(!(flags & minus_flag) && --width > 0)
Packit 022b05
    if((*state->append_char) (state, ' '))
Packit 022b05
      return 1;
Packit 022b05
    
Packit 022b05
  if((*state->append_char) (state, arg))
Packit 022b05
    return 1;
Packit 022b05
  while((flags & minus_flag) && --width > 0)
Packit 022b05
    if((*state->append_char) (state, ' '))
Packit 022b05
      return 1;
Packit 022b05
    
Packit 022b05
  return 0;
Packit 022b05
}
Packit 022b05
Packit 022b05
/*
Packit 022b05
 * This can't be made into a function...
Packit 022b05
 */
Packit 022b05
Packit 022b05
#define PARSE_INT_FORMAT(res, arg, unsig) \
Packit 022b05
if (long_flag) \
Packit 022b05
     res = (unsig long)va_arg(arg, unsig long); \
Packit 022b05
else if (short_flag) \
Packit 022b05
     res = (unsig short)va_arg(arg, unsig int); \
Packit 022b05
else \
Packit 022b05
     res = (unsig int)va_arg(arg, unsig int)
Packit 022b05
Packit 022b05
/*
Packit 022b05
 * zyxprintf - return 0 or -1
Packit 022b05
 */
Packit 022b05
Packit 022b05
static int
Packit 022b05
xyzprintf (struct state *state, const char *char_format, va_list ap)
Packit 022b05
{
Packit 022b05
  const unsigned char *format = (const unsigned char *)char_format;
Packit 022b05
  unsigned char c;
Packit 022b05
Packit 022b05
  while((c = *format++)) {
Packit 022b05
    if (c == '%') {
Packit 022b05
      int flags      = 0;
Packit 022b05
      int width      = 0;
Packit 022b05
      int prec       = -1;
Packit 022b05
      int long_flag  = 0;
Packit 022b05
      int short_flag = 0;
Packit 022b05
Packit 022b05
      /* flags */
Packit 022b05
      while((c = *format++)){
Packit 022b05
	if(c == '-')
Packit 022b05
	  flags |= minus_flag;
Packit 022b05
	else if(c == '+')
Packit 022b05
	  flags |= plus_flag;
Packit 022b05
	else if(c == ' ')
Packit 022b05
	  flags |= space_flag;
Packit 022b05
	else if(c == '#')
Packit 022b05
	  flags |= alternate_flag;
Packit 022b05
	else if(c == '0')
Packit 022b05
	  flags |= zero_flag;
Packit 022b05
	else
Packit 022b05
	  break;
Packit 022b05
      }
Packit 022b05
      
Packit 022b05
      if((flags & space_flag) && (flags & plus_flag))
Packit 022b05
	flags ^= space_flag;
Packit 022b05
Packit 022b05
      if((flags & minus_flag) && (flags & zero_flag))
Packit 022b05
	flags ^= zero_flag;
Packit 022b05
Packit 022b05
      /* width */
Packit 022b05
      if (isdigit(c))
Packit 022b05
	do {
Packit 022b05
	  width = width * 10 + c - '0';
Packit 022b05
	  c = *format++;
Packit 022b05
	} while(isdigit(c));
Packit 022b05
      else if(c == '*') {
Packit 022b05
	width = va_arg(ap, int);
Packit 022b05
	c = *format++;
Packit 022b05
      }
Packit 022b05
Packit 022b05
      /* precision */
Packit 022b05
      if (c == '.') {
Packit 022b05
	prec = 0;
Packit 022b05
	c = *format++;
Packit 022b05
	if (isdigit(c))
Packit 022b05
	  do {
Packit 022b05
	    prec = prec * 10 + c - '0';
Packit 022b05
	    c = *format++;
Packit 022b05
	  } while(isdigit(c));
Packit 022b05
	else if (c == '*') {
Packit 022b05
	  prec = va_arg(ap, int);
Packit 022b05
	  c = *format++;
Packit 022b05
	}
Packit 022b05
      }
Packit 022b05
Packit 022b05
      /* size */
Packit 022b05
Packit 022b05
      if (c == 'h') {
Packit 022b05
	short_flag = 1;
Packit 022b05
	c = *format++;
Packit 022b05
      } else if (c == 'l') {
Packit 022b05
	long_flag = 1;
Packit 022b05
	c = *format++;
Packit 022b05
      }
Packit 022b05
Packit 022b05
      switch (c) {
Packit 022b05
      case 'c' :
Packit 022b05
	if(append_char(state, va_arg(ap, int), width, flags))
Packit 022b05
	  return -1;
Packit 022b05
	break;
Packit 022b05
      case 's' :
Packit 022b05
	if (append_string(state,
Packit 022b05
			  va_arg(ap, unsigned char*),
Packit 022b05
			  width,
Packit 022b05
			  prec, 
Packit 022b05
			  flags))
Packit 022b05
	  return -1;
Packit 022b05
	break;
Packit 022b05
      case 'd' :
Packit 022b05
      case 'i' : {
Packit 022b05
	long arg;
Packit 022b05
	unsigned long num;
Packit 022b05
	int minusp = 0;
Packit 022b05
Packit 022b05
	PARSE_INT_FORMAT(arg, ap, signed);
Packit 022b05
Packit 022b05
	if (arg < 0) {
Packit 022b05
	  minusp = 1;
Packit 022b05
	  num = -arg;
Packit 022b05
	} else
Packit 022b05
	  num = arg;
Packit 022b05
Packit 022b05
	if (append_number (state, num, 10, "0123456789",
Packit 022b05
			   width, prec, flags, minusp))
Packit 022b05
	  return -1;
Packit 022b05
	break;
Packit 022b05
      }
Packit 022b05
      case 'u' : {
Packit 022b05
	unsigned long arg;
Packit 022b05
Packit 022b05
	PARSE_INT_FORMAT(arg, ap, unsigned);
Packit 022b05
Packit 022b05
	if (append_number (state, arg, 10, "0123456789",
Packit 022b05
			   width, prec, flags, 0))
Packit 022b05
	  return -1;
Packit 022b05
	break;
Packit 022b05
      }
Packit 022b05
      case 'o' : {
Packit 022b05
	unsigned long arg;
Packit 022b05
Packit 022b05
	PARSE_INT_FORMAT(arg, ap, unsigned);
Packit 022b05
Packit 022b05
	if (append_number (state, arg, 010, "01234567",
Packit 022b05
			   width, prec, flags, 0))
Packit 022b05
	  return -1;
Packit 022b05
	break;
Packit 022b05
      }
Packit 022b05
      case 'x' : {
Packit 022b05
	unsigned long arg;
Packit 022b05
Packit 022b05
	PARSE_INT_FORMAT(arg, ap, unsigned);
Packit 022b05
Packit 022b05
	if (append_number (state, arg, 0x10, "0123456789abcdef",
Packit 022b05
			   width, prec, flags, 0))
Packit 022b05
	  return -1;
Packit 022b05
	break;
Packit 022b05
      }
Packit 022b05
      case 'X' :{
Packit 022b05
	unsigned long arg;
Packit 022b05
Packit 022b05
	PARSE_INT_FORMAT(arg, ap, unsigned);
Packit 022b05
Packit 022b05
	if (append_number (state, arg, 0x10, "0123456789ABCDEF",
Packit 022b05
			   width, prec, flags, 0))
Packit 022b05
	  return -1;
Packit 022b05
	break;
Packit 022b05
      }
Packit 022b05
      case 'p' : {
Packit 022b05
	unsigned long arg = (unsigned long)va_arg(ap, void*);
Packit 022b05
Packit 022b05
	if (append_number (state, arg, 0x10, "0123456789ABCDEF",
Packit 022b05
			   width, prec, flags, 0))
Packit 022b05
	  return -1;
Packit 022b05
	break;
Packit 022b05
      }
Packit 022b05
      case 'n' : {
Packit 022b05
	int *arg = va_arg(ap, int*);
Packit 022b05
	*arg = state->s - state->str;
Packit 022b05
	break;
Packit 022b05
      }
Packit 022b05
      case '\0' :
Packit 022b05
	  --format;
Packit 022b05
	  /* FALLTHROUGH */
Packit 022b05
      case '%' :
Packit 022b05
	if ((*state->append_char)(state, c))
Packit 022b05
	  return -1;
Packit 022b05
	break;
Packit 022b05
      default :
Packit 022b05
	if (   (*state->append_char)(state, '%')
Packit 022b05
	    || (*state->append_char)(state, c))
Packit 022b05
	  return -1;
Packit 022b05
	break;
Packit 022b05
      }
Packit 022b05
    } else
Packit 022b05
      if ((*state->append_char) (state, c))
Packit 022b05
	return -1;
Packit 022b05
  }
Packit 022b05
  return 0;
Packit 022b05
}
Packit 022b05
Packit 022b05
#ifndef HAVE_SNPRINTF
Packit 022b05
int
Packit 022b05
snprintf (char *str, size_t sz, const char *format, ...)
Packit 022b05
{
Packit 022b05
  va_list args;
Packit 022b05
  int ret;
Packit 022b05
Packit 022b05
  va_start(args, format);
Packit 022b05
  ret = vsnprintf (str, sz, format, args);
Packit 022b05
Packit 022b05
#ifdef PARANOIA
Packit 022b05
  {
Packit 022b05
    int ret2;
Packit 022b05
    char *tmp;
Packit 022b05
Packit 022b05
    tmp = malloc (sz);
Packit 022b05
    if (tmp == NULL)
Packit 022b05
      abort ();
Packit 022b05
Packit 022b05
    ret2 = vsprintf (tmp, format, args);
Packit 022b05
    if (ret != ret2 || strcmp(str, tmp))
Packit 022b05
      abort ();
Packit 022b05
    free (tmp);
Packit 022b05
  }
Packit 022b05
#endif
Packit 022b05
Packit 022b05
  va_end(args);
Packit 022b05
  return ret;
Packit 022b05
}
Packit 022b05
#endif
Packit 022b05
Packit 022b05
#ifndef HAVE_ASPRINTF
Packit 022b05
int
Packit 022b05
asprintf (char **ret, const char *format, ...)
Packit 022b05
{
Packit 022b05
  va_list args;
Packit 022b05
  int val;
Packit 022b05
Packit 022b05
  va_start(args, format);
Packit 022b05
  val = vasprintf (ret, format, args);
Packit 022b05
Packit 022b05
#ifdef PARANOIA
Packit 022b05
  {
Packit 022b05
    int ret2;
Packit 022b05
    char *tmp;
Packit 022b05
    tmp = malloc (val + 1);
Packit 022b05
    if (tmp == NULL)
Packit 022b05
      abort ();
Packit 022b05
Packit 022b05
    ret2 = vsprintf (tmp, format, args);
Packit 022b05
    if (val != ret2 || strcmp(*ret, tmp))
Packit 022b05
      abort ();
Packit 022b05
    free (tmp);
Packit 022b05
  }
Packit 022b05
#endif
Packit 022b05
Packit 022b05
  va_end(args);
Packit 022b05
  return val;
Packit 022b05
}
Packit 022b05
#endif
Packit 022b05
Packit 022b05
#ifndef HAVE_ASNPRINTF
Packit 022b05
int
Packit 022b05
asnprintf (char **ret, size_t max_sz, const char *format, ...)
Packit 022b05
{
Packit 022b05
  va_list args;
Packit 022b05
  int val;
Packit 022b05
Packit 022b05
  va_start(args, format);
Packit 022b05
  val = vasnprintf (ret, max_sz, format, args);
Packit 022b05
Packit 022b05
#ifdef PARANOIA
Packit 022b05
  {
Packit 022b05
    int ret2;
Packit 022b05
    char *tmp;
Packit 022b05
    tmp = malloc (val + 1);
Packit 022b05
    if (tmp == NULL)
Packit 022b05
      abort ();
Packit 022b05
Packit 022b05
    ret2 = vsprintf (tmp, format, args);
Packit 022b05
    if (val != ret2 || strcmp(*ret, tmp))
Packit 022b05
      abort ();
Packit 022b05
    free (tmp);
Packit 022b05
  }
Packit 022b05
#endif
Packit 022b05
Packit 022b05
  va_end(args);
Packit 022b05
  return val;
Packit 022b05
}
Packit 022b05
#endif
Packit 022b05
Packit 022b05
#ifndef HAVE_VASPRINTF
Packit 022b05
int
Packit 022b05
vasprintf (char **ret, const char *format, va_list args)
Packit 022b05
{
Packit 022b05
  return vasnprintf (ret, 0, format, args);
Packit 022b05
}
Packit 022b05
#endif
Packit 022b05
Packit 022b05
Packit 022b05
#ifndef HAVE_VASNPRINTF
Packit 022b05
int
Packit 022b05
vasnprintf (char **ret, size_t max_sz, const char *format, va_list args)
Packit 022b05
{
Packit 022b05
  int st;
Packit 022b05
  size_t len;
Packit 022b05
  struct state state;
Packit 022b05
Packit 022b05
  state.max_sz = max_sz;
Packit 022b05
  state.sz     = 1;
Packit 022b05
  state.str    = malloc(state.sz);
Packit 022b05
  if (state.str == NULL) {
Packit 022b05
    *ret = NULL;
Packit 022b05
    return -1;
Packit 022b05
  }
Packit 022b05
  state.s = state.str;
Packit 022b05
  state.theend = state.s + state.sz - 1;
Packit 022b05
  state.append_char = as_append_char;
Packit 022b05
  state.reserve     = as_reserve;
Packit 022b05
Packit 022b05
  st = xyzprintf (&state, format, args);
Packit 022b05
  if (st) {
Packit 022b05
    free (state.str);
Packit 022b05
    *ret = NULL;
Packit 022b05
    return -1;
Packit 022b05
  } else {
Packit 022b05
    char *tmp;
Packit 022b05
Packit 022b05
    *state.s = '\0';
Packit 022b05
    len = state.s - state.str;
Packit 022b05
    tmp = realloc (state.str, len+1);
Packit 022b05
    if (tmp == NULL) {
Packit 022b05
      free (state.str);
Packit 022b05
      *ret = NULL;
Packit 022b05
      return -1;
Packit 022b05
    }
Packit 022b05
    *ret = tmp;
Packit 022b05
    return len;
Packit 022b05
  }
Packit 022b05
}
Packit 022b05
#endif
Packit 022b05
Packit 022b05
#ifndef HAVE_VSNPRINTF
Packit 022b05
int
Packit 022b05
vsnprintf (char *str, size_t sz, const char *format, va_list args)
Packit 022b05
{
Packit 022b05
  struct state state;
Packit 022b05
  int ret;
Packit 022b05
  unsigned char *ustr = (unsigned char *)str;
Packit 022b05
Packit 022b05
  state.max_sz = 0;
Packit 022b05
  state.sz     = sz;
Packit 022b05
  state.str    = ustr;
Packit 022b05
  state.s      = ustr;
Packit 022b05
  state.theend = ustr + sz - 1;
Packit 022b05
  state.append_char = sn_append_char;
Packit 022b05
  state.reserve     = sn_reserve;
Packit 022b05
Packit 022b05
  ret = xyzprintf (&state, format, args);
Packit 022b05
  *state.s = '\0';
Packit 022b05
  if (ret)
Packit 022b05
    return sz;
Packit 022b05
  else
Packit 022b05
    return state.s - state.str;
Packit 022b05
}
Packit 022b05
#endif