From e7d0a7724b70b13eaee2c732de8d3696220dd91e Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 24 Oct 2000 06:18:37 +0000 Subject: [PATCH] Initial revision --- lib/prepargs.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++ lib/prepargs.h | 3 ++ tests/extrac04.sh | 28 +++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 lib/prepargs.c create mode 100644 lib/prepargs.h create mode 100755 tests/extrac04.sh diff --git a/lib/prepargs.c b/lib/prepargs.c new file mode 100644 index 0000000..25dd601 --- /dev/null +++ b/lib/prepargs.c @@ -0,0 +1,91 @@ +/* Parse arguments from a string and prepend them to an argv. + Copyright 1999, 2000 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. */ + +/* Written by Paul Eggert . */ + +#ifdef HAVE_CONFIG_H +# include +#endif +#include "prepargs.h" +#include +#include + +#include + +/* IN_CTYPE_DOMAIN (C) is nonzero if the unsigned char C can safely be given + as an argument to macros like "isspace". */ +#ifdef STDC_HEADERS +# define IN_CTYPE_DOMAIN(c) 1 +#else +# define IN_CTYPE_DOMAIN(c) ((c) <= 0177) +#endif + +#define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c)) + +/* Find the white-space-separated options specified by OPTIONS, and + using BUF to store copies of these options, set ARGV[0], ARGV[1], + etc. to the option copies. Return the number N of options found. + Do not set ARGV[N] to NULL. If ARGV is NULL, do not store ARGV[0] + etc. Backslash can be used to escape whitespace (and backslashes). */ +static int +prepend_args (char const *options, char *buf, char **argv) +{ + char const *o = options; + char *b = buf; + int n = 0; + + for (;;) + { + while (ISSPACE ((unsigned char) *o)) + o++; + if (!*o) + return n; + if (argv) + argv[n] = b; + n++; + + do + if ((*b++ = *o++) == '\\' && *o) + b[-1] = *o++; + while (*o && ! ISSPACE ((unsigned char) *o)); + + *b++ = '\0'; + } +} + +/* Prepend the whitespace-separated options in OPTIONS to the argument + vector of a main program with argument count *PARGC and argument + vector *PARGV. */ +void +prepend_default_options (char const *options, int *pargc, char ***pargv) +{ + if (options) + { + char *buf = xmalloc (strlen (options) + 1); + int prepended = prepend_args (options, buf, (char **) NULL); + int argc = *pargc; + char * const *argv = *pargv; + char **pp = (char **) xmalloc ((prepended + argc + 1) * sizeof *pp); + *pargc = prepended + argc; + *pargv = pp; + *pp++ = *argv++; + pp += prepend_args (options, buf, pp); + while ((*pp++ = *argv++)) + continue; + } +} diff --git a/lib/prepargs.h b/lib/prepargs.h new file mode 100644 index 0000000..ce93ea8 --- /dev/null +++ b/lib/prepargs.h @@ -0,0 +1,3 @@ +/* Parse arguments from a string and prepend them to an argv. */ + +void prepend_default_options (char const *, int *, char ***); diff --git a/tests/extrac04.sh b/tests/extrac04.sh new file mode 100755 index 0000000..6cf6d6d --- /dev/null +++ b/tests/extrac04.sh @@ -0,0 +1,28 @@ +#! /bin/sh +# Check for fnmatch problems in glibc 2.1.95. + +. ./preset +. $srcdir/before + +set -e +touch file1 +mkdir directory +mkdir directory/subdirectory +touch directory/file1 +touch directory/file2 +touch directory/subdirectory/file1 +touch directory/subdirectory/file2 +tar -cf archive ./file1 directory +tar -tf archive \ + --exclude='./*1' \ + --exclude='d*/*1' \ + --exclude='d*/s*/*2' | sort + +out="\ +directory/ +directory/file2 +directory/subdirectory/ +directory/subdirectory/file1 +" + +. $srcdir/after -- 2.45.2