* config/tc-xtensa.h (struct xtensa_frag_type): Add lit_frchain field.
[deliverable/binutils-gdb.git] / libiberty / concat.c
CommitLineData
252b5132 1/* Concatenate variable number of strings.
d42dae6c 2 Copyright (C) 1991, 1994, 2001 Free Software Foundation, Inc.
252b5132
RH
3 Written by Fred Fish @ Cygnus Support
4
5This file is part of the libiberty library.
6Libiberty is free software; you can redistribute it and/or
7modify it under the terms of the GNU Library General Public
8License as published by the Free Software Foundation; either
9version 2 of the License, or (at your option) any later version.
10
11Libiberty is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14Library General Public License for more details.
15
16You should have received a copy of the GNU Library General Public
17License along with libiberty; see the file COPYING.LIB. If
18not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
20
21
22/*
23
5d852400 24@deftypefn Extension char* concat (const char *@var{s1}, const char *@var{s2}, @dots{}, @code{NULL})
252b5132 25
ba19b94f 26Concatenate zero or more of strings and return the result in freshly
5d852400 27@code{xmalloc}ed memory. Returns @code{NULL} if insufficient memory is
ba19b94f
DD
28available. The argument list is terminated by the first @code{NULL}
29pointer encountered. Pointers to empty strings are ignored.
252b5132 30
ba19b94f 31@end deftypefn
252b5132
RH
32
33NOTES
34
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.
ba19b94f 43
252b5132
RH
44*/
45
46
65a4f13c
DD
47#ifdef HAVE_CONFIG_H
48#include "config.h"
49#endif
252b5132
RH
50#include "ansidecl.h"
51#include "libiberty.h"
926150e2 52#include <sys/types.h> /* size_t */
252b5132 53
252b5132 54#include <stdarg.h>
252b5132 55
d42dae6c
DD
56# if HAVE_STRING_H
57# include <string.h>
58# else
59# if HAVE_STRINGS_H
60# include <strings.h>
61# endif
62# endif
252b5132 63
916aaa12
DD
64#if HAVE_STDLIB_H
65#include <stdlib.h>
66#endif
67
9334f9c6 68static inline unsigned long vconcat_length (const char *, va_list);
54c20242 69static inline unsigned long
9334f9c6 70vconcat_length (const char *first, va_list args)
252b5132 71{
54c20242
DD
72 unsigned long length = 0;
73 const char *arg;
252b5132 74
d42dae6c
DD
75 for (arg = first; arg ; arg = va_arg (args, const char *))
76 length += strlen (arg);
77
54c20242
DD
78 return length;
79}
252b5132 80
54c20242 81static inline char *
9334f9c6 82vconcat_copy (char *dst, const char *first, va_list args)
54c20242
DD
83{
84 char *end = dst;
85 const char *arg;
d42dae6c 86
d42dae6c
DD
87 for (arg = first; arg ; arg = va_arg (args, const char *))
88 {
54c20242 89 unsigned long length = strlen (arg);
d42dae6c
DD
90 memcpy (end, arg, length);
91 end += length;
252b5132 92 }
d42dae6c 93 *end = '\000';
54c20242
DD
94
95 return dst;
96}
97
ba19b94f
DD
98/* @undocumented concat_length */
99
54c20242 100unsigned long
9334f9c6 101concat_length (const char *first, ...)
54c20242
DD
102{
103 unsigned long length;
104
105 VA_OPEN (args, first);
106 VA_FIXEDARG (args, const char *, first);
107 length = vconcat_length (first, args);
108 VA_CLOSE (args);
109
110 return length;
111}
112
ba19b94f
DD
113/* @undocumented concat_copy */
114
54c20242 115char *
9334f9c6 116concat_copy (char *dst, const char *first, ...)
54c20242
DD
117{
118 char *save_dst;
119
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);
126
127 return save_dst;
128}
129
130char *libiberty_concat_ptr;
131
ba19b94f
DD
132/* @undocumented concat_copy2 */
133
54c20242 134char *
9334f9c6 135concat_copy2 (const char *first, ...)
54c20242
DD
136{
137 VA_OPEN (args, first);
138 VA_FIXEDARG (args, const char *, first);
139 vconcat_copy (libiberty_concat_ptr, first, args);
140 VA_CLOSE (args);
141
142 return libiberty_concat_ptr;
143}
144
145char *
9334f9c6 146concat (const char *first, ...)
54c20242
DD
147{
148 char *newstr;
149
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);
155
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);
8a423cb3 160 VA_CLOSE (args);
252b5132 161
d42dae6c 162 return newstr;
252b5132
RH
163}
164
ba19b94f
DD
165/*
166
5d852400 167@deftypefn Extension char* reconcat (char *@var{optr}, const char *@var{s1}, @dots{}, @code{NULL})
ba19b94f
DD
168
169Same as @code{concat}, except that if @var{optr} is not @code{NULL} it
170is freed after the string is created. This is intended to be useful
171when you're extending an existing string or building up a string in a
172loop:
173
174@example
175 str = reconcat (str, "pre-", str, NULL);
176@end example
177
178@end deftypefn
179
180*/
181
99ee3a8f 182char *
9334f9c6 183reconcat (char *optr, const char *first, ...)
99ee3a8f
DD
184{
185 char *newstr;
186
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);
193
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);
66c94e19 199 if (optr) /* Done before VA_CLOSE so optr stays in scope for K&R C. */
99ee3a8f 200 free (optr);
66c94e19 201 VA_CLOSE (args);
99ee3a8f
DD
202
203 return newstr;
204}
205
252b5132 206#ifdef MAIN
d42dae6c 207#define NULLP (char *)0
252b5132
RH
208
209/* Simple little test driver. */
210
211#include <stdio.h>
212
213int
9334f9c6 214main (void)
252b5132
RH
215{
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;
224}
225
226#endif
This page took 0.340217 seconds and 4 git commands to generate.