Merge from gcc
[binutils.git] / libiberty / snprintf.c
blob89164695b5002be7c9bfbb8456553ec57fef3ac5
1 /* Implement the snprintf function.
2 Copyright (C) 2003 Free Software Foundation, Inc.
3 Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
5 This file is part of the libiberty library. This library is free
6 software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 As a special exception, if you link this library with files
21 compiled with a GNU compiler to produce an executable, this does not cause
22 the resulting executable to be covered by the GNU General Public License.
23 This exception does not however invalidate any other reasons why
24 the executable file might be covered by the GNU General Public License. */
28 @deftypefn Supplemental int snprintf (char *@var{buf}, size_t @var{n}, const char *@var{format}, ...)
30 This function is similar to sprintf, but it will print at most @var{n}
31 characters. On error the return value is -1, otherwise it returns the
32 number of characters that would have been printed had @var{n} been
33 sufficiently large, regardless of the actual value of @var{n}. Note
34 some pre-C99 system libraries do not implement this correctly so users
35 cannot generally rely on the return value if the system version of
36 this function is used.
38 @end deftypefn
42 #include "ansidecl.h"
44 #ifdef ANSI_PROTOTYPES
45 #include <stdarg.h>
46 #include <stddef.h>
47 #else
48 #include <varargs.h>
49 #define size_t unsigned long
50 #endif
52 int vsnprintf PARAMS ((char *, size_t, const char *, va_list));
54 int
55 snprintf VPARAMS ((char *s, size_t n, const char *format, ...))
57 int result;
58 VA_OPEN (ap, format);
59 VA_FIXEDARG (ap, char *, s);
60 VA_FIXEDARG (ap, size_t, n);
61 VA_FIXEDARG (ap, const char *, format);
62 result = vsnprintf (s, n, format, ap);
63 VA_CLOSE (ap);
64 return result;