merge from gcc
[deliverable/binutils-gdb.git] / libiberty / concat.c
1 /* Concatenate variable number of strings.
2 Copyright (C) 1991, 1994, 2001 Free Software Foundation, Inc.
3 Written by Fred Fish @ Cygnus Support
4
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 /*
23
24 NAME
25
26 concat -- concatenate a variable number of strings
27
28 SYNOPSIS
29
30 #include <varargs.h>
31
32 char *concat (s1, s2, s3, ..., NULL)
33
34 DESCRIPTION
35
36 Concatenate a variable number of strings and return the result
37 in freshly malloc'd memory.
38
39 Returns NULL if insufficient memory is available. The argument
40 list is terminated by the first NULL pointer encountered. Pointers
41 to empty strings are ignored.
42
43 NOTES
44
45 This function uses xmalloc() which is expected to be a front end
46 function to malloc() that deals with low memory situations. In
47 typical use, if malloc() returns NULL then xmalloc() diverts to an
48 error handler routine which never returns, and thus xmalloc will
49 never return a NULL pointer. If the client application wishes to
50 deal with low memory situations itself, it should supply an xmalloc
51 that just directly invokes malloc and blindly returns whatever
52 malloc returns.
53 */
54
55
56 #ifdef HAVE_CONFIG_H
57 #include "config.h"
58 #endif
59 #include "ansidecl.h"
60 #include "libiberty.h"
61 #include <sys/types.h> /* size_t */
62
63 #ifdef ANSI_PROTOTYPES
64 #include <stdarg.h>
65 #else
66 #include <varargs.h>
67 #endif
68
69 # if HAVE_STRING_H
70 # include <string.h>
71 # else
72 # if HAVE_STRINGS_H
73 # include <strings.h>
74 # endif
75 # endif
76
77 #if HAVE_STDLIB_H
78 #include <stdlib.h>
79 #endif
80
81 static inline unsigned long vconcat_length PARAMS ((const char *, va_list));
82 static inline unsigned long
83 vconcat_length (first, args)
84 const char *first;
85 va_list args;
86 {
87 unsigned long length = 0;
88 const char *arg;
89
90 for (arg = first; arg ; arg = va_arg (args, const char *))
91 length += strlen (arg);
92
93 return length;
94 }
95
96 static inline char *vconcat_copy PARAMS ((char *, const char *, va_list));
97 static inline char *
98 vconcat_copy (dst, first, args)
99 char *dst;
100 const char *first;
101 va_list args;
102 {
103 char *end = dst;
104 const char *arg;
105
106 for (arg = first; arg ; arg = va_arg (args, const char *))
107 {
108 unsigned long length = strlen (arg);
109 memcpy (end, arg, length);
110 end += length;
111 }
112 *end = '\000';
113
114 return dst;
115 }
116
117 unsigned long
118 concat_length VPARAMS ((const char *first, ...))
119 {
120 unsigned long length;
121
122 VA_OPEN (args, first);
123 VA_FIXEDARG (args, const char *, first);
124 length = vconcat_length (first, args);
125 VA_CLOSE (args);
126
127 return length;
128 }
129
130 char *
131 concat_copy VPARAMS ((char *dst, const char *first, ...))
132 {
133 char *save_dst;
134
135 VA_OPEN (args, first);
136 VA_FIXEDARG (args, char *, dst);
137 VA_FIXEDARG (args, const char *, first);
138 vconcat_copy (dst, first, args);
139 save_dst = dst; /* With K&R C, dst goes out of scope here. */
140 VA_CLOSE (args);
141
142 return save_dst;
143 }
144
145 char *libiberty_concat_ptr;
146
147 char *
148 concat_copy2 VPARAMS ((const char *first, ...))
149 {
150 VA_OPEN (args, first);
151 VA_FIXEDARG (args, const char *, first);
152 vconcat_copy (libiberty_concat_ptr, first, args);
153 VA_CLOSE (args);
154
155 return libiberty_concat_ptr;
156 }
157
158 char *
159 concat VPARAMS ((const char *first, ...))
160 {
161 char *newstr;
162
163 /* First compute the size of the result and get sufficient memory. */
164 VA_OPEN (args, first);
165 VA_FIXEDARG (args, const char *, first);
166 newstr = (char *) xmalloc (vconcat_length (first, args) + 1);
167 VA_CLOSE (args);
168
169 /* Now copy the individual pieces to the result string. */
170 VA_OPEN (args, first);
171 VA_FIXEDARG (args, const char *, first);
172 vconcat_copy (newstr, first, args);
173 VA_CLOSE (args);
174
175 return newstr;
176 }
177
178 char *
179 reconcat VPARAMS ((char *optr, const char *first, ...))
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);
187 newstr = (char *) xmalloc (vconcat_length (first, args) + 1);
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);
195 if (optr) /* Done before VA_CLOSE so optr stays in scope for K&R C. */
196 free (optr);
197 VA_CLOSE (args);
198
199 return newstr;
200 }
201
202 #ifdef MAIN
203 #define NULLP (char *)0
204
205 /* Simple little test driver. */
206
207 #include <stdio.h>
208
209 int
210 main ()
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
This page took 0.035223 seconds and 5 git commands to generate.