* emultempl/elf32.em (gld${EMULATION_NAME}_stat_needed): Ignore
[binutils.git] / libiberty / concat.c
blobe3bdd91c5507139bb012978a8972f5d71cda8c48
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. */
24 @deftypefn Extension char* concat (const char *@var{s1}, const char *@var{s2}, @dots{}, @code{NULL})
26 Concatenate zero or more of strings and return the result in freshly
27 @code{xmalloc}ed memory. Returns @code{NULL} if insufficient memory is
28 available. The argument list is terminated by the first @code{NULL}
29 pointer encountered. Pointers to empty strings are ignored.
31 @end deftypefn
33 NOTES
35 This function uses xmalloc() which is expected to be a front end
36 function to malloc() that deals with low memory situations. In
37 typical use, if malloc() returns NULL then xmalloc() diverts to an
38 error handler routine which never returns, and thus xmalloc will
39 never return a NULL pointer. If the client application wishes to
40 deal with low memory situations itself, it should supply an xmalloc
41 that just directly invokes malloc and blindly returns whatever
42 malloc returns.
47 #ifdef HAVE_CONFIG_H
48 #include "config.h"
49 #endif
50 #include "ansidecl.h"
51 #include "libiberty.h"
52 #include <sys/types.h> /* size_t */
54 #include <stdarg.h>
56 # if HAVE_STRING_H
57 # include <string.h>
58 # else
59 # if HAVE_STRINGS_H
60 # include <strings.h>
61 # endif
62 # endif
64 #if HAVE_STDLIB_H
65 #include <stdlib.h>
66 #endif
68 static inline unsigned long vconcat_length (const char *, va_list);
69 static inline unsigned long
70 vconcat_length (const char *first, va_list args)
72 unsigned long length = 0;
73 const char *arg;
75 for (arg = first; arg ; arg = va_arg (args, const char *))
76 length += strlen (arg);
78 return length;
81 static inline char *
82 vconcat_copy (char *dst, const char *first, va_list args)
84 char *end = dst;
85 const char *arg;
87 for (arg = first; arg ; arg = va_arg (args, const char *))
89 unsigned long length = strlen (arg);
90 memcpy (end, arg, length);
91 end += length;
93 *end = '\000';
95 return dst;
98 /* @undocumented concat_length */
100 unsigned long
101 concat_length (const char *first, ...)
103 unsigned long length;
105 VA_OPEN (args, first);
106 VA_FIXEDARG (args, const char *, first);
107 length = vconcat_length (first, args);
108 VA_CLOSE (args);
110 return length;
113 /* @undocumented concat_copy */
115 char *
116 concat_copy (char *dst, const char *first, ...)
118 char *save_dst;
120 VA_OPEN (args, first);
121 VA_FIXEDARG (args, char *, dst);
122 VA_FIXEDARG (args, const char *, first);
123 vconcat_copy (dst, first, args);
124 save_dst = dst; /* With K&R C, dst goes out of scope here. */
125 VA_CLOSE (args);
127 return save_dst;
130 char *libiberty_concat_ptr;
132 /* @undocumented concat_copy2 */
134 char *
135 concat_copy2 (const char *first, ...)
137 VA_OPEN (args, first);
138 VA_FIXEDARG (args, const char *, first);
139 vconcat_copy (libiberty_concat_ptr, first, args);
140 VA_CLOSE (args);
142 return libiberty_concat_ptr;
145 char *
146 concat (const char *first, ...)
148 char *newstr;
150 /* First compute the size of the result and get sufficient memory. */
151 VA_OPEN (args, first);
152 VA_FIXEDARG (args, const char *, first);
153 newstr = (char *) xmalloc (vconcat_length (first, args) + 1);
154 VA_CLOSE (args);
156 /* Now copy the individual pieces to the result string. */
157 VA_OPEN (args, first);
158 VA_FIXEDARG (args, const char *, first);
159 vconcat_copy (newstr, first, args);
160 VA_CLOSE (args);
162 return newstr;
167 @deftypefn Extension char* reconcat (char *@var{optr}, const char *@var{s1}, @dots{}, @code{NULL})
169 Same as @code{concat}, except that if @var{optr} is not @code{NULL} it
170 is freed after the string is created. This is intended to be useful
171 when you're extending an existing string or building up a string in a
172 loop:
174 @example
175 str = reconcat (str, "pre-", str, NULL);
176 @end example
178 @end deftypefn
182 char *
183 reconcat (char *optr, const char *first, ...)
185 char *newstr;
187 /* First compute the size of the result and get sufficient memory. */
188 VA_OPEN (args, first);
189 VA_FIXEDARG (args, char *, optr);
190 VA_FIXEDARG (args, const char *, first);
191 newstr = (char *) xmalloc (vconcat_length (first, args) + 1);
192 VA_CLOSE (args);
194 /* Now copy the individual pieces to the result string. */
195 VA_OPEN (args, first);
196 VA_FIXEDARG (args, char *, optr);
197 VA_FIXEDARG (args, const char *, first);
198 vconcat_copy (newstr, first, args);
199 if (optr) /* Done before VA_CLOSE so optr stays in scope for K&R C. */
200 free (optr);
201 VA_CLOSE (args);
203 return newstr;
206 #ifdef MAIN
207 #define NULLP (char *)0
209 /* Simple little test driver. */
211 #include <stdio.h>
214 main (void)
216 printf ("\"\" = \"%s\"\n", concat (NULLP));
217 printf ("\"a\" = \"%s\"\n", concat ("a", NULLP));
218 printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP));
219 printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP));
220 printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP));
221 printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP));
222 printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP));
223 return 0;
226 #endif