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 viprintf.c */
7 
8 #define _DEFAULT_SOURCE
9 #include <stdio.h>
10 #include <stdarg.h>
11 #include <limits.h>
12 #include <errno.h>
13 #include "local.h"
14 
15 char *
vasniprintf(char * buf,size_t * lenp,const char * fmt,va_list ap)16 vasniprintf (
17        char *buf,
18        size_t *lenp,
19        const char *fmt,
20        va_list ap)
21 {
22   int ret;
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._flags2 = 0;
39   f._bf._base = f._p = (unsigned char *) buf;
40   /* For now, inherit the 32-bit signed limit of FILE._bf._size.
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   ret = svfiprintf ( &f, fmt, ap);
50   if (ret < 0)
51     return NULL;
52   *lenp = ret;
53   *f._p = '\0';
54   return (char *) f._bf._base;
55 }
56