1 /* prepend_args.c - utilility programs for manpiulating argv[]
2 Copyright (C) 1999 Free Software Foundation, Inc.
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 2, or (at your option)
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 /* $FreeBSD: src/gnu/usr.bin/cvs/cvs/prepend_args.c,v 1.3.2.2 2002/12/19 21:18:01 peter Exp $ */
20 /* $DragonFly: src/gnu/usr.bin/cvs/cvs/prepend_args.c,v 1.3 2004/01/20 05:42:03 asmodai Exp $ */
27 #include "prepend_args.h"
30 /* Find the white-space-separated options specified by OPTIONS, and
31 using BUF to store copies of these options, set ARGV[0], ARGV[1],
32 etc. to the option copies. Return the number N of options found.
33 Do not set ARGV[N] to NULL. If ARGV is NULL, do not store ARGV[0]
34 etc. Backslash can be used to escape whitespace (and backslashes). */
36 prepend_args (char const *options, char *buf, char **argv)
38 char const *o = options;
44 while (isspace ((unsigned char) *o))
53 if ((*b++ = *o++) == '\\' && *o)
55 while (*o && ! isspace ((unsigned char) *o));
61 /* Prepend the whitespace-separated options in OPTIONS to the argument
62 vector of a main program with argument count *PARGC and argument
65 prepend_default_options (char const *options, int *pargc, char ***pargv)
69 char *buf = xmalloc (strlen (options) + 1);
70 int prepended = prepend_args (options, buf, (char **) NULL);
72 char * const *argv = *pargv;
73 char **pp = (char **) xmalloc ((prepended + argc + 1) * sizeof *pp);
74 *pargc = prepended + argc;
77 pp += prepend_args (options, buf, pp);
78 while ((*pp++ = *argv++))