vasprintf.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* Formatted output to strings.
  2. Copyright (C) 1999, 2002, 2006 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 2, 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, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "vasprintf.h"
  17. #include <errno.h>
  18. #include <limits.h>
  19. #include <stdlib.h>
  20. #include "vasnprintf.h"
  21. /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
  22. #ifndef EOVERFLOW
  23. # define EOVERFLOW E2BIG
  24. #endif
  25. int
  26. vasprintf (char **resultp, const char *format, va_list args)
  27. {
  28. size_t length;
  29. char *result = vasnprintf (NULL, &length, format, args);
  30. if (result == NULL)
  31. return -1;
  32. if (length > INT_MAX)
  33. {
  34. free (result);
  35. errno = EOVERFLOW;
  36. return -1;
  37. }
  38. *resultp = result;
  39. /* Return the number of resulting bytes, excluding the trailing NUL. */
  40. return length;
  41. }