comparison libiberty/concat.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents f6334be47118
children 84e7813d76e9
comparison
equal deleted inserted replaced
68:561a7518be6b 111:04ced10e8804
1 /* Concatenate variable number of strings. 1 /* Concatenate variable number of strings.
2 Copyright (C) 1991, 1994, 2001, 2011 Free Software Foundation, Inc. 2 Copyright (C) 1991-2017 Free Software Foundation, Inc.
3 Written by Fred Fish @ Cygnus Support 3 Written by Fred Fish @ Cygnus Support
4 4
5 This file is part of the libiberty library. 5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or 6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public 7 modify it under the terms of the GNU Library General Public
23 23
24 @deftypefn Extension char* concat (const char *@var{s1}, const char *@var{s2}, @ 24 @deftypefn Extension char* concat (const char *@var{s1}, const char *@var{s2}, @
25 @dots{}, @code{NULL}) 25 @dots{}, @code{NULL})
26 26
27 Concatenate zero or more of strings and return the result in freshly 27 Concatenate zero or more of strings and return the result in freshly
28 @code{xmalloc}ed memory. Returns @code{NULL} if insufficient memory is 28 @code{xmalloc}ed memory. The argument list is terminated by the first
29 available. The argument list is terminated by the first @code{NULL} 29 @code{NULL} pointer encountered. Pointers to empty strings are ignored.
30 pointer encountered. Pointers to empty strings are ignored.
31 30
32 @end deftypefn 31 @end deftypefn
33
34 NOTES
35
36 This function uses xmalloc() which is expected to be a front end
37 function to malloc() that deals with low memory situations. In
38 typical use, if malloc() returns NULL then xmalloc() diverts to an
39 error handler routine which never returns, and thus xmalloc will
40 never return a NULL pointer. If the client application wishes to
41 deal with low memory situations itself, it should supply an xmalloc
42 that just directly invokes malloc and blindly returns whatever
43 malloc returns.
44 32
45 */ 33 */
46 34
47 35
48 #ifdef HAVE_CONFIG_H 36 #ifdef HAVE_CONFIG_H
100 88
101 unsigned long 89 unsigned long
102 concat_length (const char *first, ...) 90 concat_length (const char *first, ...)
103 { 91 {
104 unsigned long length; 92 unsigned long length;
105 93 va_list args;
106 VA_OPEN (args, first); 94
107 VA_FIXEDARG (args, const char *, first); 95 va_start (args, first);
108 length = vconcat_length (first, args); 96 length = vconcat_length (first, args);
109 VA_CLOSE (args); 97 va_end (args);
110 98
111 return length; 99 return length;
112 } 100 }
113 101
114 /* @undocumented concat_copy */ 102 /* @undocumented concat_copy */
115 103
116 char * 104 char *
117 concat_copy (char *dst, const char *first, ...) 105 concat_copy (char *dst, const char *first, ...)
118 { 106 {
119 char *save_dst; 107 char *save_dst;
120 108 va_list args;
121 VA_OPEN (args, first); 109
122 VA_FIXEDARG (args, char *, dst); 110 va_start (args, first);
123 VA_FIXEDARG (args, const char *, first);
124 vconcat_copy (dst, first, args); 111 vconcat_copy (dst, first, args);
125 save_dst = dst; /* With K&R C, dst goes out of scope here. */ 112 save_dst = dst; /* With K&R C, dst goes out of scope here. */
126 VA_CLOSE (args); 113 va_end (args);
127 114
128 return save_dst; 115 return save_dst;
129 } 116 }
130 117
131 #ifdef __cplusplus 118 #ifdef __cplusplus
139 /* @undocumented concat_copy2 */ 126 /* @undocumented concat_copy2 */
140 127
141 char * 128 char *
142 concat_copy2 (const char *first, ...) 129 concat_copy2 (const char *first, ...)
143 { 130 {
144 VA_OPEN (args, first); 131 va_list args;
145 VA_FIXEDARG (args, const char *, first); 132 va_start (args, first);
146 vconcat_copy (libiberty_concat_ptr, first, args); 133 vconcat_copy (libiberty_concat_ptr, first, args);
147 VA_CLOSE (args); 134 va_end (args);
148 135
149 return libiberty_concat_ptr; 136 return libiberty_concat_ptr;
150 } 137 }
151 138
152 char * 139 char *
153 concat (const char *first, ...) 140 concat (const char *first, ...)
154 { 141 {
155 char *newstr; 142 char *newstr;
143 va_list args;
156 144
157 /* First compute the size of the result and get sufficient memory. */ 145 /* First compute the size of the result and get sufficient memory. */
158 VA_OPEN (args, first); 146 va_start (args, first);
159 VA_FIXEDARG (args, const char *, first);
160 newstr = XNEWVEC (char, vconcat_length (first, args) + 1); 147 newstr = XNEWVEC (char, vconcat_length (first, args) + 1);
161 VA_CLOSE (args); 148 va_end (args);
162 149
163 /* Now copy the individual pieces to the result string. */ 150 /* Now copy the individual pieces to the result string. */
164 VA_OPEN (args, first); 151 va_start (args, first);
165 VA_FIXEDARG (args, const char *, first);
166 vconcat_copy (newstr, first, args); 152 vconcat_copy (newstr, first, args);
167 VA_CLOSE (args); 153 va_end (args);
168 154
169 return newstr; 155 return newstr;
170 } 156 }
171 157
172 /* 158 /*
189 175
190 char * 176 char *
191 reconcat (char *optr, const char *first, ...) 177 reconcat (char *optr, const char *first, ...)
192 { 178 {
193 char *newstr; 179 char *newstr;
180 va_list args;
194 181
195 /* First compute the size of the result and get sufficient memory. */ 182 /* First compute the size of the result and get sufficient memory. */
196 VA_OPEN (args, first); 183 va_start (args, first);
197 VA_FIXEDARG (args, char *, optr);
198 VA_FIXEDARG (args, const char *, first);
199 newstr = XNEWVEC (char, vconcat_length (first, args) + 1); 184 newstr = XNEWVEC (char, vconcat_length (first, args) + 1);
200 VA_CLOSE (args); 185 va_end (args);
201 186
202 /* Now copy the individual pieces to the result string. */ 187 /* Now copy the individual pieces to the result string. */
203 VA_OPEN (args, first); 188 va_start (args, first);
204 VA_FIXEDARG (args, char *, optr);
205 VA_FIXEDARG (args, const char *, first);
206 vconcat_copy (newstr, first, args); 189 vconcat_copy (newstr, first, args);
207 if (optr) /* Done before VA_CLOSE so optr stays in scope for K&R C. */ 190 if (optr) /* Done before VA_CLOSE so optr stays in scope for K&R C. */
208 free (optr); 191 free (optr);
209 VA_CLOSE (args); 192 va_end (args);
210 193
211 return newstr; 194 return newstr;
212 } 195 }
213 196
214 #ifdef MAIN 197 #ifdef MAIN