1 /* Copyright (C) 2007, 2008 Eric Blake
2  * Permission to use, copy, modify, and distribute this software
3  * is freely granted, provided that this notice is preserved.
4  */
5 /* This code was derived from asprintf.c */
6 /* doc in siprintf.c */
7 
8 #define _DEFAULT_SOURCE
9 #include <_ansi.h>
10 #include <stdio.h>
11 #include <stdarg.h>
12 #include <limits.h>
13 #include <errno.h>
14 #include "local.h"
15 
16 char *
asniprintf(char * buf,size_t * lenp,const char * fmt,...)17 asniprintf (char *buf,
18        size_t *lenp,
19        const char *fmt, ...)
20 {
21   int ret;
22   va_list ap;
23   FILE f;
24   size_t len = *lenp;
25 
26   if (buf && len)
27     {
28       /* mark an existing buffer, but allow allocation of larger string */
29       f._flags = __SWR | __SSTR | __SOPT;
30     }
31   else
32     {
33       /* mark a zero-length reallocatable buffer */
34       f._flags = __SWR | __SSTR | __SMBF;
35       len = 0;
36       buf = NULL;
37     }
38   f._bf._base = f._p = (unsigned char *) buf;
39   /* For now, inherit the 32-bit signed limit of FILE._bf._size.
40      FIXME - it would be nice to rewrite sys/reent.h to support size_t
41      for _size.  */
42   if (len > INT_MAX)
43     {
44       _REENT_ERRNO(ptr) = EOVERFLOW;
45       return NULL;
46     }
47   f._bf._size = f._w = len;
48   f._file = -1;  /* No file. */
49   va_start (ap, fmt);
50   ret = svfiprintf ( &f, fmt, ap);
51   va_end (ap);
52   if (ret < 0)
53     return NULL;
54   *lenp = ret;
55   *f._p = '\0';
56   return (char *) f._bf._base;
57 }
58