12a6b7db3Sskrll /* Implement the snprintf function.
2*f22f0ef4Schristos Copyright (C) 2003-2022 Free Software Foundation, Inc.
32a6b7db3Sskrll Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
42a6b7db3Sskrll
52a6b7db3Sskrll This file is part of the libiberty library. This library is free
62a6b7db3Sskrll software; you can redistribute it and/or modify it under the
72a6b7db3Sskrll terms of the GNU General Public License as published by the
82a6b7db3Sskrll Free Software Foundation; either version 2, or (at your option)
92a6b7db3Sskrll any later version.
102a6b7db3Sskrll
112a6b7db3Sskrll This library is distributed in the hope that it will be useful,
122a6b7db3Sskrll but WITHOUT ANY WARRANTY; without even the implied warranty of
132a6b7db3Sskrll MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
142a6b7db3Sskrll GNU General Public License for more details.
152a6b7db3Sskrll
162a6b7db3Sskrll You should have received a copy of the GNU General Public License
172a6b7db3Sskrll along with GNU CC; see the file COPYING. If not, write to
182a6b7db3Sskrll the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
192a6b7db3Sskrll
202a6b7db3Sskrll As a special exception, if you link this library with files
212a6b7db3Sskrll compiled with a GNU compiler to produce an executable, this does not cause
222a6b7db3Sskrll the resulting executable to be covered by the GNU General Public License.
232a6b7db3Sskrll This exception does not however invalidate any other reasons why
242a6b7db3Sskrll the executable file might be covered by the GNU General Public License. */
252a6b7db3Sskrll
262a6b7db3Sskrll /*
272a6b7db3Sskrll
2805caefcfSchristos @deftypefn Supplemental int snprintf (char *@var{buf}, size_t @var{n}, @
2905caefcfSchristos const char *@var{format}, ...)
302a6b7db3Sskrll
31b3ac4aedSchristos This function is similar to @code{sprintf}, but it will write to
32b3ac4aedSchristos @var{buf} at most @code{@var{n}-1} bytes of text, followed by a
33b3ac4aedSchristos terminating null byte, for a total of @var{n} bytes.
34b3ac4aedSchristos On error the return value is -1, otherwise it returns the number of
35b3ac4aedSchristos bytes, not including the terminating null byte, that would have been
36b3ac4aedSchristos written had @var{n} been sufficiently large, regardless of the actual
37b3ac4aedSchristos value of @var{n}. Note some pre-C99 system libraries do not implement
38b3ac4aedSchristos this correctly so users cannot generally rely on the return value if
39b3ac4aedSchristos the system version of this function is used.
402a6b7db3Sskrll
412a6b7db3Sskrll @end deftypefn
422a6b7db3Sskrll
432a6b7db3Sskrll */
442a6b7db3Sskrll
452a6b7db3Sskrll #include "ansidecl.h"
462a6b7db3Sskrll
472a6b7db3Sskrll #include <stdarg.h>
482a6b7db3Sskrll #include <stddef.h>
492a6b7db3Sskrll
502a6b7db3Sskrll int vsnprintf (char *, size_t, const char *, va_list);
512a6b7db3Sskrll
522a6b7db3Sskrll int
snprintf(char * s,size_t n,const char * format,...)532a6b7db3Sskrll snprintf (char *s, size_t n, const char *format, ...)
542a6b7db3Sskrll {
552a6b7db3Sskrll int result;
565ba6b03cSchristos va_list ap;
575ba6b03cSchristos va_start (ap, format);
582a6b7db3Sskrll result = vsnprintf (s, n, format, ap);
595ba6b03cSchristos va_end (ap);
602a6b7db3Sskrll return result;
612a6b7db3Sskrll }
62