]> Dogcows Code - chaz/tar/blob - lib/prepargs.c
Update copyright years.
[chaz/tar] / lib / prepargs.c
1 /* Parse arguments from a string and prepend them to an argv.
2 Copyright 1999-2001, 2007, 2013-2014 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
17 /* Written by Paul Eggert <eggert@twinsun.com>. */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include "prepargs.h"
23 #include <sys/types.h>
24 #include <xalloc.h>
25
26 #if HAVE_STRING_H
27 # include <string.h>
28 #endif
29
30 #include <ctype.h>
31
32 /* Find the white-space-separated options specified by OPTIONS, and
33 using BUF to store copies of these options, set ARGV[0], ARGV[1],
34 etc. to the option copies. Return the number N of options found.
35 Do not set ARGV[N]. If ARGV is null, do not store ARGV[0]
36 etc. Backslash can be used to escape whitespace (and backslashes). */
37 static int
38 prepend_args (char const *options, char *buf, char **argv)
39 {
40 char const *o = options;
41 char *b = buf;
42 int n = 0;
43
44 for (;;)
45 {
46 while (isspace ((unsigned char) *o))
47 o++;
48 if (!*o)
49 return n;
50 if (argv)
51 argv[n] = b;
52 n++;
53
54 do
55 if ((*b++ = *o++) == '\\' && *o)
56 b[-1] = *o++;
57 while (*o && ! isspace ((unsigned char) *o));
58
59 *b++ = '\0';
60 }
61 }
62
63 /* Prepend the whitespace-separated options in OPTIONS to the argument
64 vector of a main program with argument count *PARGC and argument
65 vector *PARGV. */
66 void
67 prepend_default_options (char const *options, int *pargc, char ***pargv)
68 {
69 if (options)
70 {
71 char *buf = xmalloc (strlen (options) + 1);
72 int prepended = prepend_args (options, buf, (char **) 0);
73 int argc = *pargc;
74 char * const *argv = *pargv;
75 char **pp = (char **) xmalloc ((prepended + argc + 1) * sizeof *pp);
76 *pargc = prepended + argc;
77 *pargv = pp;
78 *pp++ = *argv++;
79 pp += prepend_args (options, buf, pp);
80 while ((*pp++ = *argv++))
81 continue;
82 }
83 }
This page took 0.030394 seconds and 4 git commands to generate.