Add tests for recent getopt changes.
[glibc.git] / libio / memstream.c
blob877383f3a80d738121008ab0ac448a84e823920b
1 /* Copyright (C) 1995-97,99,2000,2002-2004,2006 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include "libioP.h"
20 #include "strfile.h"
21 #include <stdio.h>
22 #include <stdlib.h>
25 struct _IO_FILE_memstream
27 _IO_strfile _sf;
28 char **bufloc;
29 _IO_size_t *sizeloc;
33 static int _IO_mem_sync (_IO_FILE* fp) __THROW;
34 static void _IO_mem_finish (_IO_FILE* fp, int) __THROW;
37 static const struct _IO_jump_t _IO_mem_jumps =
39 JUMP_INIT_DUMMY,
40 JUMP_INIT (finish, _IO_mem_finish),
41 JUMP_INIT (overflow, INTUSE(_IO_str_overflow)),
42 JUMP_INIT (underflow, INTUSE(_IO_str_underflow)),
43 JUMP_INIT (uflow, INTUSE(_IO_default_uflow)),
44 JUMP_INIT (pbackfail, INTUSE(_IO_str_pbackfail)),
45 JUMP_INIT (xsputn, INTUSE(_IO_default_xsputn)),
46 JUMP_INIT (xsgetn, INTUSE(_IO_default_xsgetn)),
47 JUMP_INIT (seekoff, INTUSE(_IO_str_seekoff)),
48 JUMP_INIT (seekpos, _IO_default_seekpos),
49 JUMP_INIT (setbuf, _IO_default_setbuf),
50 JUMP_INIT (sync, _IO_mem_sync),
51 JUMP_INIT (doallocate, INTUSE(_IO_default_doallocate)),
52 JUMP_INIT (read, _IO_default_read),
53 JUMP_INIT (write, _IO_default_write),
54 JUMP_INIT (seek, _IO_default_seek),
55 JUMP_INIT (close, _IO_default_close),
56 JUMP_INIT (stat, _IO_default_stat),
57 JUMP_INIT(showmanyc, _IO_default_showmanyc),
58 JUMP_INIT(imbue, _IO_default_imbue)
61 /* Open a stream that writes into a malloc'd buffer that is expanded as
62 necessary. *BUFLOC and *SIZELOC are updated with the buffer's location
63 and the number of characters written on fflush or fclose. */
64 _IO_FILE *
65 open_memstream (bufloc, sizeloc)
66 char **bufloc;
67 _IO_size_t *sizeloc;
69 struct locked_FILE
71 struct _IO_FILE_memstream fp;
72 #ifdef _IO_MTSAFE_IO
73 _IO_lock_t lock;
74 #endif
75 struct _IO_wide_data wd;
76 } *new_f;
77 char *buf;
79 new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
80 if (new_f == NULL)
81 return NULL;
82 #ifdef _IO_MTSAFE_IO
83 new_f->fp._sf._sbf._f._lock = &new_f->lock;
84 #endif
86 buf = calloc (1, _IO_BUFSIZ);
87 if (buf == NULL)
88 return NULL;
89 INTUSE(_IO_init) (&new_f->fp._sf._sbf._f, 0);
90 _IO_JUMPS ((struct _IO_FILE_plus *) &new_f->fp._sf._sbf) = &_IO_mem_jumps;
91 _IO_str_init_static_internal (&new_f->fp._sf, buf, _IO_BUFSIZ, buf);
92 new_f->fp._sf._sbf._f._flags &= ~_IO_USER_BUF;
93 new_f->fp._sf._s._allocate_buffer = (_IO_alloc_type) malloc;
94 new_f->fp._sf._s._free_buffer = (_IO_free_type) free;
96 new_f->fp.bufloc = bufloc;
97 new_f->fp.sizeloc = sizeloc;
99 return (_IO_FILE *) &new_f->fp._sf._sbf;
101 libc_hidden_def (open_memstream)
104 static int
105 _IO_mem_sync (fp)
106 _IO_FILE* fp;
108 struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
110 if (fp->_IO_write_ptr == fp->_IO_write_end)
112 INTUSE(_IO_str_overflow) (fp, '\0');
113 --fp->_IO_write_ptr;
115 else
116 *fp->_IO_write_ptr = '\0';
118 *mp->bufloc = fp->_IO_write_base;
119 *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
121 return 0;
125 static void
126 _IO_mem_finish (fp, dummy)
127 _IO_FILE* fp;
128 int dummy;
130 struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
132 *mp->bufloc = (char *) realloc (fp->_IO_write_base,
133 fp->_IO_write_ptr - fp->_IO_write_base + 1);
134 if (*mp->bufloc != NULL)
136 (*mp->bufloc)[fp->_IO_write_ptr - fp->_IO_write_base] = '\0';
137 *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
139 fp->_IO_buf_base = NULL;
142 _IO_str_finish (fp, 0);