Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdio / asnprintf.c
blob4c9ad586020ab0175fecdaf7c35b2f403a424b86
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 */
8 #include <_ansi.h>
9 #include <reent.h>
10 #include <stdio.h>
11 #include <stdarg.h>
12 #include <limits.h>
13 #include <errno.h>
14 #include "local.h"
16 char *
17 _asnprintf_r (struct _reent *__restrict ptr,
18 char *buf,
19 size_t *lenp,
20 const char *__restrict fmt, ...)
22 int ret;
23 va_list ap;
24 FILE f;
25 size_t len = *lenp;
27 if (buf && len)
29 /* mark an existing buffer, but allow allocation of larger string */
30 f._flags = __SWR | __SSTR | __SOPT;
32 else
34 /* mark a zero-length reallocatable buffer */
35 f._flags = __SWR | __SSTR | __SMBF;
36 len = 0;
37 buf = NULL;
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 FIXME - it would be nice to rewrite sys/reent.h to support size_t
43 for _size. */
44 if (len > INT_MAX)
46 _REENT_ERRNO(ptr) = EOVERFLOW;
47 return NULL;
49 f._bf._size = f._w = len;
50 f._file = -1; /* No file. */
51 va_start (ap, fmt);
52 ret = _svfprintf_r (ptr, &f, fmt, ap);
53 va_end (ap);
54 if (ret < 0)
55 return NULL;
56 *lenp = ret;
57 *f._p = '\0';
58 return (char *) f._bf._base;
61 #ifdef _NANO_FORMATTED_IO
62 char *
63 _asniprintf_r (struct _reent *, char *, size_t *, const char *, ...)
64 _ATTRIBUTE ((__alias__("_asnprintf_r")));
65 #endif
67 #ifndef _REENT_ONLY
69 char *
70 asnprintf (char *__restrict buf,
71 size_t *__restrict lenp,
72 const char *__restrict fmt, ...)
74 int ret;
75 va_list ap;
76 FILE f;
77 size_t len = *lenp;
78 struct _reent *ptr = _REENT;
80 if (buf && len)
82 /* mark an existing buffer, but allow allocation of larger string */
83 f._flags = __SWR | __SSTR | __SOPT;
85 else
87 /* mark a zero-length reallocatable buffer */
88 f._flags = __SWR | __SSTR | __SMBF;
89 len = 0;
90 buf = NULL;
92 f._flags2 = 0;
93 f._bf._base = f._p = (unsigned char *) buf;
94 /* For now, inherit the 32-bit signed limit of FILE._bf._size.
95 FIXME - it would be nice to rewrite sys/reent.h to support size_t
96 for _size. */
97 if (len > INT_MAX)
99 _REENT_ERRNO(ptr) = EOVERFLOW;
100 return NULL;
102 f._bf._size = f._w = len;
103 f._file = -1; /* No file. */
104 va_start (ap, fmt);
105 ret = _svfprintf_r (ptr, &f, fmt, ap);
106 va_end (ap);
107 if (ret < 0)
108 return NULL;
109 *lenp = ret;
110 *f._p = '\0';
111 return (char *) f._bf._base;
114 #ifdef _NANO_FORMATTED_IO
115 char *
116 asniprintf (char *, size_t *, const char *, ...)
117 _ATTRIBUTE ((__alias__("asnprintf")));
118 #endif
119 #endif /* ! _REENT_ONLY */