Update.
[glibc.git] / libio / vswprintf.c
blob0b4a4585681a26cf123a0cd97cede35df17b8127
1 /* Copyright (C) 1994, 1997, 1999 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 wchar_t overflow_buf[64];
36 } _IO_strnfile;
39 static wint_t _IO_wstrn_overflow __P ((_IO_FILE *fp, wint_t c));
41 static wint_t
42 _IO_wstrn_overflow (fp, c)
43 _IO_FILE *fp;
44 wint_t 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->_wide_data->_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->_wide_data->_IO_write_ptr = '\0';
60 _IO_wsetb (fp, snf->overflow_buf,
61 snf->overflow_buf + (sizeof (snf->overflow_buf)
62 / sizeof (wchar_t)), 0);
64 fp->_wide_data->_IO_write_base = snf->overflow_buf;
65 fp->_wide_data->_IO_read_base = snf->overflow_buf;
66 fp->_wide_data->_IO_read_ptr = snf->overflow_buf;
67 fp->_wide_data->_IO_read_end = (snf->overflow_buf
68 + (sizeof (snf->overflow_buf)
69 / sizeof (wchar_t)));
72 fp->_wide_data->_IO_write_ptr = snf->overflow_buf;
73 fp->_wide_data->_IO_write_end = snf->overflow_buf;
75 /* Since we are not really interested in storing the characters
76 which do not fit in the buffer we simply ignore it. */
77 return c;
81 static struct _IO_jump_t _IO_wstrn_jumps =
83 JUMP_INIT_DUMMY,
84 JUMP_INIT(finish, _IO_wstr_finish),
85 JUMP_INIT(overflow, (_IO_overflow_t) _IO_wstrn_overflow),
86 JUMP_INIT(underflow, (_IO_underflow_t) _IO_wstr_underflow),
87 JUMP_INIT(uflow, (_IO_underflow_t) _IO_wdefault_uflow),
88 JUMP_INIT(pbackfail, (_IO_pbackfail_t) _IO_wstr_pbackfail),
89 JUMP_INIT(xsputn, _IO_wdefault_xsputn),
90 JUMP_INIT(xsgetn, _IO_wdefault_xsgetn),
91 JUMP_INIT(seekoff, _IO_wstr_seekoff),
92 JUMP_INIT(seekpos, _IO_default_seekpos),
93 JUMP_INIT(setbuf, (_IO_setbuf_t) _IO_wdefault_setbuf),
94 JUMP_INIT(sync, _IO_default_sync),
95 JUMP_INIT(doallocate, _IO_wdefault_doallocate),
96 JUMP_INIT(read, _IO_default_read),
97 JUMP_INIT(write, _IO_default_write),
98 JUMP_INIT(seek, _IO_default_seek),
99 JUMP_INIT(close, _IO_default_close),
100 JUMP_INIT(stat, _IO_default_stat),
101 JUMP_INIT(showmanyc, _IO_default_showmanyc),
102 JUMP_INIT(imbue, _IO_default_imbue)
107 _IO_vswprintf (string, maxlen, format, args)
108 wchar_t *string;
109 _IO_size_t maxlen;
110 const wchar_t *format;
111 _IO_va_list args;
113 _IO_strnfile sf;
114 int ret;
115 struct _IO_wide_data wd;
116 #ifdef _IO_MTSAFE_IO
117 _IO_lock_t lock;
118 sf.f._sbf._f._lock = &lock;
119 #endif
121 /* We need to handle the special case where MAXLEN is 0. Use the
122 overflow buffer right from the start. */
123 if (maxlen == 0)
125 string = sf.overflow_buf;
126 maxlen = sizeof (sf.overflow_buf) / sizeof (wchar_t);
129 _IO_no_init (&sf.f._sbf._f, 0, 0, &wd, &_IO_wstrn_jumps);
130 _IO_fwide (&sf.f._sbf._f, 1);
131 string[0] = L'\0';
132 _IO_wstr_init_static (&sf.f._sbf._f, string, maxlen - 1, string);
133 ret = _IO_vfwprintf (&sf.f._sbf._f, format, args);
135 if (sf.f._sbf._f._wide_data->_IO_buf_base != sf.overflow_buf)
136 *sf.f._sbf._f._wide_data->_IO_write_ptr = '\0';
137 return ret;
140 #ifdef weak_alias
141 weak_alias (_IO_vswprintf, __vswprintf)
142 weak_alias (_IO_vswprintf, vswprintf)
143 #endif