1*38fd1498Szrj /* Create and destroy argument vectors (argv's)
2*38fd1498Szrj Copyright (C) 1992-2018 Free Software Foundation, Inc.
3*38fd1498Szrj Written by Fred Fish @ Cygnus Support
4*38fd1498Szrj
5*38fd1498Szrj This file is part of the libiberty library.
6*38fd1498Szrj Libiberty is free software; you can redistribute it and/or
7*38fd1498Szrj modify it under the terms of the GNU Library General Public
8*38fd1498Szrj License as published by the Free Software Foundation; either
9*38fd1498Szrj version 2 of the License, or (at your option) any later version.
10*38fd1498Szrj
11*38fd1498Szrj Libiberty is distributed in the hope that it will be useful,
12*38fd1498Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14*38fd1498Szrj Library General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj You should have received a copy of the GNU Library General Public
17*38fd1498Szrj License along with libiberty; see the file COPYING.LIB. If
18*38fd1498Szrj not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19*38fd1498Szrj Boston, MA 02110-1301, USA. */
20*38fd1498Szrj
21*38fd1498Szrj
22*38fd1498Szrj /* Create and destroy argument vectors. An argument vector is simply an
23*38fd1498Szrj array of string pointers, terminated by a NULL pointer. */
24*38fd1498Szrj
25*38fd1498Szrj #ifdef HAVE_CONFIG_H
26*38fd1498Szrj #include "config.h"
27*38fd1498Szrj #endif
28*38fd1498Szrj #include "ansidecl.h"
29*38fd1498Szrj #include "libiberty.h"
30*38fd1498Szrj #include "safe-ctype.h"
31*38fd1498Szrj
32*38fd1498Szrj /* Routines imported from standard C runtime libraries. */
33*38fd1498Szrj
34*38fd1498Szrj #include <stddef.h>
35*38fd1498Szrj #include <string.h>
36*38fd1498Szrj #include <stdlib.h>
37*38fd1498Szrj #include <stdio.h>
38*38fd1498Szrj #include <sys/types.h>
39*38fd1498Szrj #ifdef HAVE_UNISTD_H
40*38fd1498Szrj #include <unistd.h>
41*38fd1498Szrj #endif
42*38fd1498Szrj #if HAVE_SYS_STAT_H
43*38fd1498Szrj #include <sys/stat.h>
44*38fd1498Szrj #endif
45*38fd1498Szrj
46*38fd1498Szrj #ifndef NULL
47*38fd1498Szrj #define NULL 0
48*38fd1498Szrj #endif
49*38fd1498Szrj
50*38fd1498Szrj #ifndef EOS
51*38fd1498Szrj #define EOS '\0'
52*38fd1498Szrj #endif
53*38fd1498Szrj
54*38fd1498Szrj #define INITIAL_MAXARGC 8 /* Number of args + NULL in initial argv */
55*38fd1498Szrj
56*38fd1498Szrj
57*38fd1498Szrj /*
58*38fd1498Szrj
59*38fd1498Szrj @deftypefn Extension char** dupargv (char * const *@var{vector})
60*38fd1498Szrj
61*38fd1498Szrj Duplicate an argument vector. Simply scans through @var{vector},
62*38fd1498Szrj duplicating each argument until the terminating @code{NULL} is found.
63*38fd1498Szrj Returns a pointer to the argument vector if successful. Returns
64*38fd1498Szrj @code{NULL} if there is insufficient memory to complete building the
65*38fd1498Szrj argument vector.
66*38fd1498Szrj
67*38fd1498Szrj @end deftypefn
68*38fd1498Szrj
69*38fd1498Szrj */
70*38fd1498Szrj
71*38fd1498Szrj char **
dupargv(char * const * argv)72*38fd1498Szrj dupargv (char * const *argv)
73*38fd1498Szrj {
74*38fd1498Szrj int argc;
75*38fd1498Szrj char **copy;
76*38fd1498Szrj
77*38fd1498Szrj if (argv == NULL)
78*38fd1498Szrj return NULL;
79*38fd1498Szrj
80*38fd1498Szrj /* the vector */
81*38fd1498Szrj for (argc = 0; argv[argc] != NULL; argc++);
82*38fd1498Szrj copy = (char **) xmalloc ((argc + 1) * sizeof (char *));
83*38fd1498Szrj
84*38fd1498Szrj /* the strings */
85*38fd1498Szrj for (argc = 0; argv[argc] != NULL; argc++)
86*38fd1498Szrj copy[argc] = xstrdup (argv[argc]);
87*38fd1498Szrj copy[argc] = NULL;
88*38fd1498Szrj return copy;
89*38fd1498Szrj }
90*38fd1498Szrj
91*38fd1498Szrj /*
92*38fd1498Szrj
93*38fd1498Szrj @deftypefn Extension void freeargv (char **@var{vector})
94*38fd1498Szrj
95*38fd1498Szrj Free an argument vector that was built using @code{buildargv}. Simply
96*38fd1498Szrj scans through @var{vector}, freeing the memory for each argument until
97*38fd1498Szrj the terminating @code{NULL} is found, and then frees @var{vector}
98*38fd1498Szrj itself.
99*38fd1498Szrj
100*38fd1498Szrj @end deftypefn
101*38fd1498Szrj
102*38fd1498Szrj */
103*38fd1498Szrj
freeargv(char ** vector)104*38fd1498Szrj void freeargv (char **vector)
105*38fd1498Szrj {
106*38fd1498Szrj register char **scan;
107*38fd1498Szrj
108*38fd1498Szrj if (vector != NULL)
109*38fd1498Szrj {
110*38fd1498Szrj for (scan = vector; *scan != NULL; scan++)
111*38fd1498Szrj {
112*38fd1498Szrj free (*scan);
113*38fd1498Szrj }
114*38fd1498Szrj free (vector);
115*38fd1498Szrj }
116*38fd1498Szrj }
117*38fd1498Szrj
118*38fd1498Szrj static void
consume_whitespace(const char ** input)119*38fd1498Szrj consume_whitespace (const char **input)
120*38fd1498Szrj {
121*38fd1498Szrj while (ISSPACE (**input))
122*38fd1498Szrj {
123*38fd1498Szrj (*input)++;
124*38fd1498Szrj }
125*38fd1498Szrj }
126*38fd1498Szrj
127*38fd1498Szrj static int
only_whitespace(const char * input)128*38fd1498Szrj only_whitespace (const char* input)
129*38fd1498Szrj {
130*38fd1498Szrj while (*input != EOS && ISSPACE (*input))
131*38fd1498Szrj input++;
132*38fd1498Szrj
133*38fd1498Szrj return (*input == EOS);
134*38fd1498Szrj }
135*38fd1498Szrj
136*38fd1498Szrj /*
137*38fd1498Szrj
138*38fd1498Szrj @deftypefn Extension char** buildargv (char *@var{sp})
139*38fd1498Szrj
140*38fd1498Szrj Given a pointer to a string, parse the string extracting fields
141*38fd1498Szrj separated by whitespace and optionally enclosed within either single
142*38fd1498Szrj or double quotes (which are stripped off), and build a vector of
143*38fd1498Szrj pointers to copies of the string for each field. The input string
144*38fd1498Szrj remains unchanged. The last element of the vector is followed by a
145*38fd1498Szrj @code{NULL} element.
146*38fd1498Szrj
147*38fd1498Szrj All of the memory for the pointer array and copies of the string
148*38fd1498Szrj is obtained from @code{xmalloc}. All of the memory can be returned to the
149*38fd1498Szrj system with the single function call @code{freeargv}, which takes the
150*38fd1498Szrj returned result of @code{buildargv}, as it's argument.
151*38fd1498Szrj
152*38fd1498Szrj Returns a pointer to the argument vector if successful. Returns
153*38fd1498Szrj @code{NULL} if @var{sp} is @code{NULL} or if there is insufficient
154*38fd1498Szrj memory to complete building the argument vector.
155*38fd1498Szrj
156*38fd1498Szrj If the input is a null string (as opposed to a @code{NULL} pointer),
157*38fd1498Szrj then buildarg returns an argument vector that has one arg, a null
158*38fd1498Szrj string.
159*38fd1498Szrj
160*38fd1498Szrj @end deftypefn
161*38fd1498Szrj
162*38fd1498Szrj The memory for the argv array is dynamically expanded as necessary.
163*38fd1498Szrj
164*38fd1498Szrj In order to provide a working buffer for extracting arguments into,
165*38fd1498Szrj with appropriate stripping of quotes and translation of backslash
166*38fd1498Szrj sequences, we allocate a working buffer at least as long as the input
167*38fd1498Szrj string. This ensures that we always have enough space in which to
168*38fd1498Szrj work, since the extracted arg is never larger than the input string.
169*38fd1498Szrj
170*38fd1498Szrj The argument vector is always kept terminated with a @code{NULL} arg
171*38fd1498Szrj pointer, so it can be passed to @code{freeargv} at any time, or
172*38fd1498Szrj returned, as appropriate.
173*38fd1498Szrj
174*38fd1498Szrj */
175*38fd1498Szrj
buildargv(const char * input)176*38fd1498Szrj char **buildargv (const char *input)
177*38fd1498Szrj {
178*38fd1498Szrj char *arg;
179*38fd1498Szrj char *copybuf;
180*38fd1498Szrj int squote = 0;
181*38fd1498Szrj int dquote = 0;
182*38fd1498Szrj int bsquote = 0;
183*38fd1498Szrj int argc = 0;
184*38fd1498Szrj int maxargc = 0;
185*38fd1498Szrj char **argv = NULL;
186*38fd1498Szrj char **nargv;
187*38fd1498Szrj
188*38fd1498Szrj if (input != NULL)
189*38fd1498Szrj {
190*38fd1498Szrj copybuf = (char *) xmalloc (strlen (input) + 1);
191*38fd1498Szrj /* Is a do{}while to always execute the loop once. Always return an
192*38fd1498Szrj argv, even for null strings. See NOTES above, test case below. */
193*38fd1498Szrj do
194*38fd1498Szrj {
195*38fd1498Szrj /* Pick off argv[argc] */
196*38fd1498Szrj consume_whitespace (&input);
197*38fd1498Szrj
198*38fd1498Szrj if ((maxargc == 0) || (argc >= (maxargc - 1)))
199*38fd1498Szrj {
200*38fd1498Szrj /* argv needs initialization, or expansion */
201*38fd1498Szrj if (argv == NULL)
202*38fd1498Szrj {
203*38fd1498Szrj maxargc = INITIAL_MAXARGC;
204*38fd1498Szrj nargv = (char **) xmalloc (maxargc * sizeof (char *));
205*38fd1498Szrj }
206*38fd1498Szrj else
207*38fd1498Szrj {
208*38fd1498Szrj maxargc *= 2;
209*38fd1498Szrj nargv = (char **) xrealloc (argv, maxargc * sizeof (char *));
210*38fd1498Szrj }
211*38fd1498Szrj argv = nargv;
212*38fd1498Szrj argv[argc] = NULL;
213*38fd1498Szrj }
214*38fd1498Szrj /* Begin scanning arg */
215*38fd1498Szrj arg = copybuf;
216*38fd1498Szrj while (*input != EOS)
217*38fd1498Szrj {
218*38fd1498Szrj if (ISSPACE (*input) && !squote && !dquote && !bsquote)
219*38fd1498Szrj {
220*38fd1498Szrj break;
221*38fd1498Szrj }
222*38fd1498Szrj else
223*38fd1498Szrj {
224*38fd1498Szrj if (bsquote)
225*38fd1498Szrj {
226*38fd1498Szrj bsquote = 0;
227*38fd1498Szrj *arg++ = *input;
228*38fd1498Szrj }
229*38fd1498Szrj else if (*input == '\\')
230*38fd1498Szrj {
231*38fd1498Szrj bsquote = 1;
232*38fd1498Szrj }
233*38fd1498Szrj else if (squote)
234*38fd1498Szrj {
235*38fd1498Szrj if (*input == '\'')
236*38fd1498Szrj {
237*38fd1498Szrj squote = 0;
238*38fd1498Szrj }
239*38fd1498Szrj else
240*38fd1498Szrj {
241*38fd1498Szrj *arg++ = *input;
242*38fd1498Szrj }
243*38fd1498Szrj }
244*38fd1498Szrj else if (dquote)
245*38fd1498Szrj {
246*38fd1498Szrj if (*input == '"')
247*38fd1498Szrj {
248*38fd1498Szrj dquote = 0;
249*38fd1498Szrj }
250*38fd1498Szrj else
251*38fd1498Szrj {
252*38fd1498Szrj *arg++ = *input;
253*38fd1498Szrj }
254*38fd1498Szrj }
255*38fd1498Szrj else
256*38fd1498Szrj {
257*38fd1498Szrj if (*input == '\'')
258*38fd1498Szrj {
259*38fd1498Szrj squote = 1;
260*38fd1498Szrj }
261*38fd1498Szrj else if (*input == '"')
262*38fd1498Szrj {
263*38fd1498Szrj dquote = 1;
264*38fd1498Szrj }
265*38fd1498Szrj else
266*38fd1498Szrj {
267*38fd1498Szrj *arg++ = *input;
268*38fd1498Szrj }
269*38fd1498Szrj }
270*38fd1498Szrj input++;
271*38fd1498Szrj }
272*38fd1498Szrj }
273*38fd1498Szrj *arg = EOS;
274*38fd1498Szrj argv[argc] = xstrdup (copybuf);
275*38fd1498Szrj argc++;
276*38fd1498Szrj argv[argc] = NULL;
277*38fd1498Szrj
278*38fd1498Szrj consume_whitespace (&input);
279*38fd1498Szrj }
280*38fd1498Szrj while (*input != EOS);
281*38fd1498Szrj
282*38fd1498Szrj free (copybuf);
283*38fd1498Szrj }
284*38fd1498Szrj return (argv);
285*38fd1498Szrj }
286*38fd1498Szrj
287*38fd1498Szrj /*
288*38fd1498Szrj
289*38fd1498Szrj @deftypefn Extension int writeargv (char * const *@var{argv}, FILE *@var{file})
290*38fd1498Szrj
291*38fd1498Szrj Write each member of ARGV, handling all necessary quoting, to the file
292*38fd1498Szrj named by FILE, separated by whitespace. Return 0 on success, non-zero
293*38fd1498Szrj if an error occurred while writing to FILE.
294*38fd1498Szrj
295*38fd1498Szrj @end deftypefn
296*38fd1498Szrj
297*38fd1498Szrj */
298*38fd1498Szrj
299*38fd1498Szrj int
writeargv(char * const * argv,FILE * f)300*38fd1498Szrj writeargv (char * const *argv, FILE *f)
301*38fd1498Szrj {
302*38fd1498Szrj int status = 0;
303*38fd1498Szrj
304*38fd1498Szrj if (f == NULL)
305*38fd1498Szrj return 1;
306*38fd1498Szrj
307*38fd1498Szrj while (*argv != NULL)
308*38fd1498Szrj {
309*38fd1498Szrj const char *arg = *argv;
310*38fd1498Szrj
311*38fd1498Szrj while (*arg != EOS)
312*38fd1498Szrj {
313*38fd1498Szrj char c = *arg;
314*38fd1498Szrj
315*38fd1498Szrj if (ISSPACE(c) || c == '\\' || c == '\'' || c == '"')
316*38fd1498Szrj if (EOF == fputc ('\\', f))
317*38fd1498Szrj {
318*38fd1498Szrj status = 1;
319*38fd1498Szrj goto done;
320*38fd1498Szrj }
321*38fd1498Szrj
322*38fd1498Szrj if (EOF == fputc (c, f))
323*38fd1498Szrj {
324*38fd1498Szrj status = 1;
325*38fd1498Szrj goto done;
326*38fd1498Szrj }
327*38fd1498Szrj arg++;
328*38fd1498Szrj }
329*38fd1498Szrj
330*38fd1498Szrj if (EOF == fputc ('\n', f))
331*38fd1498Szrj {
332*38fd1498Szrj status = 1;
333*38fd1498Szrj goto done;
334*38fd1498Szrj }
335*38fd1498Szrj argv++;
336*38fd1498Szrj }
337*38fd1498Szrj
338*38fd1498Szrj done:
339*38fd1498Szrj return status;
340*38fd1498Szrj }
341*38fd1498Szrj
342*38fd1498Szrj /*
343*38fd1498Szrj
344*38fd1498Szrj @deftypefn Extension void expandargv (int *@var{argcp}, char ***@var{argvp})
345*38fd1498Szrj
346*38fd1498Szrj The @var{argcp} and @code{argvp} arguments are pointers to the usual
347*38fd1498Szrj @code{argc} and @code{argv} arguments to @code{main}. This function
348*38fd1498Szrj looks for arguments that begin with the character @samp{@@}. Any such
349*38fd1498Szrj arguments are interpreted as ``response files''. The contents of the
350*38fd1498Szrj response file are interpreted as additional command line options. In
351*38fd1498Szrj particular, the file is separated into whitespace-separated strings;
352*38fd1498Szrj each such string is taken as a command-line option. The new options
353*38fd1498Szrj are inserted in place of the option naming the response file, and
354*38fd1498Szrj @code{*argcp} and @code{*argvp} will be updated. If the value of
355*38fd1498Szrj @code{*argvp} is modified by this function, then the new value has
356*38fd1498Szrj been dynamically allocated and can be deallocated by the caller with
357*38fd1498Szrj @code{freeargv}. However, most callers will simply call
358*38fd1498Szrj @code{expandargv} near the beginning of @code{main} and allow the
359*38fd1498Szrj operating system to free the memory when the program exits.
360*38fd1498Szrj
361*38fd1498Szrj @end deftypefn
362*38fd1498Szrj
363*38fd1498Szrj */
364*38fd1498Szrj
365*38fd1498Szrj void
expandargv(int * argcp,char *** argvp)366*38fd1498Szrj expandargv (int *argcp, char ***argvp)
367*38fd1498Szrj {
368*38fd1498Szrj /* The argument we are currently processing. */
369*38fd1498Szrj int i = 0;
370*38fd1498Szrj /* To check if ***argvp has been dynamically allocated. */
371*38fd1498Szrj char ** const original_argv = *argvp;
372*38fd1498Szrj /* Limit the number of response files that we parse in order
373*38fd1498Szrj to prevent infinite recursion. */
374*38fd1498Szrj unsigned int iteration_limit = 2000;
375*38fd1498Szrj /* Loop over the arguments, handling response files. We always skip
376*38fd1498Szrj ARGVP[0], as that is the name of the program being run. */
377*38fd1498Szrj while (++i < *argcp)
378*38fd1498Szrj {
379*38fd1498Szrj /* The name of the response file. */
380*38fd1498Szrj const char *filename;
381*38fd1498Szrj /* The response file. */
382*38fd1498Szrj FILE *f;
383*38fd1498Szrj /* An upper bound on the number of characters in the response
384*38fd1498Szrj file. */
385*38fd1498Szrj long pos;
386*38fd1498Szrj /* The number of characters in the response file, when actually
387*38fd1498Szrj read. */
388*38fd1498Szrj size_t len;
389*38fd1498Szrj /* A dynamically allocated buffer used to hold options read from a
390*38fd1498Szrj response file. */
391*38fd1498Szrj char *buffer;
392*38fd1498Szrj /* Dynamically allocated storage for the options read from the
393*38fd1498Szrj response file. */
394*38fd1498Szrj char **file_argv;
395*38fd1498Szrj /* The number of options read from the response file, if any. */
396*38fd1498Szrj size_t file_argc;
397*38fd1498Szrj #ifdef S_ISDIR
398*38fd1498Szrj struct stat sb;
399*38fd1498Szrj #endif
400*38fd1498Szrj /* We are only interested in options of the form "@file". */
401*38fd1498Szrj filename = (*argvp)[i];
402*38fd1498Szrj if (filename[0] != '@')
403*38fd1498Szrj continue;
404*38fd1498Szrj /* If we have iterated too many times then stop. */
405*38fd1498Szrj if (-- iteration_limit == 0)
406*38fd1498Szrj {
407*38fd1498Szrj fprintf (stderr, "%s: error: too many @-files encountered\n", (*argvp)[0]);
408*38fd1498Szrj xexit (1);
409*38fd1498Szrj }
410*38fd1498Szrj #ifdef S_ISDIR
411*38fd1498Szrj if (stat (filename+1, &sb) < 0)
412*38fd1498Szrj continue;
413*38fd1498Szrj if (S_ISDIR(sb.st_mode))
414*38fd1498Szrj {
415*38fd1498Szrj fprintf (stderr, "%s: error: @-file refers to a directory\n", (*argvp)[0]);
416*38fd1498Szrj xexit (1);
417*38fd1498Szrj }
418*38fd1498Szrj #endif
419*38fd1498Szrj /* Read the contents of the file. */
420*38fd1498Szrj f = fopen (++filename, "r");
421*38fd1498Szrj if (!f)
422*38fd1498Szrj continue;
423*38fd1498Szrj if (fseek (f, 0L, SEEK_END) == -1)
424*38fd1498Szrj goto error;
425*38fd1498Szrj pos = ftell (f);
426*38fd1498Szrj if (pos == -1)
427*38fd1498Szrj goto error;
428*38fd1498Szrj if (fseek (f, 0L, SEEK_SET) == -1)
429*38fd1498Szrj goto error;
430*38fd1498Szrj buffer = (char *) xmalloc (pos * sizeof (char) + 1);
431*38fd1498Szrj len = fread (buffer, sizeof (char), pos, f);
432*38fd1498Szrj if (len != (size_t) pos
433*38fd1498Szrj /* On Windows, fread may return a value smaller than POS,
434*38fd1498Szrj due to CR/LF->CR translation when reading text files.
435*38fd1498Szrj That does not in-and-of itself indicate failure. */
436*38fd1498Szrj && ferror (f))
437*38fd1498Szrj goto error;
438*38fd1498Szrj /* Add a NUL terminator. */
439*38fd1498Szrj buffer[len] = '\0';
440*38fd1498Szrj /* If the file is empty or contains only whitespace, buildargv would
441*38fd1498Szrj return a single empty argument. In this context we want no arguments,
442*38fd1498Szrj instead. */
443*38fd1498Szrj if (only_whitespace (buffer))
444*38fd1498Szrj {
445*38fd1498Szrj file_argv = (char **) xmalloc (sizeof (char *));
446*38fd1498Szrj file_argv[0] = NULL;
447*38fd1498Szrj }
448*38fd1498Szrj else
449*38fd1498Szrj /* Parse the string. */
450*38fd1498Szrj file_argv = buildargv (buffer);
451*38fd1498Szrj /* If *ARGVP is not already dynamically allocated, copy it. */
452*38fd1498Szrj if (*argvp == original_argv)
453*38fd1498Szrj *argvp = dupargv (*argvp);
454*38fd1498Szrj /* Count the number of arguments. */
455*38fd1498Szrj file_argc = 0;
456*38fd1498Szrj while (file_argv[file_argc])
457*38fd1498Szrj ++file_argc;
458*38fd1498Szrj /* Now, insert FILE_ARGV into ARGV. The "+1" below handles the
459*38fd1498Szrj NULL terminator at the end of ARGV. */
460*38fd1498Szrj *argvp = ((char **)
461*38fd1498Szrj xrealloc (*argvp,
462*38fd1498Szrj (*argcp + file_argc + 1) * sizeof (char *)));
463*38fd1498Szrj memmove (*argvp + i + file_argc, *argvp + i + 1,
464*38fd1498Szrj (*argcp - i) * sizeof (char *));
465*38fd1498Szrj memcpy (*argvp + i, file_argv, file_argc * sizeof (char *));
466*38fd1498Szrj /* The original option has been replaced by all the new
467*38fd1498Szrj options. */
468*38fd1498Szrj *argcp += file_argc - 1;
469*38fd1498Szrj /* Free up memory allocated to process the response file. We do
470*38fd1498Szrj not use freeargv because the individual options in FILE_ARGV
471*38fd1498Szrj are now in the main ARGV. */
472*38fd1498Szrj free (file_argv);
473*38fd1498Szrj free (buffer);
474*38fd1498Szrj /* Rescan all of the arguments just read to support response
475*38fd1498Szrj files that include other response files. */
476*38fd1498Szrj --i;
477*38fd1498Szrj error:
478*38fd1498Szrj /* We're all done with the file now. */
479*38fd1498Szrj fclose (f);
480*38fd1498Szrj }
481*38fd1498Szrj }
482*38fd1498Szrj
483*38fd1498Szrj /*
484*38fd1498Szrj
485*38fd1498Szrj @deftypefn Extension int countargv (char * const *@var{argv})
486*38fd1498Szrj
487*38fd1498Szrj Return the number of elements in @var{argv}.
488*38fd1498Szrj Returns zero if @var{argv} is NULL.
489*38fd1498Szrj
490*38fd1498Szrj @end deftypefn
491*38fd1498Szrj
492*38fd1498Szrj */
493*38fd1498Szrj
494*38fd1498Szrj int
countargv(char * const * argv)495*38fd1498Szrj countargv (char * const *argv)
496*38fd1498Szrj {
497*38fd1498Szrj int argc;
498*38fd1498Szrj
499*38fd1498Szrj if (argv == NULL)
500*38fd1498Szrj return 0;
501*38fd1498Szrj for (argc = 0; argv[argc] != NULL; argc++)
502*38fd1498Szrj continue;
503*38fd1498Szrj return argc;
504*38fd1498Szrj }
505*38fd1498Szrj
506*38fd1498Szrj #ifdef MAIN
507*38fd1498Szrj
508*38fd1498Szrj /* Simple little test driver. */
509*38fd1498Szrj
510*38fd1498Szrj static const char *const tests[] =
511*38fd1498Szrj {
512*38fd1498Szrj "a simple command line",
513*38fd1498Szrj "arg 'foo' is single quoted",
514*38fd1498Szrj "arg \"bar\" is double quoted",
515*38fd1498Szrj "arg \"foo bar\" has embedded whitespace",
516*38fd1498Szrj "arg 'Jack said \\'hi\\'' has single quotes",
517*38fd1498Szrj "arg 'Jack said \\\"hi\\\"' has double quotes",
518*38fd1498Szrj "a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9",
519*38fd1498Szrj
520*38fd1498Szrj /* This should be expanded into only one argument. */
521*38fd1498Szrj "trailing-whitespace ",
522*38fd1498Szrj
523*38fd1498Szrj "",
524*38fd1498Szrj NULL
525*38fd1498Szrj };
526*38fd1498Szrj
527*38fd1498Szrj int
main(void)528*38fd1498Szrj main (void)
529*38fd1498Szrj {
530*38fd1498Szrj char **argv;
531*38fd1498Szrj const char *const *test;
532*38fd1498Szrj char **targs;
533*38fd1498Szrj
534*38fd1498Szrj for (test = tests; *test != NULL; test++)
535*38fd1498Szrj {
536*38fd1498Szrj printf ("buildargv(\"%s\")\n", *test);
537*38fd1498Szrj if ((argv = buildargv (*test)) == NULL)
538*38fd1498Szrj {
539*38fd1498Szrj printf ("failed!\n\n");
540*38fd1498Szrj }
541*38fd1498Szrj else
542*38fd1498Szrj {
543*38fd1498Szrj for (targs = argv; *targs != NULL; targs++)
544*38fd1498Szrj {
545*38fd1498Szrj printf ("\t\"%s\"\n", *targs);
546*38fd1498Szrj }
547*38fd1498Szrj printf ("\n");
548*38fd1498Szrj }
549*38fd1498Szrj freeargv (argv);
550*38fd1498Szrj }
551*38fd1498Szrj
552*38fd1498Szrj return 0;
553*38fd1498Szrj }
554*38fd1498Szrj
555*38fd1498Szrj #endif /* MAIN */
556