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 sprintf.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 *
asnprintf(char * buf,size_t * lenp,const char * __restrict fmt,...)17 asnprintf (
18 char *buf,
19 size_t *lenp,
20 const char *__restrict fmt, ...)
21 {
22 int ret;
23 va_list ap;
24 FILE f;
25 size_t len = *lenp;
26
27 if (buf && len)
28 {
29 /* mark an existing buffer, but allow allocation of larger string */
30 f._flags = __SWR | __SSTR | __SOPT;
31 }
32 else
33 {
34 /* mark a zero-length reallocatable buffer */
35 f._flags = __SWR | __SSTR | __SMBF;
36 len = 0;
37 buf = NULL;
38 }
39 f._flags2 = 0;
40 f._bf._base = f._p = (unsigned char *) buf;
41 /* For now, inherit the 32-bit signed limit of FILE._bf._size.
42 for _size. */
43 if (len > INT_MAX)
44 {
45 _REENT_ERRNO(ptr) = EOVERFLOW;
46 return NULL;
47 }
48 f._bf._size = f._w = len;
49 f._file = -1; /* No file. */
50 va_start (ap, fmt);
51 ret = svfprintf (&f, fmt, ap);
52 va_end (ap);
53 if (ret < 0)
54 return NULL;
55 *lenp = ret;
56 *f._p = '\0';
57 return (char *) f._bf._base;
58 }
59
60 #ifdef _NANO_FORMATTED_IO
61 char *
62 asniprintf (char *, size_t *, const char *, ...)
63 _ATTRIBUTE ((__alias__("asnprintf")));
64 #endif
65