xref: /openbsd/gnu/lib/libiberty/src/snprintf.c (revision 20fce977)
132d62142Sespie /* Implement the snprintf function.
232d62142Sespie    Copyright (C) 2003 Free Software Foundation, Inc.
332d62142Sespie    Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
432d62142Sespie 
532d62142Sespie This file is part of the libiberty library.  This library is free
632d62142Sespie software; you can redistribute it and/or modify it under the
732d62142Sespie terms of the GNU General Public License as published by the
832d62142Sespie Free Software Foundation; either version 2, or (at your option)
932d62142Sespie any later version.
1032d62142Sespie 
1132d62142Sespie This library is distributed in the hope that it will be useful,
1232d62142Sespie but WITHOUT ANY WARRANTY; without even the implied warranty of
1332d62142Sespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1432d62142Sespie GNU General Public License for more details.
1532d62142Sespie 
1632d62142Sespie You should have received a copy of the GNU General Public License
1732d62142Sespie along with GNU CC; see the file COPYING.  If not, write to
18*20fce977Smiod the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
1932d62142Sespie 
2032d62142Sespie As a special exception, if you link this library with files
2132d62142Sespie compiled with a GNU compiler to produce an executable, this does not cause
2232d62142Sespie the resulting executable to be covered by the GNU General Public License.
2332d62142Sespie This exception does not however invalidate any other reasons why
2432d62142Sespie the executable file might be covered by the GNU General Public License. */
2532d62142Sespie 
2632d62142Sespie /*
2732d62142Sespie 
2832d62142Sespie @deftypefn Supplemental int snprintf (char *@var{buf}, size_t @var{n}, const char *@var{format}, ...)
2932d62142Sespie 
3032d62142Sespie This function is similar to sprintf, but it will print at most @var{n}
3132d62142Sespie characters.  On error the return value is -1, otherwise it returns the
3232d62142Sespie number of characters that would have been printed had @var{n} been
3332d62142Sespie sufficiently large, regardless of the actual value of @var{n}.  Note
3432d62142Sespie some pre-C99 system libraries do not implement this correctly so users
3532d62142Sespie cannot generally rely on the return value if the system version of
3632d62142Sespie this function is used.
3732d62142Sespie 
3832d62142Sespie @end deftypefn
3932d62142Sespie 
4032d62142Sespie */
4132d62142Sespie 
4232d62142Sespie #include "ansidecl.h"
4332d62142Sespie 
4432d62142Sespie #include <stdarg.h>
4532d62142Sespie #include <stddef.h>
4632d62142Sespie 
47*20fce977Smiod int vsnprintf (char *, size_t, const char *, va_list);
4832d62142Sespie 
4932d62142Sespie int
snprintf(char * s,size_t n,const char * format,...)50*20fce977Smiod snprintf (char *s, size_t n, const char *format, ...)
5132d62142Sespie {
5232d62142Sespie   int result;
5332d62142Sespie   VA_OPEN (ap, format);
5432d62142Sespie   VA_FIXEDARG (ap, char *, s);
5532d62142Sespie   VA_FIXEDARG (ap, size_t, n);
5632d62142Sespie   VA_FIXEDARG (ap, const char *, format);
5732d62142Sespie   result = vsnprintf (s, n, format, ap);
5832d62142Sespie   VA_CLOSE (ap);
5932d62142Sespie   return result;
6032d62142Sespie }
61