(memchr): Don't do arithmetic on `void *'.
[glibc/history.git] / stdio / vasprintf.c
blobffcf3eb97297c01f03daed06b6405173bb04e750
1 /* Copyright (C) 1991, 1992, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <stddef.h>
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
27 /* Enlarge STREAM's buffer. */
28 static void
29 enlarge_buffer (FILE *stream, int c)
31 ptrdiff_t bufp_offset = stream->__bufp - stream->__buffer;
32 char *newbuf;
34 stream->__bufsize += 100;
35 newbuf = (char *) realloc ((void *) stream->__buffer, stream->__bufsize);
36 if (newbuf == NULL)
38 free ((void *) stream->__buffer);
39 stream->__buffer = stream->__bufp
40 = stream->__put_limit = stream->__get_limit = NULL;
41 stream->__error = 1;
43 else
45 stream->__buffer = newbuf;
46 stream->__bufp = stream->__buffer + bufp_offset;
47 stream->__get_limit = stream->__put_limit;
48 stream->__put_limit = stream->__buffer + stream->__bufsize;
49 if (c != EOF)
50 *stream->__bufp++ = (unsigned char) c;
54 /* Write formatted output from FORMAT to a string which is
55 allocated with malloc and stored in *STRING_PTR. */
56 int
57 vasprintf (char **string_ptr,
58 const char *format,
59 va_list args)
61 FILE f;
62 int done;
64 memset ((void *) &f, 0, sizeof (f));
65 f.__magic = _IOMAGIC;
66 f.__bufsize = 100;
67 f.__buffer = (char *) malloc (f.__bufsize);
68 if (f.__buffer == NULL)
69 return -1;
70 f.__bufp = f.__buffer;
71 f.__put_limit = f.__buffer + f.__bufsize;
72 f.__mode.__write = 1;
73 f.__room_funcs.__output = enlarge_buffer;
74 f.__seen = 1;
76 done = vfprintf (&f, format, args);
77 if (done < 0)
78 return done;
80 *string_ptr = realloc (f.__buffer, (f.__bufp - f.__buffer) + 1);
81 if (*string_ptr == NULL)
82 *string_ptr = f.__buffer;
83 (*string_ptr)[f.__bufp - f.__buffer] = '\0';
84 return done;