*** empty log message ***
[deliverable/binutils-gdb.git] / libiberty / concat.c
CommitLineData
252b5132 1/* Concatenate variable number of strings.
d42dae6c 2 Copyright (C) 1991, 1994, 2001 Free Software Foundation, Inc.
252b5132
RH
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
18not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
20
21
22/*
23
24NAME
25
26 concat -- concatenate a variable number of strings
27
28SYNOPSIS
29
30 #include <varargs.h>
31
32 char *concat (s1, s2, s3, ..., NULL)
33
34DESCRIPTION
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
43NOTES
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
65a4f13c
DD
56#ifdef HAVE_CONFIG_H
57#include "config.h"
58#endif
252b5132
RH
59#include "ansidecl.h"
60#include "libiberty.h"
926150e2 61#include <sys/types.h> /* size_t */
252b5132
RH
62
63#ifdef ANSI_PROTOTYPES
64#include <stdarg.h>
65#else
66#include <varargs.h>
67#endif
68
d42dae6c
DD
69# if HAVE_STRING_H
70# include <string.h>
71# else
72# if HAVE_STRINGS_H
73# include <strings.h>
74# endif
75# endif
252b5132 76
54c20242
DD
77static inline unsigned long vconcat_length PARAMS ((const char *, va_list));
78static inline unsigned long
79vconcat_length (first, args)
80 const char *first;
81 va_list args;
252b5132 82{
54c20242
DD
83 unsigned long length = 0;
84 const char *arg;
252b5132 85
d42dae6c
DD
86 for (arg = first; arg ; arg = va_arg (args, const char *))
87 length += strlen (arg);
88
54c20242
DD
89 return length;
90}
252b5132 91
54c20242
DD
92static inline char *vconcat_copy PARAMS ((char *, const char *, va_list));
93static inline char *
94vconcat_copy (dst, first, args)
95 char *dst;
96 const char *first;
97 va_list args;
98{
99 char *end = dst;
100 const char *arg;
d42dae6c 101
d42dae6c
DD
102 for (arg = first; arg ; arg = va_arg (args, const char *))
103 {
54c20242 104 unsigned long length = strlen (arg);
d42dae6c
DD
105 memcpy (end, arg, length);
106 end += length;
252b5132 107 }
d42dae6c 108 *end = '\000';
54c20242
DD
109
110 return dst;
111}
112
113unsigned long
114concat_length VPARAMS ((const char *first, ...))
115{
116 unsigned long length;
117
118 VA_OPEN (args, first);
119 VA_FIXEDARG (args, const char *, first);
120 length = vconcat_length (first, args);
121 VA_CLOSE (args);
122
123 return length;
124}
125
126char *
127concat_copy VPARAMS ((char *dst, const char *first, ...))
128{
129 char *save_dst;
130
131 VA_OPEN (args, first);
132 VA_FIXEDARG (args, char *, dst);
133 VA_FIXEDARG (args, const char *, first);
134 vconcat_copy (dst, first, args);
135 save_dst = dst; /* With K&R C, dst goes out of scope here. */
136 VA_CLOSE (args);
137
138 return save_dst;
139}
140
141char *libiberty_concat_ptr;
142
143char *
144concat_copy2 VPARAMS ((const char *first, ...))
145{
146 VA_OPEN (args, first);
147 VA_FIXEDARG (args, const char *, first);
148 vconcat_copy (libiberty_concat_ptr, first, args);
149 VA_CLOSE (args);
150
151 return libiberty_concat_ptr;
152}
153
154char *
155concat VPARAMS ((const char *first, ...))
156{
157 char *newstr;
158
159 /* First compute the size of the result and get sufficient memory. */
160 VA_OPEN (args, first);
161 VA_FIXEDARG (args, const char *, first);
162 newstr = (char *) xmalloc (vconcat_length (first, args) + 1);
163 VA_CLOSE (args);
164
165 /* Now copy the individual pieces to the result string. */
166 VA_OPEN (args, first);
167 VA_FIXEDARG (args, const char *, first);
168 vconcat_copy (newstr, first, args);
8a423cb3 169 VA_CLOSE (args);
252b5132 170
d42dae6c 171 return newstr;
252b5132
RH
172}
173
99ee3a8f
DD
174char *
175reconcat VPARAMS ((char *optr, const char *first, ...))
176{
177 char *newstr;
178
179 /* First compute the size of the result and get sufficient memory. */
180 VA_OPEN (args, first);
181 VA_FIXEDARG (args, char *, optr);
182 VA_FIXEDARG (args, const char *, first);
183 newstr = (char *) xmalloc (vconcat_length (first, args) + 1);
184 VA_CLOSE (args);
185
186 /* Now copy the individual pieces to the result string. */
187 VA_OPEN (args, first);
188 VA_FIXEDARG (args, char *, optr);
189 VA_FIXEDARG (args, const char *, first);
190 vconcat_copy (newstr, first, args);
66c94e19 191 if (optr) /* Done before VA_CLOSE so optr stays in scope for K&R C. */
99ee3a8f 192 free (optr);
66c94e19 193 VA_CLOSE (args);
99ee3a8f
DD
194
195 return newstr;
196}
197
252b5132 198#ifdef MAIN
d42dae6c 199#define NULLP (char *)0
252b5132
RH
200
201/* Simple little test driver. */
202
203#include <stdio.h>
204
205int
206main ()
207{
208 printf ("\"\" = \"%s\"\n", concat (NULLP));
209 printf ("\"a\" = \"%s\"\n", concat ("a", NULLP));
210 printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP));
211 printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP));
212 printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP));
213 printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP));
214 printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP));
215 return 0;
216}
217
218#endif
This page took 0.093756 seconds and 4 git commands to generate.