]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/concat.c
re PR debug/43092 (Wrong debuginfo with VTA and -fomit-frame-pointer/-mno-accumulate...
[thirdparty/gcc.git] / libiberty / concat.c
CommitLineData
6599da04 1/* Concatenate variable number of strings.
0bdcca68 2 Copyright (C) 1991, 1994, 2001 Free Software Foundation, Inc.
6599da04
JM
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
ee58dffd
NC
18not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19Boston, MA 02110-1301, USA. */
6599da04
JM
20
21
22/*
23
5bed56d9 24@deftypefn Extension char* concat (const char *@var{s1}, const char *@var{s2}, @dots{}, @code{NULL})
6599da04 25
aac04c15 26Concatenate zero or more of strings and return the result in freshly
5bed56d9 27@code{xmalloc}ed memory. Returns @code{NULL} if insufficient memory is
aac04c15
DD
28available. The argument list is terminated by the first @code{NULL}
29pointer encountered. Pointers to empty strings are ignored.
6599da04 30
aac04c15 31@end deftypefn
6599da04
JM
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.
aac04c15 43
6599da04
JM
44*/
45
46
b6a2f884
AJ
47#ifdef HAVE_CONFIG_H
48#include "config.h"
49#endif
6599da04
JM
50#include "ansidecl.h"
51#include "libiberty.h"
c8b28221 52#include <sys/types.h> /* size_t */
6599da04 53
6599da04 54#include <stdarg.h>
6599da04 55
0bdcca68
RH
56# if HAVE_STRING_H
57# include <string.h>
58# else
59# if HAVE_STRINGS_H
60# include <strings.h>
61# endif
62# endif
6599da04 63
576fb787
KG
64#if HAVE_STDLIB_H
65#include <stdlib.h>
66#endif
67
9486db4f 68static inline unsigned long vconcat_length (const char *, va_list);
c793eea7 69static inline unsigned long
9486db4f 70vconcat_length (const char *first, va_list args)
6599da04 71{
c793eea7
KG
72 unsigned long length = 0;
73 const char *arg;
6599da04 74
0bdcca68
RH
75 for (arg = first; arg ; arg = va_arg (args, const char *))
76 length += strlen (arg);
77
c793eea7
KG
78 return length;
79}
6599da04 80
c793eea7 81static inline char *
9486db4f 82vconcat_copy (char *dst, const char *first, va_list args)
c793eea7
KG
83{
84 char *end = dst;
85 const char *arg;
0bdcca68 86
0bdcca68
RH
87 for (arg = first; arg ; arg = va_arg (args, const char *))
88 {
c793eea7 89 unsigned long length = strlen (arg);
0bdcca68
RH
90 memcpy (end, arg, length);
91 end += length;
6599da04 92 }
0bdcca68 93 *end = '\000';
c793eea7
KG
94
95 return dst;
96}
97
aac04c15
DD
98/* @undocumented concat_length */
99
c793eea7 100unsigned long
9486db4f 101concat_length (const char *first, ...)
c793eea7
KG
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
aac04c15
DD
113/* @undocumented concat_copy */
114
c793eea7 115char *
9486db4f 116concat_copy (char *dst, const char *first, ...)
c793eea7
KG
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
d7cf8390
GDR
130#ifdef __cplusplus
131extern "C" {
132#endif /* __cplusplus */
c793eea7 133char *libiberty_concat_ptr;
d7cf8390
GDR
134#ifdef __cplusplus
135}
136#endif /* __cplusplus */
c793eea7 137
aac04c15
DD
138/* @undocumented concat_copy2 */
139
c793eea7 140char *
9486db4f 141concat_copy2 (const char *first, ...)
c793eea7
KG
142{
143 VA_OPEN (args, first);
144 VA_FIXEDARG (args, const char *, first);
145 vconcat_copy (libiberty_concat_ptr, first, args);
146 VA_CLOSE (args);
147
148 return libiberty_concat_ptr;
149}
150
151char *
9486db4f 152concat (const char *first, ...)
c793eea7
KG
153{
154 char *newstr;
155
156 /* First compute the size of the result and get sufficient memory. */
157 VA_OPEN (args, first);
158 VA_FIXEDARG (args, const char *, first);
d7cf8390 159 newstr = XNEWVEC (char, vconcat_length (first, args) + 1);
c793eea7
KG
160 VA_CLOSE (args);
161
162 /* Now copy the individual pieces to the result string. */
163 VA_OPEN (args, first);
164 VA_FIXEDARG (args, const char *, first);
165 vconcat_copy (newstr, first, args);
e2dff3f2 166 VA_CLOSE (args);
6599da04 167
0bdcca68 168 return newstr;
6599da04
JM
169}
170
aac04c15
DD
171/*
172
5bed56d9 173@deftypefn Extension char* reconcat (char *@var{optr}, const char *@var{s1}, @dots{}, @code{NULL})
aac04c15
DD
174
175Same as @code{concat}, except that if @var{optr} is not @code{NULL} it
176is freed after the string is created. This is intended to be useful
177when you're extending an existing string or building up a string in a
178loop:
179
180@example
181 str = reconcat (str, "pre-", str, NULL);
182@end example
183
184@end deftypefn
185
186*/
187
ad43d46f 188char *
9486db4f 189reconcat (char *optr, const char *first, ...)
ad43d46f
KG
190{
191 char *newstr;
192
193 /* First compute the size of the result and get sufficient memory. */
194 VA_OPEN (args, first);
195 VA_FIXEDARG (args, char *, optr);
196 VA_FIXEDARG (args, const char *, first);
d7cf8390 197 newstr = XNEWVEC (char, vconcat_length (first, args) + 1);
ad43d46f
KG
198 VA_CLOSE (args);
199
200 /* Now copy the individual pieces to the result string. */
201 VA_OPEN (args, first);
202 VA_FIXEDARG (args, char *, optr);
203 VA_FIXEDARG (args, const char *, first);
204 vconcat_copy (newstr, first, args);
c1766881 205 if (optr) /* Done before VA_CLOSE so optr stays in scope for K&R C. */
ad43d46f 206 free (optr);
c1766881 207 VA_CLOSE (args);
ad43d46f
KG
208
209 return newstr;
210}
211
6599da04 212#ifdef MAIN
0bdcca68 213#define NULLP (char *)0
6599da04
JM
214
215/* Simple little test driver. */
216
217#include <stdio.h>
218
219int
9486db4f 220main (void)
6599da04
JM
221{
222 printf ("\"\" = \"%s\"\n", concat (NULLP));
223 printf ("\"a\" = \"%s\"\n", concat ("a", NULLP));
224 printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP));
225 printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP));
226 printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP));
227 printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP));
228 printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP));
229 return 0;
230}
231
232#endif