1 /* vasprintf and asprintf with out-of-memory checking.
2 Copyright (C) 1999, 2002-2004, 2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include "xvasprintf.h"
29 #include "vasprintf.h"
32 /* Checked size_t computations. */
35 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
37 # define EOVERFLOW E2BIG
41 xstrcat (size_t argcount, va_list args)
49 /* Determine the total size. */
52 for (i = argcount; i > 0; i--)
54 const char *next = va_arg (ap, const char *);
55 totalsize = xsum (totalsize, strlen (next));
59 /* Test for overflow in the summing pass above or in (totalsize + 1) below.
60 Also, don't return a string longer than INT_MAX, for consistency with
62 if (totalsize == SIZE_MAX || totalsize > INT_MAX)
68 /* Allocate and fill the result string. */
69 result = (char *) xmalloc (totalsize + 1);
71 for (i = argcount; i > 0; i--)
73 const char *next = va_arg (args, const char *);
74 size_t len = strlen (next);
75 memcpy (p, next, len);
84 xvasprintf (const char *format, va_list args)
88 /* Recognize the special case format = "%s...%s". It is a frequently used
89 idiom for string concatenation and needs to be fast. We don't want to
90 have a separate function xstrcat() for this purpose. */
98 /* Recognized the special case of string concatenation. */
99 return xstrcat (argcount, args);
110 if (vasprintf (&result, format, args) < 0)