Update.
[glibc.git] / libio / vsnprintf.c
blob5ada74215cd8504b2b7ca5197b9a5c0bd71cd856
1 /*
2 Copyright (C) 1994, 1997 Free Software Foundation, Inc.
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 this library; see the file COPYING. If not, write to the Free
17 Software Foundation, 59 Temple Place - Suite 330, Boston, 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 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 "libioP.h"
26 #include "strfile.h"
29 typedef struct
31 _IO_strfile f;
32 /* This is used for the characters which do not fit in the buffer
33 provided by the user. */
34 char overflow_buf[64];
35 } _IO_strnfile;
38 static int
39 _IO_strn_overflow (_IO_FILE* fp, int c)
41 /* When we come to here this means the user supplied buffer is
42 filled. But since we must return the number of characters which
43 would have been written in total we must provide a buffer for
44 further use. We can do this by writing on and on in the overflow
45 buffer in the _IO_strnfile structure. */
46 _IO_strnfile *snf = (_IO_strnfile *) fp;
48 if (fp->_IO_buf_base != snf->overflow_buf)
50 /* Terminate the string. We know that there is room for at
51 least one more character since we initialized the stream with
52 a size to make this possible. */
53 *fp->_IO_write_ptr = '\0';
55 _IO_setb (fp, snf->overflow_buf,
56 snf->overflow_buf + sizeof (snf->overflow_buf), 0);
58 fp->_IO_write_base = snf->overflow_buf;
59 fp->_IO_read_base = snf->overflow_buf;
60 fp->_IO_read_ptr = snf->overflow_buf;
61 fp->_IO_read_end = snf->overflow_buf + sizeof (snf->overflow_buf);
64 fp->_IO_write_ptr = snf->overflow_buf;
65 fp->_IO_write_end = snf->overflow_buf;
67 /* Since we are not really interested in storing the characters
68 which do not fit in the buffer we simply ignore it. */
69 return c;
73 static struct _IO_jump_t _IO_strn_jumps = {
74 JUMP_INIT_DUMMY,
75 JUMP_INIT(finish, _IO_str_finish),
76 JUMP_INIT(overflow, _IO_strn_overflow),
77 JUMP_INIT(underflow, _IO_str_underflow),
78 JUMP_INIT(uflow, _IO_default_uflow),
79 JUMP_INIT(pbackfail, _IO_str_pbackfail),
80 JUMP_INIT(xsputn, _IO_default_xsputn),
81 JUMP_INIT(xsgetn, _IO_default_xsgetn),
82 JUMP_INIT(seekoff, _IO_str_seekoff),
83 JUMP_INIT(seekpos, _IO_default_seekpos),
84 JUMP_INIT(setbuf, _IO_default_setbuf),
85 JUMP_INIT(sync, _IO_default_sync),
86 JUMP_INIT(doallocate, _IO_default_doallocate),
87 JUMP_INIT(read, _IO_default_read),
88 JUMP_INIT(write, _IO_default_write),
89 JUMP_INIT(seek, _IO_default_seek),
90 JUMP_INIT(close, _IO_default_close),
91 JUMP_INIT(stat, _IO_default_stat)
95 int
96 _IO_vsnprintf (string, maxlen, format, args)
97 char *string;
98 _IO_size_t maxlen;
99 const char *format;
100 _IO_va_list args;
102 _IO_strnfile sf;
103 int ret;
104 #ifdef _IO_MTSAFE_IO
105 _IO_lock_t lock;
106 sf.f._sbf._f._lock = &lock;
107 #endif
109 /* We need to handle the special case where MAXLEN is 0. Use the
110 overflow buffer right from the start. */
111 if (maxlen == 0)
113 string = sf.overflow_buf;
114 maxlen = sizeof (sf.overflow_buf);
117 _IO_init ((_IO_FILE *) &sf, 0);
118 _IO_JUMPS ((_IO_FILE *) &sf) = &_IO_strn_jumps;
119 _IO_str_init_static ((_IO_FILE *) &sf, string, maxlen - 1, string);
120 ret = _IO_vfprintf ((_IO_FILE *) &sf, format, args);
122 if (sf.f._sbf._f._IO_buf_base != sf.overflow_buf)
123 *sf.f._sbf._f._IO_write_ptr = '\0';
124 return ret;
126 weak_alias (_IO_vsnprintf, __vsnprintf)
127 weak_alias (_IO_vsnprintf, vsnprintf)