xvasprintf.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* Implement the xvasprintf function.
  2. Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. Contributed by Manuel Lopez-Ibanez.
  4. This file is part of the libiberty library.
  5. Libiberty is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. Libiberty 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 GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with libiberty; see the file COPYING.LIB. If not, write
  15. to the Free Software Foundation, Inc., 51 Franklin Street - Fifth
  16. Floor, Boston, MA 02110-1301, USA. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include <ansidecl.h>
  21. #include <stdarg.h>
  22. #if !defined (va_copy) && defined (__va_copy)
  23. # define va_copy(d,s) __va_copy((d),(s))
  24. #endif
  25. #include <stdio.h>
  26. #ifdef HAVE_STRING_H
  27. #include <string.h>
  28. #endif
  29. #include "libiberty.h"
  30. #include "vprintf-support.h"
  31. /*
  32. @deftypefn Replacement char* xvasprintf (const char *@var{format}, va_list @var{args})
  33. Print to allocated string without fail. If @code{xvasprintf} fails,
  34. this will print a message to @code{stderr} (using the name set by
  35. @code{xmalloc_set_program_name}, if any) and then call @code{xexit}.
  36. @end deftypefn
  37. */
  38. char *
  39. xvasprintf (const char *format,
  40. #if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
  41. _BSD_VA_LIST_ args)
  42. #else
  43. va_list args)
  44. #endif
  45. {
  46. char *result;
  47. int total_width = libiberty_vprintf_buffer_size (format, args);
  48. result = (char *) xmalloc (total_width);
  49. vsprintf (result, format, args);
  50. return result;
  51. }