vasprintf.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* Formatted output to strings.
  2. Copyright (C) 1999, 2002, 2006-2007 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, 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. #ifdef IN_LIBASPRINTF
  17. # include "vasprintf.h"
  18. #else
  19. # include <stdio.h>
  20. #endif
  21. #include <errno.h>
  22. #include <limits.h>
  23. #include <stdlib.h>
  24. #include "vasnprintf.h"
  25. /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
  26. #ifndef EOVERFLOW
  27. # define EOVERFLOW E2BIG
  28. #endif
  29. int
  30. vasprintf (char **resultp, const char *format, va_list args)
  31. {
  32. size_t length;
  33. char *result = vasnprintf (NULL, &length, format, args);
  34. if (result == NULL)
  35. return -1;
  36. if (length > INT_MAX)
  37. {
  38. free (result);
  39. errno = EOVERFLOW;
  40. return -1;
  41. }
  42. *resultp = result;
  43. /* Return the number of resulting bytes, excluding the trailing NUL. */
  44. return length;
  45. }