Update copyright notices with scripts/update-copyrights
[platform/upstream/glibc.git] / libio / vsnprintf.c
1 /* Copyright (C) 1994-2014 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <http://www.gnu.org/licenses/>.
17
18    As a special exception, if you link the code in this file with
19    files compiled with a GNU compiler to produce an executable,
20    that does not cause the resulting executable to be covered by
21    the GNU Lesser General Public License.  This exception does not
22    however invalidate any other reasons why the executable file
23    might be covered by the GNU Lesser General Public License.
24    This exception applies to code released by its copyright holders
25    in files containing the exception.  */
26
27 #include "libioP.h"
28 #include "strfile.h"
29
30 static int _IO_strn_overflow (_IO_FILE *fp, int c) __THROW;
31
32 static int
33 _IO_strn_overflow (fp, c)
34      _IO_FILE *fp;
35      int c;
36 {
37   /* When we come to here this means the user supplied buffer is
38      filled.  But since we must return the number of characters which
39      would have been written in total we must provide a buffer for
40      further use.  We can do this by writing on and on in the overflow
41      buffer in the _IO_strnfile structure.  */
42   _IO_strnfile *snf = (_IO_strnfile *) fp;
43
44   if (fp->_IO_buf_base != snf->overflow_buf)
45     {
46       /* Terminate the string.  We know that there is room for at
47          least one more character since we initialized the stream with
48          a size to make this possible.  */
49       *fp->_IO_write_ptr = '\0';
50
51       _IO_setb (fp, snf->overflow_buf,
52                 snf->overflow_buf + sizeof (snf->overflow_buf), 0);
53
54       fp->_IO_write_base = snf->overflow_buf;
55       fp->_IO_read_base = snf->overflow_buf;
56       fp->_IO_read_ptr = snf->overflow_buf;
57       fp->_IO_read_end = snf->overflow_buf + sizeof (snf->overflow_buf);
58     }
59
60   fp->_IO_write_ptr = snf->overflow_buf;
61   fp->_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_strn_jumps attribute_hidden =
70 {
71   JUMP_INIT_DUMMY,
72   JUMP_INIT(finish, _IO_str_finish),
73   JUMP_INIT(overflow, _IO_strn_overflow),
74   JUMP_INIT(underflow, _IO_str_underflow),
75   JUMP_INIT(uflow, _IO_default_uflow),
76   JUMP_INIT(pbackfail, _IO_str_pbackfail),
77   JUMP_INIT(xsputn, _IO_default_xsputn),
78   JUMP_INIT(xsgetn, _IO_default_xsgetn),
79   JUMP_INIT(seekoff, _IO_str_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, _IO_default_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_vsnprintf (string, maxlen, format, args)
96      char *string;
97      _IO_size_t maxlen;
98      const char *format;
99      _IO_va_list args;
100 {
101   _IO_strnfile sf;
102   int ret;
103 #ifdef _IO_MTSAFE_IO
104   sf.f._sbf._f._lock = NULL;
105 #endif
106
107   /* We need to handle the special case where MAXLEN is 0.  Use the
108      overflow buffer right from the start.  */
109   if (maxlen == 0)
110     {
111       string = sf.overflow_buf;
112       maxlen = sizeof (sf.overflow_buf);
113     }
114
115   _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
116   _IO_JUMPS (&sf.f._sbf) = &_IO_strn_jumps;
117   string[0] = '\0';
118   _IO_str_init_static_internal (&sf.f, string, maxlen - 1, string);
119   ret = _IO_vfprintf (&sf.f._sbf._f, format, args);
120
121   if (sf.f._sbf._f._IO_buf_base != sf.overflow_buf)
122     *sf.f._sbf._f._IO_write_ptr = '\0';
123   return ret;
124 }
125 ldbl_weak_alias (_IO_vsnprintf, __vsnprintf)
126 ldbl_weak_alias (_IO_vsnprintf, vsnprintf)