1 | /* Concatenate variable number of strings.
|
---|
2 | Copyright (C) 1991, 1994 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 | #include "ansidecl.h"
|
---|
57 | #include "libiberty.h"
|
---|
58 |
|
---|
59 | #ifdef ANSI_PROTOTYPES
|
---|
60 | #include <stdarg.h>
|
---|
61 | #else
|
---|
62 | #include <varargs.h>
|
---|
63 | #endif
|
---|
64 |
|
---|
65 | #ifdef __STDC__
|
---|
66 | #include <stddef.h>
|
---|
67 | extern size_t strlen (const char *s);
|
---|
68 | #else
|
---|
69 | extern int strlen ();
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | #define NULLP (char *)0
|
---|
73 |
|
---|
74 | /* VARARGS */
|
---|
75 | #ifdef ANSI_PROTOTYPES
|
---|
76 | char *
|
---|
77 | concat (const char *first, ...)
|
---|
78 | #else
|
---|
79 | char *
|
---|
80 | concat (va_alist)
|
---|
81 | va_dcl
|
---|
82 | #endif
|
---|
83 | {
|
---|
84 | register int length;
|
---|
85 | register char *newstr;
|
---|
86 | register char *end;
|
---|
87 | register const char *arg;
|
---|
88 | va_list args;
|
---|
89 | #ifndef ANSI_PROTOTYPES
|
---|
90 | const char *first;
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | /* First compute the size of the result and get sufficient memory. */
|
---|
94 |
|
---|
95 | #ifdef ANSI_PROTOTYPES
|
---|
96 | va_start (args, first);
|
---|
97 | #else
|
---|
98 | va_start (args);
|
---|
99 | first = va_arg (args, const char *);
|
---|
100 | #endif
|
---|
101 |
|
---|
102 | if (first == NULLP)
|
---|
103 | length = 0;
|
---|
104 | else
|
---|
105 | {
|
---|
106 | length = strlen (first);
|
---|
107 | while ((arg = va_arg (args, const char *)) != NULLP)
|
---|
108 | {
|
---|
109 | length += strlen (arg);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | newstr = (char *) xmalloc (length + 1);
|
---|
113 | va_end (args);
|
---|
114 |
|
---|
115 | /* Now copy the individual pieces to the result string. */
|
---|
116 |
|
---|
117 | if (newstr != NULLP)
|
---|
118 | {
|
---|
119 | #ifdef ANSI_PROTOTYPES
|
---|
120 | va_start (args, first);
|
---|
121 | #else
|
---|
122 | va_start (args);
|
---|
123 | first = va_arg (args, const char *);
|
---|
124 | #endif
|
---|
125 | end = newstr;
|
---|
126 | if (first != NULLP)
|
---|
127 | {
|
---|
128 | arg = first;
|
---|
129 | while (*arg)
|
---|
130 | {
|
---|
131 | *end++ = *arg++;
|
---|
132 | }
|
---|
133 | while ((arg = va_arg (args, const char *)) != NULLP)
|
---|
134 | {
|
---|
135 | while (*arg)
|
---|
136 | {
|
---|
137 | *end++ = *arg++;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | *end = '\000';
|
---|
142 | va_end (args);
|
---|
143 | }
|
---|
144 |
|
---|
145 | return (newstr);
|
---|
146 | }
|
---|
147 |
|
---|
148 | #ifdef MAIN
|
---|
149 |
|
---|
150 | /* Simple little test driver. */
|
---|
151 |
|
---|
152 | #include <stdio.h>
|
---|
153 |
|
---|
154 | int
|
---|
155 | main ()
|
---|
156 | {
|
---|
157 | printf ("\"\" = \"%s\"\n", concat (NULLP));
|
---|
158 | printf ("\"a\" = \"%s\"\n", concat ("a", NULLP));
|
---|
159 | printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP));
|
---|
160 | printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP));
|
---|
161 | printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP));
|
---|
162 | printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP));
|
---|
163 | printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP));
|
---|
164 | return 0;
|
---|
165 | }
|
---|
166 |
|
---|
167 | #endif
|
---|