vasprintf.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* Formatted output to strings.
  2. Copyright (C) 1999, 2002, 2006-2013 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, see <http://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. /* Specification. */
  15. #ifdef IN_LIBASPRINTF
  16. # include "vasprintf.h"
  17. #else
  18. # include <stdio.h>
  19. #endif
  20. #include <errno.h>
  21. #include <limits.h>
  22. #include <stdlib.h>
  23. #include "vasnprintf.h"
  24. int
  25. vasprintf (char **resultp, const char *format, va_list args)
  26. {
  27. size_t length;
  28. char *result = vasnprintf (NULL, &length, format, args);
  29. if (result == NULL)
  30. return -1;
  31. if (length > INT_MAX)
  32. {
  33. free (result);
  34. #if (defined _MSC_VER) && (_MSC_VER < 1800)
  35. #else
  36. errno = EOVERFLOW;
  37. #endif
  38. return -1;
  39. }
  40. *resultp = result;
  41. /* Return the number of resulting bytes, excluding the trailing NUL. */
  42. return length;
  43. }