1 /* Concatenate variable number of strings.
2 Copyright (C) 1991, 1994, 2001 Free Software Foundation, Inc.
3 Written by Fred Fish @ Cygnus Support
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 concat -- concatenate a variable number of strings
32 char *concat (s1, s2, s3, ..., NULL)
36 Concatenate a variable number of strings and return the result
37 in freshly malloc'd memory.
39 Returns NULL if insufficient memory is available. The argument
40 list is terminated by the first NULL pointer encountered. Pointers
41 to empty strings are ignored.
45 This function uses xmalloc() which is expected to be a front end
46 function to malloc() that deals with low memory situations. In
47 typical use, if malloc() returns NULL then xmalloc() diverts to an
48 error handler routine which never returns, and thus xmalloc will
49 never return a NULL pointer. If the client application wishes to
50 deal with low memory situations itself, it should supply an xmalloc
51 that just directly invokes malloc and blindly returns whatever
60 #include "libiberty.h"
61 #include <sys/types.h> /* size_t */
63 #ifdef ANSI_PROTOTYPES
77 static inline unsigned long vconcat_length PARAMS ((const char *, va_list));
78 static inline unsigned long
79 vconcat_length (first, args)
83 unsigned long length = 0;
86 for (arg = first; arg ; arg = va_arg (args, const char *))
87 length += strlen (arg);
92 static inline char *vconcat_copy PARAMS ((char *, const char *, va_list));
94 vconcat_copy (dst, first, args)
102 for (arg = first; arg ; arg = va_arg (args, const char *))
104 unsigned long length = strlen (arg);
105 memcpy (end, arg, length);
114 concat_length VPARAMS ((const char *first, ...))
116 unsigned long length;
118 VA_OPEN (args, first);
119 VA_FIXEDARG (args, const char *, first);
120 length = vconcat_length (first, args);
127 concat_copy VPARAMS ((char *dst, const char *first, ...))
131 VA_OPEN (args, first);
132 VA_FIXEDARG (args, char *, dst);
133 VA_FIXEDARG (args, const char *, first);
134 vconcat_copy (dst, first, args);
135 save_dst = dst; /* With K&R C, dst goes out of scope here. */
141 char *libiberty_concat_ptr;
144 concat_copy2 VPARAMS ((const char *first, ...))
146 VA_OPEN (args, first);
147 VA_FIXEDARG (args, const char *, first);
148 vconcat_copy (libiberty_concat_ptr, first, args);
151 return libiberty_concat_ptr;
155 concat VPARAMS ((const char *first, ...))
159 /* First compute the size of the result and get sufficient memory. */
160 VA_OPEN (args, first);
161 VA_FIXEDARG (args, const char *, first);
162 newstr = (char *) xmalloc (vconcat_length (first, args) + 1);
165 /* Now copy the individual pieces to the result string. */
166 VA_OPEN (args, first);
167 VA_FIXEDARG (args, const char *, first);
168 vconcat_copy (newstr, first, args);
175 reconcat VPARAMS ((char *optr, const char *first, ...))
179 /* First compute the size of the result and get sufficient memory. */
180 VA_OPEN (args, first);
181 VA_FIXEDARG (args, char *, optr);
182 VA_FIXEDARG (args, const char *, first);
183 newstr = (char *) xmalloc (vconcat_length (first, args) + 1);
186 /* Now copy the individual pieces to the result string. */
187 VA_OPEN (args, first);
188 VA_FIXEDARG (args, char *, optr);
189 VA_FIXEDARG (args, const char *, first);
190 vconcat_copy (newstr, first, args);
200 #define NULLP (char *)0
202 /* Simple little test driver. */
209 printf ("\"\" = \"%s\"\n", concat (NULLP));
210 printf ("\"a\" = \"%s\"\n", concat ("a", NULLP));
211 printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP));
212 printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP));
213 printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP));
214 printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP));
215 printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP));