Changeset 609 for branches/GNU/src/binutils/libiberty/concat.c
- Timestamp:
- Aug 16, 2003, 6:59:22 PM (22 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GNU/src/binutils/libiberty/concat.c
-
Property cvs2svn:cvs-rev
changed from
1.1
to1.1.1.2
r608 r609 1 1 /* Concatenate variable number of strings. 2 Copyright (C) 1991, 1994 Free Software Foundation, Inc.2 Copyright (C) 1991, 1994, 2001 Free Software Foundation, Inc. 3 3 Written by Fred Fish @ Cygnus Support 4 4 … … 22 22 /* 23 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. 24 @deftypefn Extension char* concat (const char *@var{s1}, const char *@var{s2}, @dots{}, @code{NULL}) 25 26 Concatenate zero or more of strings and return the result in freshly 27 @code{xmalloc}ed memory. Returns @code{NULL} if insufficient memory is 28 available. The argument list is terminated by the first @code{NULL} 29 pointer encountered. Pointers to empty strings are ignored. 30 31 @end deftypefn 42 32 43 33 NOTES … … 51 41 that just directly invokes malloc and blindly returns whatever 52 42 malloc returns. 43 53 44 */ 54 45 55 46 47 #ifdef HAVE_CONFIG_H 48 #include "config.h" 49 #endif 56 50 #include "ansidecl.h" 57 51 #include "libiberty.h" 52 #include <sys/types.h> /* size_t */ 58 53 59 54 #ifdef ANSI_PROTOTYPES … … 63 58 #endif 64 59 65 #ifdef __STDC__ 66 #include <stddef.h> 67 extern size_t strlen (const char *s); 68 #else 69 extern int strlen (); 70 #endif 71 60 # if HAVE_STRING_H 61 # include <string.h> 62 # else 63 # if HAVE_STRINGS_H 64 # include <strings.h> 65 # endif 66 # endif 67 68 #if HAVE_STDLIB_H 69 #include <stdlib.h> 70 #endif 71 72 static inline unsigned long vconcat_length PARAMS ((const char *, va_list)); 73 static inline unsigned long 74 vconcat_length (first, args) 75 const char *first; 76 va_list args; 77 { 78 unsigned long length = 0; 79 const char *arg; 80 81 for (arg = first; arg ; arg = va_arg (args, const char *)) 82 length += strlen (arg); 83 84 return length; 85 } 86 87 static inline char *vconcat_copy PARAMS ((char *, const char *, va_list)); 88 static inline char * 89 vconcat_copy (dst, first, args) 90 char *dst; 91 const char *first; 92 va_list args; 93 { 94 char *end = dst; 95 const char *arg; 96 97 for (arg = first; arg ; arg = va_arg (args, const char *)) 98 { 99 unsigned long length = strlen (arg); 100 memcpy (end, arg, length); 101 end += length; 102 } 103 *end = '\000'; 104 105 return dst; 106 } 107 108 /* @undocumented concat_length */ 109 110 unsigned long 111 concat_length VPARAMS ((const char *first, ...)) 112 { 113 unsigned long length; 114 115 VA_OPEN (args, first); 116 VA_FIXEDARG (args, const char *, first); 117 length = vconcat_length (first, args); 118 VA_CLOSE (args); 119 120 return length; 121 } 122 123 /* @undocumented concat_copy */ 124 125 char * 126 concat_copy VPARAMS ((char *dst, const char *first, ...)) 127 { 128 char *save_dst; 129 130 VA_OPEN (args, first); 131 VA_FIXEDARG (args, char *, dst); 132 VA_FIXEDARG (args, const char *, first); 133 vconcat_copy (dst, first, args); 134 save_dst = dst; /* With K&R C, dst goes out of scope here. */ 135 VA_CLOSE (args); 136 137 return save_dst; 138 } 139 140 char *libiberty_concat_ptr; 141 142 /* @undocumented concat_copy2 */ 143 144 char * 145 concat_copy2 VPARAMS ((const char *first, ...)) 146 { 147 VA_OPEN (args, first); 148 VA_FIXEDARG (args, const char *, first); 149 vconcat_copy (libiberty_concat_ptr, first, args); 150 VA_CLOSE (args); 151 152 return libiberty_concat_ptr; 153 } 154 155 char * 156 concat VPARAMS ((const char *first, ...)) 157 { 158 char *newstr; 159 160 /* First compute the size of the result and get sufficient memory. */ 161 VA_OPEN (args, first); 162 VA_FIXEDARG (args, const char *, first); 163 newstr = (char *) xmalloc (vconcat_length (first, args) + 1); 164 VA_CLOSE (args); 165 166 /* Now copy the individual pieces to the result string. */ 167 VA_OPEN (args, first); 168 VA_FIXEDARG (args, const char *, first); 169 vconcat_copy (newstr, first, args); 170 VA_CLOSE (args); 171 172 return newstr; 173 } 174 175 /* 176 177 @deftypefn Extension char* reconcat (char *@var{optr}, const char *@var{s1}, @dots{}, @code{NULL}) 178 179 Same as @code{concat}, except that if @var{optr} is not @code{NULL} it 180 is freed after the string is created. This is intended to be useful 181 when you're extending an existing string or building up a string in a 182 loop: 183 184 @example 185 str = reconcat (str, "pre-", str, NULL); 186 @end example 187 188 @end deftypefn 189 190 */ 191 192 char * 193 reconcat VPARAMS ((char *optr, const char *first, ...)) 194 { 195 char *newstr; 196 197 /* First compute the size of the result and get sufficient memory. */ 198 VA_OPEN (args, first); 199 VA_FIXEDARG (args, char *, optr); 200 VA_FIXEDARG (args, const char *, first); 201 newstr = (char *) xmalloc (vconcat_length (first, args) + 1); 202 VA_CLOSE (args); 203 204 /* Now copy the individual pieces to the result string. */ 205 VA_OPEN (args, first); 206 VA_FIXEDARG (args, char *, optr); 207 VA_FIXEDARG (args, const char *, first); 208 vconcat_copy (newstr, first, args); 209 if (optr) /* Done before VA_CLOSE so optr stays in scope for K&R C. */ 210 free (optr); 211 VA_CLOSE (args); 212 213 return newstr; 214 } 215 216 #ifdef MAIN 72 217 #define NULLP (char *)0 73 74 /* VARARGS */75 #ifdef ANSI_PROTOTYPES76 char *77 concat (const char *first, ...)78 #else79 char *80 concat (va_alist)81 va_dcl82 #endif83 {84 register int length;85 register char *newstr;86 register char *end;87 register const char *arg;88 va_list args;89 #ifndef ANSI_PROTOTYPES90 const char *first;91 #endif92 93 /* First compute the size of the result and get sufficient memory. */94 95 #ifdef ANSI_PROTOTYPES96 va_start (args, first);97 #else98 va_start (args);99 first = va_arg (args, const char *);100 #endif101 102 if (first == NULLP)103 length = 0;104 else105 {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_PROTOTYPES120 va_start (args, first);121 #else122 va_start (args);123 first = va_arg (args, const char *);124 #endif125 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 MAIN149 218 150 219 /* Simple little test driver. */ -
Property cvs2svn:cvs-rev
changed from
Note:
See TracChangeset
for help on using the changeset viewer.