Back to index

glibc  2.9
vsnprintf.c
Go to the documentation of this file.
00001 /* Copyright (C) 1994,1997,1999-2003, 2004, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.
00018 
00019    As a special exception, if you link the code in this file with
00020    files compiled with a GNU compiler to produce an executable,
00021    that does not cause the resulting executable to be covered by
00022    the GNU Lesser General Public License.  This exception does not
00023    however invalidate any other reasons why the executable file
00024    might be covered by the GNU Lesser General Public License.
00025    This exception applies to code released by its copyright holders
00026    in files containing the exception.  */
00027 
00028 #include "libioP.h"
00029 #include "strfile.h"
00030 
00031 static int _IO_strn_overflow (_IO_FILE *fp, int c) __THROW;
00032 
00033 static int
00034 _IO_strn_overflow (fp, c)
00035      _IO_FILE *fp;
00036      int c;
00037 {
00038   /* When we come to here this means the user supplied buffer is
00039      filled.  But since we must return the number of characters which
00040      would have been written in total we must provide a buffer for
00041      further use.  We can do this by writing on and on in the overflow
00042      buffer in the _IO_strnfile structure.  */
00043   _IO_strnfile *snf = (_IO_strnfile *) fp;
00044 
00045   if (fp->_IO_buf_base != snf->overflow_buf)
00046     {
00047       /* Terminate the string.  We know that there is room for at
00048         least one more character since we initialized the stream with
00049         a size to make this possible.  */
00050       *fp->_IO_write_ptr = '\0';
00051 
00052       INTUSE(_IO_setb) (fp, snf->overflow_buf,
00053                      snf->overflow_buf + sizeof (snf->overflow_buf), 0);
00054 
00055       fp->_IO_write_base = snf->overflow_buf;
00056       fp->_IO_read_base = snf->overflow_buf;
00057       fp->_IO_read_ptr = snf->overflow_buf;
00058       fp->_IO_read_end = snf->overflow_buf + sizeof (snf->overflow_buf);
00059     }
00060 
00061   fp->_IO_write_ptr = snf->overflow_buf;
00062   fp->_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_strn_jumps attribute_hidden =
00071 {
00072   JUMP_INIT_DUMMY,
00073   JUMP_INIT(finish, _IO_str_finish),
00074   JUMP_INIT(overflow, _IO_strn_overflow),
00075   JUMP_INIT(underflow, INTUSE(_IO_str_underflow)),
00076   JUMP_INIT(uflow, INTUSE(_IO_default_uflow)),
00077   JUMP_INIT(pbackfail, INTUSE(_IO_str_pbackfail)),
00078   JUMP_INIT(xsputn, INTUSE(_IO_default_xsputn)),
00079   JUMP_INIT(xsgetn, INTUSE(_IO_default_xsgetn)),
00080   JUMP_INIT(seekoff, INTUSE(_IO_str_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_default_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_vsnprintf (string, maxlen, format, args)
00097      char *string;
00098      _IO_size_t maxlen;
00099      const char *format;
00100      _IO_va_list args;
00101 {
00102   _IO_strnfile sf;
00103   int ret;
00104 #ifdef _IO_MTSAFE_IO
00105   sf.f._sbf._f._lock = NULL;
00106 #endif
00107 
00108   /* We need to handle the special case where MAXLEN is 0.  Use the
00109      overflow buffer right from the start.  */
00110   if (maxlen == 0)
00111     {
00112       string = sf.overflow_buf;
00113       maxlen = sizeof (sf.overflow_buf);
00114     }
00115 
00116   _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
00117   _IO_JUMPS ((struct _IO_FILE_plus *) &sf.f._sbf) = &_IO_strn_jumps;
00118   string[0] = '\0';
00119   _IO_str_init_static_internal (&sf.f, string, maxlen - 1, string);
00120   ret = INTUSE(_IO_vfprintf) ((_IO_FILE *) &sf.f._sbf, format, args);
00121 
00122   if (sf.f._sbf._f._IO_buf_base != sf.overflow_buf)
00123     *sf.f._sbf._f._IO_write_ptr = '\0';
00124   return ret;
00125 }
00126 ldbl_weak_alias (_IO_vsnprintf, __vsnprintf)
00127 ldbl_weak_alias (_IO_vsnprintf, vsnprintf)