]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/concat.c
re PR tree-optimization/58689 ([meta-bug] __attribute__((returns_nonnull)) enhancements)
[thirdparty/gcc.git] / libiberty / concat.c
CommitLineData
6599da04 1/* Concatenate variable number of strings.
996c0cb0 2 Copyright (C) 1991, 1994, 2001, 2011 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
996c0cb0
RW
24@deftypefn Extension char* concat (const char *@var{s1}, const char *@var{s2}, @
25 @dots{}, @code{NULL})
6599da04 26
aac04c15 27Concatenate zero or more of strings and return the result in freshly
eda14d6a
MG
28@code{xmalloc}ed memory. The argument list is terminated by the first
29@code{NULL} pointer encountered. Pointers to empty strings are ignored.
6599da04 30
aac04c15 31@end deftypefn
6599da04 32
6599da04
JM
33*/
34
35
b6a2f884
AJ
36#ifdef HAVE_CONFIG_H
37#include "config.h"
38#endif
6599da04
JM
39#include "ansidecl.h"
40#include "libiberty.h"
c8b28221 41#include <sys/types.h> /* size_t */
6599da04 42
6599da04 43#include <stdarg.h>
6599da04 44
0bdcca68
RH
45# if HAVE_STRING_H
46# include <string.h>
47# else
48# if HAVE_STRINGS_H
49# include <strings.h>
50# endif
51# endif
6599da04 52
576fb787
KG
53#if HAVE_STDLIB_H
54#include <stdlib.h>
55#endif
56
9486db4f 57static inline unsigned long vconcat_length (const char *, va_list);
c793eea7 58static inline unsigned long
9486db4f 59vconcat_length (const char *first, va_list args)
6599da04 60{
c793eea7
KG
61 unsigned long length = 0;
62 const char *arg;
6599da04 63
0bdcca68
RH
64 for (arg = first; arg ; arg = va_arg (args, const char *))
65 length += strlen (arg);
66
c793eea7
KG
67 return length;
68}
6599da04 69
c793eea7 70static inline char *
9486db4f 71vconcat_copy (char *dst, const char *first, va_list args)
c793eea7
KG
72{
73 char *end = dst;
74 const char *arg;
0bdcca68 75
0bdcca68
RH
76 for (arg = first; arg ; arg = va_arg (args, const char *))
77 {
c793eea7 78 unsigned long length = strlen (arg);
0bdcca68
RH
79 memcpy (end, arg, length);
80 end += length;
6599da04 81 }
0bdcca68 82 *end = '\000';
c793eea7
KG
83
84 return dst;
85}
86
aac04c15
DD
87/* @undocumented concat_length */
88
c793eea7 89unsigned long
9486db4f 90concat_length (const char *first, ...)
c793eea7
KG
91{
92 unsigned long length;
93
94 VA_OPEN (args, first);
95 VA_FIXEDARG (args, const char *, first);
96 length = vconcat_length (first, args);
97 VA_CLOSE (args);
98
99 return length;
100}
101
aac04c15
DD
102/* @undocumented concat_copy */
103
c793eea7 104char *
9486db4f 105concat_copy (char *dst, const char *first, ...)
c793eea7
KG
106{
107 char *save_dst;
108
109 VA_OPEN (args, first);
110 VA_FIXEDARG (args, char *, dst);
111 VA_FIXEDARG (args, const char *, first);
112 vconcat_copy (dst, first, args);
113 save_dst = dst; /* With K&R C, dst goes out of scope here. */
114 VA_CLOSE (args);
115
116 return save_dst;
117}
118
d7cf8390
GDR
119#ifdef __cplusplus
120extern "C" {
121#endif /* __cplusplus */
c793eea7 122char *libiberty_concat_ptr;
d7cf8390
GDR
123#ifdef __cplusplus
124}
125#endif /* __cplusplus */
c793eea7 126
aac04c15
DD
127/* @undocumented concat_copy2 */
128
c793eea7 129char *
9486db4f 130concat_copy2 (const char *first, ...)
c793eea7
KG
131{
132 VA_OPEN (args, first);
133 VA_FIXEDARG (args, const char *, first);
134 vconcat_copy (libiberty_concat_ptr, first, args);
135 VA_CLOSE (args);
136
137 return libiberty_concat_ptr;
138}
139
140char *
9486db4f 141concat (const char *first, ...)
c793eea7
KG
142{
143 char *newstr;
144
145 /* First compute the size of the result and get sufficient memory. */
146 VA_OPEN (args, first);
147 VA_FIXEDARG (args, const char *, first);
d7cf8390 148 newstr = XNEWVEC (char, vconcat_length (first, args) + 1);
c793eea7
KG
149 VA_CLOSE (args);
150
151 /* Now copy the individual pieces to the result string. */
152 VA_OPEN (args, first);
153 VA_FIXEDARG (args, const char *, first);
154 vconcat_copy (newstr, first, args);
e2dff3f2 155 VA_CLOSE (args);
6599da04 156
0bdcca68 157 return newstr;
6599da04
JM
158}
159
aac04c15
DD
160/*
161
996c0cb0
RW
162@deftypefn Extension char* reconcat (char *@var{optr}, const char *@var{s1}, @
163 @dots{}, @code{NULL})
aac04c15
DD
164
165Same as @code{concat}, except that if @var{optr} is not @code{NULL} it
166is freed after the string is created. This is intended to be useful
167when you're extending an existing string or building up a string in a
168loop:
169
170@example
171 str = reconcat (str, "pre-", str, NULL);
172@end example
173
174@end deftypefn
175
176*/
177
ad43d46f 178char *
9486db4f 179reconcat (char *optr, const char *first, ...)
ad43d46f
KG
180{
181 char *newstr;
182
183 /* First compute the size of the result and get sufficient memory. */
184 VA_OPEN (args, first);
185 VA_FIXEDARG (args, char *, optr);
186 VA_FIXEDARG (args, const char *, first);
d7cf8390 187 newstr = XNEWVEC (char, vconcat_length (first, args) + 1);
ad43d46f
KG
188 VA_CLOSE (args);
189
190 /* Now copy the individual pieces to the result string. */
191 VA_OPEN (args, first);
192 VA_FIXEDARG (args, char *, optr);
193 VA_FIXEDARG (args, const char *, first);
194 vconcat_copy (newstr, first, args);
c1766881 195 if (optr) /* Done before VA_CLOSE so optr stays in scope for K&R C. */
ad43d46f 196 free (optr);
c1766881 197 VA_CLOSE (args);
ad43d46f
KG
198
199 return newstr;
200}
201
6599da04 202#ifdef MAIN
0bdcca68 203#define NULLP (char *)0
6599da04
JM
204
205/* Simple little test driver. */
206
207#include <stdio.h>
208
209int
9486db4f 210main (void)
6599da04
JM
211{
212 printf ("\"\" = \"%s\"\n", concat (NULLP));
213 printf ("\"a\" = \"%s\"\n", concat ("a", NULLP));
214 printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP));
215 printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP));
216 printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP));
217 printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP));
218 printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP));
219 return 0;
220}
221
222#endif