Update.
[glibc.git] / libio / vsnprintf.c
blobe223e5dec2f3718149b0b9166a29ed9afd49ab40
1 /* Copyright (C) 1994, 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 "libioP.h"
27 #include "strfile.h"
30 typedef struct
32 _IO_strfile f;
33 /* This is used for the characters which do not fit in the buffer
34 provided by the user. */
35 char overflow_buf[64];
36 } _IO_strnfile;
39 static int _IO_strn_overflow __P ((_IO_FILE *fp, int c));
41 static int
42 _IO_strn_overflow (fp, c)
43 _IO_FILE *fp;
44 int c;
46 /* When we come to here this means the user supplied buffer is
47 filled. But since we must return the number of characters which
48 would have been written in total we must provide a buffer for
49 further use. We can do this by writing on and on in the overflow
50 buffer in the _IO_strnfile structure. */
51 _IO_strnfile *snf = (_IO_strnfile *) fp;
53 if (fp->_IO_buf_base != snf->overflow_buf)
55 /* Terminate the string. We know that there is room for at
56 least one more character since we initialized the stream with
57 a size to make this possible. */
58 *fp->_IO_write_ptr = '\0';
60 _IO_setb (fp, snf->overflow_buf,
61 snf->overflow_buf + sizeof (snf->overflow_buf), 0);
63 fp->_IO_write_base = snf->overflow_buf;
64 fp->_IO_read_base = snf->overflow_buf;
65 fp->_IO_read_ptr = snf->overflow_buf;
66 fp->_IO_read_end = snf->overflow_buf + sizeof (snf->overflow_buf);
69 fp->_IO_write_ptr = snf->overflow_buf;
70 fp->_IO_write_end = snf->overflow_buf;
72 /* Since we are not really interested in storing the characters
73 which do not fit in the buffer we simply ignore it. */
74 return c;
78 static struct _IO_jump_t _IO_strn_jumps =
80 JUMP_INIT_DUMMY,
81 JUMP_INIT(finish, _IO_str_finish),
82 JUMP_INIT(overflow, _IO_strn_overflow),
83 JUMP_INIT(underflow, _IO_str_underflow),
84 JUMP_INIT(uflow, _IO_default_uflow),
85 JUMP_INIT(pbackfail, _IO_str_pbackfail),
86 JUMP_INIT(xsputn, _IO_default_xsputn),
87 JUMP_INIT(xsgetn, _IO_default_xsgetn),
88 JUMP_INIT(seekoff, _IO_str_seekoff),
89 JUMP_INIT(seekpos, _IO_default_seekpos),
90 JUMP_INIT(setbuf, _IO_default_setbuf),
91 JUMP_INIT(sync, _IO_default_sync),
92 JUMP_INIT(doallocate, _IO_default_doallocate),
93 JUMP_INIT(read, _IO_default_read),
94 JUMP_INIT(write, _IO_default_write),
95 JUMP_INIT(seek, _IO_default_seek),
96 JUMP_INIT(close, _IO_default_close),
97 JUMP_INIT(stat, _IO_default_stat),
98 JUMP_INIT(showmanyc, _IO_default_showmanyc),
99 JUMP_INIT(imbue, _IO_default_imbue)
104 _IO_vsnprintf (string, maxlen, format, args)
105 char *string;
106 _IO_size_t maxlen;
107 const char *format;
108 _IO_va_list args;
110 _IO_strnfile sf;
111 int ret;
112 #ifdef _IO_MTSAFE_IO
113 _IO_lock_t lock;
114 sf.f._sbf._f._lock = &lock;
115 #endif
117 /* We need to handle the special case where MAXLEN is 0. Use the
118 overflow buffer right from the start. */
119 if (maxlen == 0)
121 string = sf.overflow_buf;
122 maxlen = sizeof (sf.overflow_buf);
125 _IO_init ((_IO_FILE *) &sf, 0);
126 _IO_JUMPS ((_IO_FILE *) &sf) = &_IO_strn_jumps;
127 string[0] = '\0';
128 _IO_str_init_static ((_IO_FILE *) &sf, string, maxlen - 1, string);
129 ret = _IO_vfprintf ((_IO_FILE *) &sf, format, args);
131 if (sf.f._sbf._f._IO_buf_base != sf.overflow_buf)
132 *sf.f._sbf._f._IO_write_ptr = '\0';
133 return ret;
136 #ifdef weak_alias
137 weak_alias (_IO_vsnprintf, __vsnprintf)
138 weak_alias (_IO_vsnprintf, vsnprintf)
139 #endif