Changeset 609 for branches/GNU/src/binutils/libiberty/argv.c
- Timestamp:
- Aug 16, 2003, 6:59:22 PM (22 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GNU/src/binutils/libiberty/argv.c
-
Property cvs2svn:cvs-rev
changed from
1.1
to1.1.1.2
r608 r609 1 1 /* Create and destroy argument vectors (argv's) 2 Copyright (C) 1992 Free Software Foundation, Inc.2 Copyright (C) 1992, 2001 Free Software Foundation, Inc. 3 3 Written by Fred Fish @ Cygnus Support 4 4 … … 30 30 /* Routines imported from standard C runtime libraries. */ 31 31 32 #ifdef __STDC__32 #ifdef ANSI_PROTOTYPES 33 33 34 34 #include <stddef.h> … … 36 36 #include <stdlib.h> 37 37 38 #else /* ! __STDC__*/38 #else /* !ANSI_PROTOTYPES */ 39 39 40 40 #if !defined _WIN32 || defined __GNUC__ … … 47 47 #endif 48 48 49 #endif /* __STDC__ */ 50 51 #include "alloca-conf.h" 49 #endif /* ANSI_PROTOTYPES */ 50 52 51 53 52 #ifndef NULL … … 64 63 /* 65 64 66 NAME 67 68 dupargv -- duplicate an argument vector 69 70 SYNOPSIS 71 72 char **dupargv (vector) 73 char **vector; 74 75 DESCRIPTION 76 77 Duplicate an argument vector. Simply scans through the 78 vector, duplicating each argument until the 79 terminating NULL is found. 80 81 RETURNS 82 83 Returns a pointer to the argument vector if 84 successful. Returns NULL if there is insufficient memory to 85 complete building the argument vector. 65 @deftypefn Extension char** dupargv (char **@var{vector}) 66 67 Duplicate an argument vector. Simply scans through @var{vector}, 68 duplicating each argument until the terminating @code{NULL} is found. 69 Returns a pointer to the argument vector if successful. Returns 70 @code{NULL} if there is insufficient memory to complete building the 71 argument vector. 72 73 @end deftypefn 86 74 87 75 */ … … 121 109 /* 122 110 123 NAME 124 125 freeargv -- free an argument vector 126 127 SYNOPSIS 128 129 void freeargv (vector) 130 char **vector; 131 132 DESCRIPTION 133 134 Free an argument vector that was built using buildargv. Simply scans 135 through the vector, freeing the memory for each argument until the 136 terminating NULL is found, and then frees the vector itself. 137 138 RETURNS 139 140 No value. 111 @deftypefn Extension void freeargv (char **@var{vector}) 112 113 Free an argument vector that was built using @code{buildargv}. Simply 114 scans through @var{vector}, freeing the memory for each argument until 115 the terminating @code{NULL} is found, and then frees @var{vector} 116 itself. 117 118 @end deftypefn 141 119 142 120 */ … … 159 137 /* 160 138 161 NAME 162 163 buildargv -- build an argument vector from a string 164 165 SYNOPSIS 166 167 char **buildargv (sp) 168 char *sp; 169 170 DESCRIPTION 171 172 Given a pointer to a string, parse the string extracting fields 173 separated by whitespace and optionally enclosed within either single 174 or double quotes (which are stripped off), and build a vector of 175 pointers to copies of the string for each field. The input string 176 remains unchanged. 177 178 All of the memory for the pointer array and copies of the string 179 is obtained from malloc. All of the memory can be returned to the 180 system with the single function call freeargv, which takes the 181 returned result of buildargv, as it's argument. 182 183 The memory for the argv array is dynamically expanded as necessary. 184 185 RETURNS 186 187 Returns a pointer to the argument vector if successful. Returns NULL 188 if the input string pointer is NULL or if there is insufficient 189 memory to complete building the argument vector. 190 191 NOTES 192 193 In order to provide a working buffer for extracting arguments into, 194 with appropriate stripping of quotes and translation of backslash 195 sequences, we allocate a working buffer at least as long as the input 196 string. This ensures that we always have enough space in which to 197 work, since the extracted arg is never larger than the input string. 198 199 If the input is a null string (as opposed to a NULL pointer), then 200 buildarg returns an argv that has one arg, a null string. 201 202 Argv is always kept terminated with a NULL arg pointer, so it can 203 be passed to freeargv at any time, or returned, as appropriate. 139 @deftypefn Extension char** buildargv (char *@var{sp}) 140 141 Given a pointer to a string, parse the string extracting fields 142 separated by whitespace and optionally enclosed within either single 143 or double quotes (which are stripped off), and build a vector of 144 pointers to copies of the string for each field. The input string 145 remains unchanged. The last element of the vector is followed by a 146 @code{NULL} element. 147 148 All of the memory for the pointer array and copies of the string 149 is obtained from @code{malloc}. All of the memory can be returned to the 150 system with the single function call @code{freeargv}, which takes the 151 returned result of @code{buildargv}, as it's argument. 152 153 Returns a pointer to the argument vector if successful. Returns 154 @code{NULL} if @var{sp} is @code{NULL} or if there is insufficient 155 memory to complete building the argument vector. 156 157 If the input is a null string (as opposed to a @code{NULL} pointer), 158 then buildarg returns an argument vector that has one arg, a null 159 string. 160 161 @end deftypefn 162 163 The memory for the argv array is dynamically expanded as necessary. 164 165 In order to provide a working buffer for extracting arguments into, 166 with appropriate stripping of quotes and translation of backslash 167 sequences, we allocate a working buffer at least as long as the input 168 string. This ensures that we always have enough space in which to 169 work, since the extracted arg is never larger than the input string. 170 171 The argument vector is always kept terminated with a @code{NULL} arg 172 pointer, so it can be passed to @code{freeargv} at any time, or 173 returned, as appropriate. 174 204 175 */ 205 176 206 177 char **buildargv (input) 207 char *input;178 const char *input; 208 179 { 209 180 char *arg; … … 338 309 /* Simple little test driver. */ 339 310 340 static c har *tests[] =311 static const char *const tests[] = 341 312 { 342 313 "a simple command line", … … 355 326 }; 356 327 357 main ()328 int main () 358 329 { 359 330 char **argv; 360 c har **test;331 const char *const *test; 361 332 char **targs; 362 333 … … 379 350 } 380 351 352 return 0; 381 353 } 382 354 -
Property cvs2svn:cvs-rev
changed from
Note:
See TracChangeset
for help on using the changeset viewer.