Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
d42dae6c DD |
65 | # if HAVE_STRING_H |
66 | # include <string.h> | |
67 | # else | |
68 | # if HAVE_STRINGS_H | |
69 | # include <strings.h> | |
70 | # endif | |
71 | # endif | |
252b5132 RH |
72 | |
73 | /* VARARGS */ | |
74 | #ifdef ANSI_PROTOTYPES | |
75 | char * | |
76 | concat (const char *first, ...) | |
77 | #else | |
78 | char * | |
79 | concat (va_alist) | |
80 | va_dcl | |
81 | #endif | |
82 | { | |
d42dae6c | 83 | register size_t length; |
252b5132 RH |
84 | register char *newstr; |
85 | register char *end; | |
86 | register const char *arg; | |
87 | va_list args; | |
88 | #ifndef ANSI_PROTOTYPES | |
89 | const char *first; | |
90 | #endif | |
91 | ||
d42dae6c | 92 | /* First compute the size of the result and get sufficient memory. */ |
252b5132 RH |
93 | #ifdef ANSI_PROTOTYPES |
94 | va_start (args, first); | |
95 | #else | |
96 | va_start (args); | |
97 | first = va_arg (args, const char *); | |
98 | #endif | |
99 | ||
d42dae6c DD |
100 | length = 0; |
101 | for (arg = first; arg ; arg = va_arg (args, const char *)) | |
102 | length += strlen (arg); | |
103 | ||
252b5132 RH |
104 | va_end (args); |
105 | ||
d42dae6c | 106 | newstr = (char *) xmalloc (length + 1); |
252b5132 | 107 | |
d42dae6c | 108 | /* Now copy the individual pieces to the result string. */ |
252b5132 | 109 | #ifdef ANSI_PROTOTYPES |
d42dae6c | 110 | va_start (args, first); |
252b5132 | 111 | #else |
d42dae6c DD |
112 | va_start (args); |
113 | first = va_arg (args, const char *); | |
252b5132 | 114 | #endif |
d42dae6c DD |
115 | |
116 | end = newstr; | |
117 | for (arg = first; arg ; arg = va_arg (args, const char *)) | |
118 | { | |
119 | length = strlen (arg); | |
120 | memcpy (end, arg, length); | |
121 | end += length; | |
252b5132 | 122 | } |
d42dae6c DD |
123 | *end = '\000'; |
124 | va_end (args); | |
252b5132 | 125 | |
d42dae6c | 126 | return newstr; |
252b5132 RH |
127 | } |
128 | ||
129 | #ifdef MAIN | |
d42dae6c | 130 | #define NULLP (char *)0 |
252b5132 RH |
131 | |
132 | /* Simple little test driver. */ | |
133 | ||
134 | #include <stdio.h> | |
135 | ||
136 | int | |
137 | main () | |
138 | { | |
139 | printf ("\"\" = \"%s\"\n", concat (NULLP)); | |
140 | printf ("\"a\" = \"%s\"\n", concat ("a", NULLP)); | |
141 | printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP)); | |
142 | printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP)); | |
143 | printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP)); | |
144 | printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP)); | |
145 | printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP)); | |
146 | return 0; | |
147 | } | |
148 | ||
149 | #endif |