1 /* Copyright (C) 1994, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU IO Library.
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.
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.
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,
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. */
33 /* This is used for the characters which do not fit in the buffer
34 provided by the user. */
35 char overflow_buf
[64];
39 static int _IO_strn_overflow
__P ((_IO_FILE
*fp
, int c
));
42 _IO_strn_overflow (fp
, c
)
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
;
53 if (fp
->_IO_buf_base
!= snf
->overflow_buf
)
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';
60 _IO_setb (fp
, snf
->overflow_buf
,
61 snf
->overflow_buf
+ sizeof (snf
->overflow_buf
), 0);
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
);
69 fp
->_IO_write_ptr
= snf
->overflow_buf
;
70 fp
->_IO_write_end
= snf
->overflow_buf
;
72 /* Since we are not really interested in storing the characters
73 which do not fit in the buffer we simply ignore it. */
78 static struct _IO_jump_t _IO_strn_jumps
=
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
)
102 _IO_vsnprintf (string
, maxlen
, format
, args
)
112 sf
.f
._sbf
._f
._lock
= &lock
;
115 /* We need to handle the special case where MAXLEN is 0. Use the
116 overflow buffer right from the start. */
119 string
= sf
.overflow_buf
;
120 maxlen
= sizeof (sf
.overflow_buf
);
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
);
128 if (sf
.f
._sbf
._f
._IO_buf_base
!= sf
.overflow_buf
)
129 *sf
.f
._sbf
._f
._IO_write_ptr
= '\0';
134 weak_alias (_IO_vsnprintf
, __vsnprintf
)
135 weak_alias (_IO_vsnprintf
, vsnprintf
)