Avoid dup3 PLT usage.
[platform/upstream/glibc.git] / libio / vswprintf.c
1 /* Copyright (C) 1994, 1997, 1999-2002, 2004, 2005, 2006
2    Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C 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 GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.
18
19    As a special exception, if you link the code in this file with
20    files compiled with a GNU compiler to produce an executable,
21    that does not cause the resulting executable to be covered by
22    the GNU Lesser General Public License.  This exception does not
23    however invalidate any other reasons why the executable file
24    might be covered by the GNU Lesser General Public License.
25    This exception applies to code released by its copyright holders
26    in files containing the exception.  */
27
28 #include "libioP.h"
29 #include "strfile.h"
30
31
32 static wint_t _IO_wstrn_overflow (_IO_FILE *fp, wint_t c) __THROW;
33
34 static wint_t
35 _IO_wstrn_overflow (fp, c)
36      _IO_FILE *fp;
37      wint_t c;
38 {
39   /* When we come to here this means the user supplied buffer is
40      filled.  But since we must return the number of characters which
41      would have been written in total we must provide a buffer for
42      further use.  We can do this by writing on and on in the overflow
43      buffer in the _IO_wstrnfile structure.  */
44   _IO_wstrnfile *snf = (_IO_wstrnfile *) fp;
45
46   if (fp->_wide_data->_IO_buf_base != snf->overflow_buf)
47     {
48       INTUSE(_IO_wsetb) (fp, snf->overflow_buf,
49                          snf->overflow_buf + (sizeof (snf->overflow_buf)
50                                               / sizeof (wchar_t)), 0);
51
52       fp->_wide_data->_IO_write_base = snf->overflow_buf;
53       fp->_wide_data->_IO_read_base = snf->overflow_buf;
54       fp->_wide_data->_IO_read_ptr = snf->overflow_buf;
55       fp->_wide_data->_IO_read_end = (snf->overflow_buf
56                                       + (sizeof (snf->overflow_buf)
57                                          / sizeof (wchar_t)));
58     }
59
60   fp->_wide_data->_IO_write_ptr = snf->overflow_buf;
61   fp->_wide_data->_IO_write_end = snf->overflow_buf;
62
63   /* Since we are not really interested in storing the characters
64      which do not fit in the buffer we simply ignore it.  */
65   return c;
66 }
67
68
69 const struct _IO_jump_t _IO_wstrn_jumps attribute_hidden =
70 {
71   JUMP_INIT_DUMMY,
72   JUMP_INIT(finish, _IO_wstr_finish),
73   JUMP_INIT(overflow, (_IO_overflow_t) _IO_wstrn_overflow),
74   JUMP_INIT(underflow, (_IO_underflow_t) _IO_wstr_underflow),
75   JUMP_INIT(uflow, (_IO_underflow_t) INTUSE(_IO_wdefault_uflow)),
76   JUMP_INIT(pbackfail, (_IO_pbackfail_t) _IO_wstr_pbackfail),
77   JUMP_INIT(xsputn, INTUSE(_IO_wdefault_xsputn)),
78   JUMP_INIT(xsgetn, INTUSE(_IO_wdefault_xsgetn)),
79   JUMP_INIT(seekoff, _IO_wstr_seekoff),
80   JUMP_INIT(seekpos, _IO_default_seekpos),
81   JUMP_INIT(setbuf, _IO_default_setbuf),
82   JUMP_INIT(sync, _IO_default_sync),
83   JUMP_INIT(doallocate, INTUSE(_IO_wdefault_doallocate)),
84   JUMP_INIT(read, _IO_default_read),
85   JUMP_INIT(write, _IO_default_write),
86   JUMP_INIT(seek, _IO_default_seek),
87   JUMP_INIT(close, _IO_default_close),
88   JUMP_INIT(stat, _IO_default_stat),
89   JUMP_INIT(showmanyc, _IO_default_showmanyc),
90   JUMP_INIT(imbue, _IO_default_imbue)
91 };
92
93
94 int
95 _IO_vswprintf (string, maxlen, format, args)
96      wchar_t *string;
97      _IO_size_t maxlen;
98      const wchar_t *format;
99      _IO_va_list args;
100 {
101   _IO_wstrnfile sf;
102   int ret;
103   struct _IO_wide_data wd;
104 #ifdef _IO_MTSAFE_IO
105   sf.f._sbf._f._lock = NULL;
106 #endif
107
108   if (maxlen == 0)
109     /* Since we have to write at least the terminating L'\0' a buffer
110        length of zero always makes the function fail.  */
111     return -1;
112
113   _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, 0, &wd, &_IO_wstrn_jumps);
114   _IO_fwide (&sf.f._sbf._f, 1);
115   string[0] = L'\0';
116   _IO_wstr_init_static (&sf.f._sbf._f, string, maxlen - 1, string);
117   ret = _IO_vfwprintf ((_IO_FILE *) &sf.f._sbf, format, args);
118
119   if (sf.f._sbf._f._wide_data->_IO_buf_base == sf.overflow_buf)
120     /* ISO C99 requires swprintf/vswprintf to return an error if the
121        output does not fit in the provided buffer.  */
122     return -1;
123
124   /* Terminate the string.  */
125   *sf.f._sbf._f._wide_data->_IO_write_ptr = '\0';
126
127   return ret;
128 }
129 weak_alias (_IO_vswprintf, __vswprintf)
130 ldbl_weak_alias (_IO_vswprintf, vswprintf)