xref: /netbsd-src/external/gpl3/gdb/dist/libiberty/snprintf.c (revision 5173eb0a33e5d83890ba976253e703be4c92557c)
198b9484cSchristos /* Implement the snprintf function.
2*5173eb0aSchristos    Copyright (C) 2003-2024 Free Software Foundation, Inc.
398b9484cSchristos    Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
498b9484cSchristos 
598b9484cSchristos This file is part of the libiberty library.  This library is free
698b9484cSchristos software; you can redistribute it and/or modify it under the
798b9484cSchristos terms of the GNU General Public License as published by the
898b9484cSchristos Free Software Foundation; either version 2, or (at your option)
998b9484cSchristos any later version.
1098b9484cSchristos 
1198b9484cSchristos This library is distributed in the hope that it will be useful,
1298b9484cSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1398b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1498b9484cSchristos GNU General Public License for more details.
1598b9484cSchristos 
1698b9484cSchristos You should have received a copy of the GNU General Public License
1798b9484cSchristos along with GNU CC; see the file COPYING.  If not, write to
1898b9484cSchristos the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
1998b9484cSchristos 
2098b9484cSchristos As a special exception, if you link this library with files
2198b9484cSchristos compiled with a GNU compiler to produce an executable, this does not cause
2298b9484cSchristos the resulting executable to be covered by the GNU General Public License.
2398b9484cSchristos This exception does not however invalidate any other reasons why
2498b9484cSchristos the executable file might be covered by the GNU General Public License. */
2598b9484cSchristos 
2698b9484cSchristos /*
2798b9484cSchristos 
2898b9484cSchristos @deftypefn Supplemental int snprintf (char *@var{buf}, size_t @var{n}, @
2998b9484cSchristos   const char *@var{format}, ...)
3098b9484cSchristos 
3198b9484cSchristos This function is similar to @code{sprintf}, but it will write to
3298b9484cSchristos @var{buf} at most @code{@var{n}-1} bytes of text, followed by a
3398b9484cSchristos terminating null byte, for a total of @var{n} bytes.
3498b9484cSchristos On error the return value is -1, otherwise it returns the number of
3598b9484cSchristos bytes, not including the terminating null byte, that would have been
3698b9484cSchristos written had @var{n} been sufficiently large, regardless of the actual
3798b9484cSchristos value of @var{n}.  Note some pre-C99 system libraries do not implement
3898b9484cSchristos this correctly so users cannot generally rely on the return value if
3998b9484cSchristos the system version of this function is used.
4098b9484cSchristos 
4198b9484cSchristos @end deftypefn
4298b9484cSchristos 
4398b9484cSchristos */
4498b9484cSchristos 
4598b9484cSchristos #include "ansidecl.h"
4698b9484cSchristos 
4798b9484cSchristos #include <stdarg.h>
4898b9484cSchristos #include <stddef.h>
4998b9484cSchristos 
5098b9484cSchristos int vsnprintf (char *, size_t, const char *, va_list);
5198b9484cSchristos 
5298b9484cSchristos int
5398b9484cSchristos snprintf (char *s, size_t n, const char *format, ...)
5498b9484cSchristos {
5598b9484cSchristos   int result;
56837edd6bSchristos   va_list ap;
57837edd6bSchristos   va_start (ap, format);
5898b9484cSchristos   result = vsnprintf (s, n, format, ap);
59837edd6bSchristos   va_end (ap);
6098b9484cSchristos   return result;
6198b9484cSchristos }
62