xasprintf.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* Implement the xasprintf function.
  2. Copyright (C) 2014 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 "libiberty.h"
  22. #include <stdarg.h>
  23. /*
  24. @deftypefn Replacement char* xasprintf (const char *@var{format}, ...)
  25. Print to allocated string without fail. If @code{xasprintf} fails,
  26. this will print a message to @code{stderr} (using the name set by
  27. @code{xmalloc_set_program_name}, if any) and then call @code{xexit}.
  28. @end deftypefn
  29. */
  30. char *
  31. xasprintf (const char *fmt, ...)
  32. {
  33. char *buf;
  34. va_list ap;
  35. va_start (ap, fmt);
  36. buf = xvasprintf (fmt, ap);
  37. va_end (ap);
  38. return buf;
  39. }