snprintf.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Implement the snprintf function.
  2. Copyright (C) 2003, 2011, 2013 Free Software Foundation, Inc.
  3. Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>.
  4. This file is part of the libiberty library. This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU CC; see the file COPYING. If not, write to
  15. the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  16. As a special exception, if you link this library with files
  17. compiled with a GNU compiler to produce an executable, this does not cause
  18. the resulting executable to be covered by the GNU General Public License.
  19. This exception does not however invalidate any other reasons why
  20. the executable file might be covered by the GNU General Public License. */
  21. /*
  22. @deftypefn Supplemental int snprintf (char *@var{buf}, size_t @var{n}, @
  23. const char *@var{format}, ...)
  24. This function is similar to @code{sprintf}, but it will write to
  25. @var{buf} at most @code{@var{n}-1} bytes of text, followed by a
  26. terminating null byte, for a total of @var{n} bytes.
  27. On error the return value is -1, otherwise it returns the number of
  28. bytes, not including the terminating null byte, that would have been
  29. written had @var{n} been sufficiently large, regardless of the actual
  30. value of @var{n}. Note some pre-C99 system libraries do not implement
  31. this correctly so users cannot generally rely on the return value if
  32. the system version of this function is used.
  33. @end deftypefn
  34. */
  35. #include "ansidecl.h"
  36. #include <stdarg.h>
  37. #include <stddef.h>
  38. int vsnprintf (char *, size_t, const char *, va_list);
  39. int
  40. snprintf (char *s, size_t n, const char *format, ...)
  41. {
  42. int result;
  43. va_list ap;
  44. va_start (ap, format);
  45. result = vsnprintf (s, n, format, ap);
  46. va_end (ap);
  47. return result;
  48. }