1*a9fa9459Szrj /* Concatenate variable number of strings.
2*a9fa9459Szrj    Copyright (C) 1991, 1994, 2001, 2011, 2013 Free Software Foundation, Inc.
3*a9fa9459Szrj    Written by Fred Fish @ Cygnus Support
4*a9fa9459Szrj 
5*a9fa9459Szrj This file is part of the libiberty library.
6*a9fa9459Szrj Libiberty is free software; you can redistribute it and/or
7*a9fa9459Szrj modify it under the terms of the GNU Library General Public
8*a9fa9459Szrj License as published by the Free Software Foundation; either
9*a9fa9459Szrj version 2 of the License, or (at your option) any later version.
10*a9fa9459Szrj 
11*a9fa9459Szrj Libiberty is distributed in the hope that it will be useful,
12*a9fa9459Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
13*a9fa9459Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*a9fa9459Szrj Library General Public License for more details.
15*a9fa9459Szrj 
16*a9fa9459Szrj You should have received a copy of the GNU Library General Public
17*a9fa9459Szrj License along with libiberty; see the file COPYING.LIB.  If
18*a9fa9459Szrj not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19*a9fa9459Szrj Boston, MA 02110-1301, USA.  */
20*a9fa9459Szrj 
21*a9fa9459Szrj 
22*a9fa9459Szrj /*
23*a9fa9459Szrj 
24*a9fa9459Szrj @deftypefn Extension char* concat (const char *@var{s1}, const char *@var{s2}, @
25*a9fa9459Szrj   @dots{}, @code{NULL})
26*a9fa9459Szrj 
27*a9fa9459Szrj Concatenate zero or more of strings and return the result in freshly
28*a9fa9459Szrj @code{xmalloc}ed memory.  The argument list is terminated by the first
29*a9fa9459Szrj @code{NULL} pointer encountered.  Pointers to empty strings are ignored.
30*a9fa9459Szrj 
31*a9fa9459Szrj @end deftypefn
32*a9fa9459Szrj 
33*a9fa9459Szrj */
34*a9fa9459Szrj 
35*a9fa9459Szrj 
36*a9fa9459Szrj #ifdef HAVE_CONFIG_H
37*a9fa9459Szrj #include "config.h"
38*a9fa9459Szrj #endif
39*a9fa9459Szrj #include "ansidecl.h"
40*a9fa9459Szrj #include "libiberty.h"
41*a9fa9459Szrj #include <sys/types.h>		/* size_t */
42*a9fa9459Szrj 
43*a9fa9459Szrj #include <stdarg.h>
44*a9fa9459Szrj 
45*a9fa9459Szrj # if HAVE_STRING_H
46*a9fa9459Szrj #  include <string.h>
47*a9fa9459Szrj # else
48*a9fa9459Szrj #  if HAVE_STRINGS_H
49*a9fa9459Szrj #   include <strings.h>
50*a9fa9459Szrj #  endif
51*a9fa9459Szrj # endif
52*a9fa9459Szrj 
53*a9fa9459Szrj #if HAVE_STDLIB_H
54*a9fa9459Szrj #include <stdlib.h>
55*a9fa9459Szrj #endif
56*a9fa9459Szrj 
57*a9fa9459Szrj static inline unsigned long vconcat_length (const char *, va_list);
58*a9fa9459Szrj static inline unsigned long
vconcat_length(const char * first,va_list args)59*a9fa9459Szrj vconcat_length (const char *first, va_list args)
60*a9fa9459Szrj {
61*a9fa9459Szrj   unsigned long length = 0;
62*a9fa9459Szrj   const char *arg;
63*a9fa9459Szrj 
64*a9fa9459Szrj   for (arg = first; arg ; arg = va_arg (args, const char *))
65*a9fa9459Szrj     length += strlen (arg);
66*a9fa9459Szrj 
67*a9fa9459Szrj   return length;
68*a9fa9459Szrj }
69*a9fa9459Szrj 
70*a9fa9459Szrj static inline char *
vconcat_copy(char * dst,const char * first,va_list args)71*a9fa9459Szrj vconcat_copy (char *dst, const char *first, va_list args)
72*a9fa9459Szrj {
73*a9fa9459Szrj   char *end = dst;
74*a9fa9459Szrj   const char *arg;
75*a9fa9459Szrj 
76*a9fa9459Szrj   for (arg = first; arg ; arg = va_arg (args, const char *))
77*a9fa9459Szrj     {
78*a9fa9459Szrj       unsigned long length = strlen (arg);
79*a9fa9459Szrj       memcpy (end, arg, length);
80*a9fa9459Szrj       end += length;
81*a9fa9459Szrj     }
82*a9fa9459Szrj   *end = '\000';
83*a9fa9459Szrj 
84*a9fa9459Szrj   return dst;
85*a9fa9459Szrj }
86*a9fa9459Szrj 
87*a9fa9459Szrj /* @undocumented concat_length */
88*a9fa9459Szrj 
89*a9fa9459Szrj unsigned long
concat_length(const char * first,...)90*a9fa9459Szrj concat_length (const char *first, ...)
91*a9fa9459Szrj {
92*a9fa9459Szrj   unsigned long length;
93*a9fa9459Szrj   va_list args;
94*a9fa9459Szrj 
95*a9fa9459Szrj   va_start (args, first);
96*a9fa9459Szrj   length = vconcat_length (first, args);
97*a9fa9459Szrj   va_end (args);
98*a9fa9459Szrj 
99*a9fa9459Szrj   return length;
100*a9fa9459Szrj }
101*a9fa9459Szrj 
102*a9fa9459Szrj /* @undocumented concat_copy */
103*a9fa9459Szrj 
104*a9fa9459Szrj char *
concat_copy(char * dst,const char * first,...)105*a9fa9459Szrj concat_copy (char *dst, const char *first, ...)
106*a9fa9459Szrj {
107*a9fa9459Szrj   char *save_dst;
108*a9fa9459Szrj   va_list args;
109*a9fa9459Szrj 
110*a9fa9459Szrj   va_start (args, first);
111*a9fa9459Szrj   vconcat_copy (dst, first, args);
112*a9fa9459Szrj   save_dst = dst; /* With K&R C, dst goes out of scope here.  */
113*a9fa9459Szrj   va_end (args);
114*a9fa9459Szrj 
115*a9fa9459Szrj   return save_dst;
116*a9fa9459Szrj }
117*a9fa9459Szrj 
118*a9fa9459Szrj #ifdef __cplusplus
119*a9fa9459Szrj extern "C" {
120*a9fa9459Szrj #endif /* __cplusplus */
121*a9fa9459Szrj char *libiberty_concat_ptr;
122*a9fa9459Szrj #ifdef __cplusplus
123*a9fa9459Szrj }
124*a9fa9459Szrj #endif /* __cplusplus */
125*a9fa9459Szrj 
126*a9fa9459Szrj /* @undocumented concat_copy2 */
127*a9fa9459Szrj 
128*a9fa9459Szrj char *
concat_copy2(const char * first,...)129*a9fa9459Szrj concat_copy2 (const char *first, ...)
130*a9fa9459Szrj {
131*a9fa9459Szrj   va_list args;
132*a9fa9459Szrj   va_start (args, first);
133*a9fa9459Szrj   vconcat_copy (libiberty_concat_ptr, first, args);
134*a9fa9459Szrj   va_end (args);
135*a9fa9459Szrj 
136*a9fa9459Szrj   return libiberty_concat_ptr;
137*a9fa9459Szrj }
138*a9fa9459Szrj 
139*a9fa9459Szrj char *
concat(const char * first,...)140*a9fa9459Szrj concat (const char *first, ...)
141*a9fa9459Szrj {
142*a9fa9459Szrj   char *newstr;
143*a9fa9459Szrj   va_list args;
144*a9fa9459Szrj 
145*a9fa9459Szrj   /* First compute the size of the result and get sufficient memory.  */
146*a9fa9459Szrj   va_start (args, first);
147*a9fa9459Szrj   newstr = XNEWVEC (char, vconcat_length (first, args) + 1);
148*a9fa9459Szrj   va_end (args);
149*a9fa9459Szrj 
150*a9fa9459Szrj   /* Now copy the individual pieces to the result string. */
151*a9fa9459Szrj   va_start (args, first);
152*a9fa9459Szrj   vconcat_copy (newstr, first, args);
153*a9fa9459Szrj   va_end (args);
154*a9fa9459Szrj 
155*a9fa9459Szrj   return newstr;
156*a9fa9459Szrj }
157*a9fa9459Szrj 
158*a9fa9459Szrj /*
159*a9fa9459Szrj 
160*a9fa9459Szrj @deftypefn Extension char* reconcat (char *@var{optr}, const char *@var{s1}, @
161*a9fa9459Szrj   @dots{}, @code{NULL})
162*a9fa9459Szrj 
163*a9fa9459Szrj Same as @code{concat}, except that if @var{optr} is not @code{NULL} it
164*a9fa9459Szrj is freed after the string is created.  This is intended to be useful
165*a9fa9459Szrj when you're extending an existing string or building up a string in a
166*a9fa9459Szrj loop:
167*a9fa9459Szrj 
168*a9fa9459Szrj @example
169*a9fa9459Szrj   str = reconcat (str, "pre-", str, NULL);
170*a9fa9459Szrj @end example
171*a9fa9459Szrj 
172*a9fa9459Szrj @end deftypefn
173*a9fa9459Szrj 
174*a9fa9459Szrj */
175*a9fa9459Szrj 
176*a9fa9459Szrj char *
reconcat(char * optr,const char * first,...)177*a9fa9459Szrj reconcat (char *optr, const char *first, ...)
178*a9fa9459Szrj {
179*a9fa9459Szrj   char *newstr;
180*a9fa9459Szrj   va_list args;
181*a9fa9459Szrj 
182*a9fa9459Szrj   /* First compute the size of the result and get sufficient memory.  */
183*a9fa9459Szrj   va_start (args, first);
184*a9fa9459Szrj   newstr = XNEWVEC (char, vconcat_length (first, args) + 1);
185*a9fa9459Szrj   va_end (args);
186*a9fa9459Szrj 
187*a9fa9459Szrj   /* Now copy the individual pieces to the result string. */
188*a9fa9459Szrj   va_start (args, first);
189*a9fa9459Szrj   vconcat_copy (newstr, first, args);
190*a9fa9459Szrj   if (optr) /* Done before VA_CLOSE so optr stays in scope for K&R C.  */
191*a9fa9459Szrj     free (optr);
192*a9fa9459Szrj   va_end (args);
193*a9fa9459Szrj 
194*a9fa9459Szrj   return newstr;
195*a9fa9459Szrj }
196*a9fa9459Szrj 
197*a9fa9459Szrj #ifdef MAIN
198*a9fa9459Szrj #define NULLP (char *)0
199*a9fa9459Szrj 
200*a9fa9459Szrj /* Simple little test driver. */
201*a9fa9459Szrj 
202*a9fa9459Szrj #include <stdio.h>
203*a9fa9459Szrj 
204*a9fa9459Szrj int
main(void)205*a9fa9459Szrj main (void)
206*a9fa9459Szrj {
207*a9fa9459Szrj   printf ("\"\" = \"%s\"\n", concat (NULLP));
208*a9fa9459Szrj   printf ("\"a\" = \"%s\"\n", concat ("a", NULLP));
209*a9fa9459Szrj   printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP));
210*a9fa9459Szrj   printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP));
211*a9fa9459Szrj   printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP));
212*a9fa9459Szrj   printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP));
213*a9fa9459Szrj   printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP));
214*a9fa9459Szrj   return 0;
215*a9fa9459Szrj }
216*a9fa9459Szrj 
217*a9fa9459Szrj #endif
218