Update.
[glibc.git] / libio / vasprintf.c
blobd1bf67132da9efde25898ae67ba65c1427f320fd
1 /* Copyright (C) 1995, 1997, 1999, 2000 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 <string.h>
28 #include "libioP.h"
29 #include "stdio.h"
30 #include "strfile.h"
32 int
33 _IO_vasprintf (result_ptr, format, args)
34 char **result_ptr;
35 const char *format;
36 _IO_va_list args;
38 /* Initial size of the buffer to be used. Will be doubled each time an
39 overflow occurs. */
40 const _IO_size_t init_string_size = 100;
41 char *string;
42 _IO_strfile sf;
43 #ifdef _IO_MTSAFE_IO
44 _IO_lock_t lock;
45 #endif
46 int ret;
47 _IO_size_t needed;
48 _IO_size_t allocated;
49 string = (char *) malloc (init_string_size);
50 if (string == NULL)
51 return -1;
52 #ifdef _IO_MTSAFE_IO
53 sf._sbf._f._lock = &lock;
54 #endif
55 _IO_no_init ((_IO_FILE *) &sf._sbf, 0, -1, NULL, NULL);
56 _IO_JUMPS ((struct _IO_FILE_plus *) &sf._sbf) = &_IO_str_jumps;
57 _IO_str_init_static (&sf, string, init_string_size, string);
58 sf._sbf._f._flags &= ~_IO_USER_BUF;
59 sf._s._allocate_buffer = (_IO_alloc_type) malloc;
60 sf._s._free_buffer = (_IO_free_type) free;
61 ret = _IO_vfprintf ((_IO_FILE *) &sf, format, args);
62 if (ret < 0)
63 return ret;
64 /* Only use realloc if the size we need is of the same order of
65 magnitude then the memory we allocated. */
66 needed = sf._sbf._f._IO_write_ptr - sf._sbf._f._IO_write_base + 1;
67 allocated = sf._sbf._f._IO_write_end - sf._sbf._f._IO_write_base;
68 if ((allocated << 1) <= needed)
69 *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed);
70 else
72 *result_ptr = (char *) malloc (needed);
73 if (*result_ptr != NULL)
75 memcpy (*result_ptr, sf._sbf._f._IO_buf_base, needed);
76 free (sf._sbf._f._IO_buf_base);
78 else
79 /* We have no choice, use the buffer we already have. */
80 *result_ptr = (char *) realloc (sf._sbf._f._IO_buf_base, needed);
82 if (*result_ptr == NULL)
83 *result_ptr = sf._sbf._f._IO_buf_base;
84 (*result_ptr)[sf._sbf._f._IO_write_ptr-sf._sbf._f._IO_write_base] = '\0';
85 return ret;
88 #ifdef weak_alias
89 weak_alias (_IO_vasprintf, vasprintf)
90 #endif