1/* Copyright (C) 1994, 1997 Free Software Foundation, Inc.
2   This file is part of the GNU IO Library.
3
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.
8
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.
13
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.
18
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.  */
25
26#include "libioP.h"
27#include "strfile.h"
28
29
30typedef struct
31{
32  _IO_strfile f;
33  /* This is used for the characters which do not fit in the buffer
34     provided by the user.  */
35  char overflow_buf[64];
36} _IO_strnfile;
37
38
39static int _IO_strn_overflow __P ((_IO_FILE *fp, int c));
40
41static int
42_IO_strn_overflow (fp, c)
43     _IO_FILE *fp;
44     int c;
45{
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;
52
53  if (fp->_IO_buf_base != snf->overflow_buf)
54    {
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->_IO_write_ptr = '\0';
59
60      _IO_setb (fp, snf->overflow_buf,
61		snf->overflow_buf + sizeof (snf->overflow_buf), 0);
62
63      fp->_IO_write_base = snf->overflow_buf;
64      fp->_IO_read_base = snf->overflow_buf;
65      fp->_IO_read_ptr = snf->overflow_buf;
66      fp->_IO_read_end = snf->overflow_buf + sizeof (snf->overflow_buf);
67    }
68
69  fp->_IO_write_ptr = snf->overflow_buf;
70  fp->_IO_write_end = snf->overflow_buf;
71
72  /* Since we are not really interested in storing the characters
73     which do not fit in the buffer we simply ignore it.  */
74  return c;
75}
76
77
78static struct _IO_jump_t _IO_strn_jumps =
79{
80  JUMP_INIT_DUMMY,
81  JUMP_INIT(finish, _IO_str_finish),
82  JUMP_INIT(overflow, _IO_strn_overflow),
83  JUMP_INIT(underflow, _IO_str_underflow),
84  JUMP_INIT(uflow, _IO_default_uflow),
85  JUMP_INIT(pbackfail, _IO_str_pbackfail),
86  JUMP_INIT(xsputn, _IO_default_xsputn),
87  JUMP_INIT(xsgetn, _IO_default_xsgetn),
88  JUMP_INIT(seekoff, _IO_str_seekoff),
89  JUMP_INIT(seekpos, _IO_default_seekpos),
90  JUMP_INIT(setbuf, _IO_default_setbuf),
91  JUMP_INIT(sync, _IO_default_sync),
92  JUMP_INIT(doallocate, _IO_default_doallocate),
93  JUMP_INIT(read, _IO_default_read),
94  JUMP_INIT(write, _IO_default_write),
95  JUMP_INIT(seek, _IO_default_seek),
96  JUMP_INIT(close, _IO_default_close),
97  JUMP_INIT(stat, _IO_default_stat)
98};
99
100
101int
102_IO_vsnprintf (string, maxlen, format, args)
103     char *string;
104     _IO_size_t maxlen;
105     const char *format;
106     _IO_va_list args;
107{
108  _IO_strnfile sf;
109  int ret;
110#ifdef _IO_MTSAFE_IO
111  _IO_lock_t lock;
112  sf.f._sbf._f._lock = &lock;
113#endif
114
115  /* We need to handle the special case where MAXLEN is 0.  Use the
116     overflow buffer right from the start.  */
117  if (maxlen == 0)
118    {
119      string = sf.overflow_buf;
120      maxlen = sizeof (sf.overflow_buf);
121    }
122
123  _IO_init ((_IO_FILE *) &sf, 0);
124  _IO_JUMPS ((_IO_FILE *) &sf) = &_IO_strn_jumps;
125  _IO_str_init_static ((_IO_FILE *) &sf, string, maxlen - 1, string);
126  ret = _IO_vfprintf ((_IO_FILE *) &sf, format, args);
127
128  if (sf.f._sbf._f._IO_buf_base != sf.overflow_buf)
129    *sf.f._sbf._f._IO_write_ptr = '\0';
130  return ret;
131}
132
133#ifdef weak_alias
134weak_alias (_IO_vsnprintf, __vsnprintf)
135weak_alias (_IO_vsnprintf, vsnprintf)
136#endif
137