Back to index

cell-binutils  2.17cvs20070401
snprintf.c
Go to the documentation of this file.
00001 /* Implement the snprintf function.
00002    Copyright (C) 2003 Free Software Foundation, Inc.
00003    Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
00004 
00005 This file is part of the libiberty library.  This library is free
00006 software; you can redistribute it and/or modify it under the
00007 terms of the GNU General Public License as published by the
00008 Free Software Foundation; either version 2, or (at your option)
00009 any later version.
00010 
00011 This library is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with GNU CC; see the file COPYING.  If not, write to
00018 the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020 As a special exception, if you link this library with files
00021 compiled with a GNU compiler to produce an executable, this does not cause
00022 the resulting executable to be covered by the GNU General Public License.
00023 This exception does not however invalidate any other reasons why
00024 the executable file might be covered by the GNU General Public License. */
00025 
00026 /*
00027 
00028 @deftypefn Supplemental int snprintf (char *@var{buf}, size_t @var{n}, const char *@var{format}, ...)
00029 
00030 This function is similar to sprintf, but it will print at most @var{n}
00031 characters.  On error the return value is -1, otherwise it returns the
00032 number of characters that would have been printed had @var{n} been
00033 sufficiently large, regardless of the actual value of @var{n}.  Note
00034 some pre-C99 system libraries do not implement this correctly so users
00035 cannot generally rely on the return value if the system version of
00036 this function is used.
00037 
00038 @end deftypefn
00039 
00040 */
00041 
00042 #include "ansidecl.h"
00043 
00044 #include <stdarg.h>
00045 #include <stddef.h>
00046 
00047 int vsnprintf (char *, size_t, const char *, va_list);
00048 
00049 int
00050 snprintf (char *s, size_t n, const char *format, ...)
00051 {
00052   int result;
00053   VA_OPEN (ap, format);
00054   VA_FIXEDARG (ap, char *, s);
00055   VA_FIXEDARG (ap, size_t, n);
00056   VA_FIXEDARG (ap, const char *, format);
00057   result = vsnprintf (s, n, format, ap);
00058   VA_CLOSE (ap);
00059   return result;
00060 }