Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdio / vasniprintf.c
blobc85ca06e8c1ab914ed239d61ecf5cb0c622a8e07
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 */
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 _vasniprintf_r (struct _reent *ptr,
18 char *buf,
19 size_t *lenp,
20 const char *fmt,
21 va_list ap)
23 int ret;
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 ret = _svfiprintf_r (ptr, &f, fmt, ap);
52 if (ret < 0)
53 return NULL;
54 *lenp = ret;
55 *f._p = '\0';
56 return (char *) f._bf._base;
59 #ifndef _REENT_ONLY
61 char *
62 vasniprintf (char *buf,
63 size_t *lenp,
64 const char *fmt,
65 va_list ap)
67 return _vasniprintf_r (_REENT, buf, lenp, fmt, ap);
70 #endif /* ! _REENT_ONLY */