1 /* prepend_args.c - utilility programs for manpiulating argv[] 2 Copyright (C) 1999 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 2, 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, write to the Free Software 16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 17 02111-1307, USA. */ 18 19 /* $FreeBSD: src/contrib/diff/prepend_args.c,v 1.1 1999/11/26 02:51:44 obrien Exp $ */ 20 21 22 #ifdef HAVE_CONFIG_H 23 # include "config.h" 24 #endif 25 #include "system.h" 26 #include "prepend_args.h" 27 #include "diff.h" 28 29 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). */ 35 static int 36 prepend_args (options, buf, argv) 37 char const *options; 38 char *buf; 39 char **argv; 40 { 41 char const *o = options; 42 char *b = buf; 43 int n = 0; 44 45 for (;;) 46 { 47 while (ISSPACE ((unsigned char) *o)) 48 o++; 49 if (!*o) 50 return n; 51 if (argv) 52 argv[n] = b; 53 n++; 54 55 do 56 if ((*b++ = *o++) == '\\' && *o) 57 b[-1] = *o++; 58 while (*o && ! ISSPACE ((unsigned char) *o)); 59 60 *b++ = '\0'; 61 } 62 } 63 64 /* Prepend the whitespace-separated options in OPTIONS to the argument 65 vector of a main program with argument count *PARGC and argument 66 vector *PARGV. */ 67 void 68 prepend_default_options (options, pargc, pargv) 69 char const *options; 70 int *pargc; 71 char ***pargv; 72 { 73 if (options) 74 { 75 char *buf = xmalloc (strlen (options) + 1); 76 int prepended = prepend_args (options, buf, (char **) NULL); 77 int argc = *pargc; 78 char * const *argv = *pargv; 79 char **pp = (char **) xmalloc ((prepended + argc + 1) * sizeof *pp); 80 *pargc = prepended + argc; 81 *pargv = pp; 82 *pp++ = *argv++; 83 pp += prepend_args (options, buf, pp); 84 while ((*pp++ = *argv++)) 85 continue; 86 } 87 } 88