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