Mon Feb 19 15:30:26 1996 Roland McGrath <roland@charlie-brown.gnu.ai.mit.edu>
[glibc/history.git] / libio / vasprintf.c
blobee92f83b1cf06baaf3141d0dede99517042fb78f
1 /*
2 Copyright (C) 1995 Free Software Foundation
4 This file is part of the GNU IO Library. This library is free
5 software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU CC; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19 As a special exception, if you link this library with files
20 compiled with a GNU compiler to produce an executable, this does not cause
21 the resulting executable to be covered by the GNU General Public License.
22 This exception does not however invalidate any other reasons why
23 the executable file might be covered by the GNU General Public License. */
25 #include <malloc.h>
26 #include "libioP.h"
27 #include "stdio.h"
28 #include "strfile.h"
30 int
31 _IO_vasprintf (result_ptr, format, args)
32 char **result_ptr;
33 const char *format;
34 _IO_va_list args;
36 /* Initial size of the buffer to be used. Will be doubled each time an
37 overflow occurs. */
38 const _IO_size_t init_string_size = 100;
39 char *string;
40 _IO_strfile sf;
41 int ret;
42 string = ALLOC_BUF(init_string_size);
43 if (string == NULL)
44 return -1;
45 _IO_init((_IO_FILE*)&sf, 0);
46 _IO_JUMPS((_IO_FILE*)&sf) = &_IO_str_jumps;
47 _IO_str_init_static ((_IO_FILE*)&sf, string, init_string_size, string);
48 sf._f._flags &= ~_IO_USER_BUF;
49 sf._s._allocate_buffer = (_IO_alloc_type)malloc;
50 sf._s._free_buffer = (_IO_free_type)free;
51 ret = _IO_vfprintf((_IO_FILE*)&sf, format, args);
52 if (ret < 0)
53 return ret;
54 *result_ptr = (char*)realloc(sf._f._IO_buf_base,
55 (sf._f._IO_write_ptr - sf._f._IO_write_base) +1);
56 if (*result_ptr == NULL)
57 *result_ptr = sf._f._IO_buf_base;
58 (*result_ptr)[sf._f._IO_write_ptr-sf._f._IO_write_base] = '\0';
59 return ret;
61 weak_alias (_IO_vasprintf, vasprintf)