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 <stdio.h>
10 #include <stdarg.h>
11 #include <limits.h>
12 #include <errno.h>
13 #include "local.h"
14
15 char *
asniprintf(char * buf,size_t * lenp,const char * fmt,...)16 asniprintf (char *buf,
17 size_t *lenp,
18 const char *fmt, ...)
19 {
20 int ret;
21 va_list ap;
22 FILE f;
23 size_t len = *lenp;
24
25 if (buf && len)
26 {
27 /* mark an existing buffer, but allow allocation of larger string */
28 f._flags = __SWR | __SSTR | __SOPT;
29 }
30 else
31 {
32 /* mark a zero-length reallocatable buffer */
33 f._flags = __SWR | __SSTR | __SMBF;
34 len = 0;
35 buf = NULL;
36 }
37 f._flags2 = 0;
38 f._bf._base = f._p = (unsigned char *) buf;
39 /* For now, inherit the 32-bit signed limit of FILE._bf._size.
40 for _size. */
41 if (len > INT_MAX)
42 {
43 _REENT_ERRNO(ptr) = EOVERFLOW;
44 return NULL;
45 }
46 f._bf._size = f._w = len;
47 f._file = -1; /* No file. */
48 va_start (ap, fmt);
49 ret = svfiprintf ( &f, fmt, ap);
50 va_end (ap);
51 if (ret < 0)
52 return NULL;
53 *lenp = ret;
54 *f._p = '\0';
55 return (char *) f._bf._base;
56 }
57