Back to index

glibc  2.9
vswprintf.c
Go to the documentation of this file.
00001 /* Copyright (C) 1994, 1997, 1999-2002, 2004, 2005, 2006
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.
00019 
00020    As a special exception, if you link the code in this file with
00021    files compiled with a GNU compiler to produce an executable,
00022    that does not cause the resulting executable to be covered by
00023    the GNU Lesser General Public License.  This exception does not
00024    however invalidate any other reasons why the executable file
00025    might be covered by the GNU Lesser General Public License.
00026    This exception applies to code released by its copyright holders
00027    in files containing the exception.  */
00028 
00029 #include "libioP.h"
00030 #include "strfile.h"
00031 
00032 
00033 static wint_t _IO_wstrn_overflow (_IO_FILE *fp, wint_t c) __THROW;
00034 
00035 static wint_t
00036 _IO_wstrn_overflow (fp, c)
00037      _IO_FILE *fp;
00038      wint_t c;
00039 {
00040   /* When we come to here this means the user supplied buffer is
00041      filled.  But since we must return the number of characters which
00042      would have been written in total we must provide a buffer for
00043      further use.  We can do this by writing on and on in the overflow
00044      buffer in the _IO_wstrnfile structure.  */
00045   _IO_wstrnfile *snf = (_IO_wstrnfile *) fp;
00046 
00047   if (fp->_wide_data->_IO_buf_base != snf->overflow_buf)
00048     {
00049       INTUSE(_IO_wsetb) (fp, snf->overflow_buf,
00050                       snf->overflow_buf + (sizeof (snf->overflow_buf)
00051                                          / sizeof (wchar_t)), 0);
00052 
00053       fp->_wide_data->_IO_write_base = snf->overflow_buf;
00054       fp->_wide_data->_IO_read_base = snf->overflow_buf;
00055       fp->_wide_data->_IO_read_ptr = snf->overflow_buf;
00056       fp->_wide_data->_IO_read_end = (snf->overflow_buf
00057                                   + (sizeof (snf->overflow_buf)
00058                                     / sizeof (wchar_t)));
00059     }
00060 
00061   fp->_wide_data->_IO_write_ptr = snf->overflow_buf;
00062   fp->_wide_data->_IO_write_end = snf->overflow_buf;
00063 
00064   /* Since we are not really interested in storing the characters
00065      which do not fit in the buffer we simply ignore it.  */
00066   return c;
00067 }
00068 
00069 
00070 const struct _IO_jump_t _IO_wstrn_jumps attribute_hidden =
00071 {
00072   JUMP_INIT_DUMMY,
00073   JUMP_INIT(finish, _IO_wstr_finish),
00074   JUMP_INIT(overflow, (_IO_overflow_t) _IO_wstrn_overflow),
00075   JUMP_INIT(underflow, (_IO_underflow_t) _IO_wstr_underflow),
00076   JUMP_INIT(uflow, (_IO_underflow_t) INTUSE(_IO_wdefault_uflow)),
00077   JUMP_INIT(pbackfail, (_IO_pbackfail_t) _IO_wstr_pbackfail),
00078   JUMP_INIT(xsputn, INTUSE(_IO_wdefault_xsputn)),
00079   JUMP_INIT(xsgetn, INTUSE(_IO_wdefault_xsgetn)),
00080   JUMP_INIT(seekoff, _IO_wstr_seekoff),
00081   JUMP_INIT(seekpos, _IO_default_seekpos),
00082   JUMP_INIT(setbuf, _IO_default_setbuf),
00083   JUMP_INIT(sync, _IO_default_sync),
00084   JUMP_INIT(doallocate, INTUSE(_IO_wdefault_doallocate)),
00085   JUMP_INIT(read, _IO_default_read),
00086   JUMP_INIT(write, _IO_default_write),
00087   JUMP_INIT(seek, _IO_default_seek),
00088   JUMP_INIT(close, _IO_default_close),
00089   JUMP_INIT(stat, _IO_default_stat),
00090   JUMP_INIT(showmanyc, _IO_default_showmanyc),
00091   JUMP_INIT(imbue, _IO_default_imbue)
00092 };
00093 
00094 
00095 int
00096 _IO_vswprintf (string, maxlen, format, args)
00097      wchar_t *string;
00098      _IO_size_t maxlen;
00099      const wchar_t *format;
00100      _IO_va_list args;
00101 {
00102   _IO_wstrnfile sf;
00103   int ret;
00104   struct _IO_wide_data wd;
00105 #ifdef _IO_MTSAFE_IO
00106   sf.f._sbf._f._lock = NULL;
00107 #endif
00108 
00109   if (maxlen == 0)
00110     /* Since we have to write at least the terminating L'\0' a buffer
00111        length of zero always makes the function fail.  */
00112     return -1;
00113 
00114   _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, 0, &wd, &_IO_wstrn_jumps);
00115   _IO_fwide (&sf.f._sbf._f, 1);
00116   string[0] = L'\0';
00117   _IO_wstr_init_static (&sf.f._sbf._f, string, maxlen - 1, string);
00118   ret = _IO_vfwprintf ((_IO_FILE *) &sf.f._sbf, format, args);
00119 
00120   if (sf.f._sbf._f._wide_data->_IO_buf_base == sf.overflow_buf)
00121     /* ISO C99 requires swprintf/vswprintf to return an error if the
00122        output does not fit in the provided buffer.  */
00123     return -1;
00124 
00125   /* Terminate the string.  */
00126   *sf.f._sbf._f._wide_data->_IO_write_ptr = '\0';
00127 
00128   return ret;
00129 }
00130 weak_alias (_IO_vswprintf, __vswprintf)
00131 ldbl_weak_alias (_IO_vswprintf, vswprintf)