1999-03-19 Andreas Jaeger <aj@arthur.rhein-neckar.de>
[glibc.git] / libio / vasprintf.c
blob15513d0fca7dc73592b6fe3dbac419fa01035cf9
1 /* Copyright (C) 1995, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU IO Library.
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2, or (at
7 your option) any later version.
9 This library is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this library; see the file COPYING. If not, write to
16 the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
17 MA 02111-1307, 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
21 not cause the resulting executable to be covered by the GNU General
22 Public License. This exception does not however invalidate any
23 other reasons why the executable file might be covered by the GNU
24 General Public License. */
26 #include <malloc.h>
27 #include "libioP.h"
28 #include "stdio.h"
29 #include "strfile.h"
31 int
32 _IO_vasprintf (result_ptr, format, args)
33 char **result_ptr;
34 const char *format;
35 _IO_va_list args;
37 /* Initial size of the buffer to be used. Will be doubled each time an
38 overflow occurs. */
39 const _IO_size_t init_string_size = 100;
40 char *string;
41 _IO_strfile sf;
42 #ifdef _IO_MTSAFE_IO
43 _IO_lock_t lock;
44 #endif
45 int ret;
46 string = (char *) malloc (init_string_size);
47 if (string == NULL)
48 return -1;
49 #ifdef _IO_MTSAFE_IO
50 sf._sbf._f._lock = &lock;
51 #endif
52 _IO_init ((_IO_FILE *) &sf, 0);
53 _IO_JUMPS ((_IO_FILE *) &sf) = &_IO_str_jumps;
54 _IO_str_init_static ((_IO_FILE *) &sf, string, init_string_size, string);
55 sf._sbf._f._flags &= ~_IO_USER_BUF;
56 sf._s._allocate_buffer = (_IO_alloc_type) malloc;
57 sf._s._free_buffer = (_IO_free_type) free;
58 ret = _IO_vfprintf ((_IO_FILE *) &sf, format, args);
59 if (ret < 0)
60 return ret;
61 *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base,
62 (sf._sbf._f._IO_write_ptr
63 - sf._sbf._f._IO_write_base) +1);
64 if (*result_ptr == NULL)
65 *result_ptr = sf._sbf._f._IO_buf_base;
66 (*result_ptr)[sf._sbf._f._IO_write_ptr-sf._sbf._f._IO_write_base] = '\0';
67 return ret;
70 #ifdef weak_alias
71 weak_alias (_IO_vasprintf, vasprintf)
72 #endif