Home | History | Annotate | Line # | Download | only in pax
getoldopt.c revision 1.13
      1  1.13        tv /*	$NetBSD: getoldopt.c,v 1.13 2002/01/29 10:20:29 tv Exp $	*/
      2   1.3       cgd 
      3   1.1       jtc /*
      4   1.1       jtc  * Plug-compatible replacement for getopt() for parsing tar-like
      5   1.1       jtc  * arguments.  If the first argument begins with "-", it uses getopt;
      6   1.1       jtc  * otherwise, it uses the old rules used by tar, dump, and ps.
      7   1.1       jtc  *
      8   1.1       jtc  * Written 25 August 1985 by John Gilmore (ihnp4!hoptoad!gnu) and placed
      9   1.4       jtc  * in the Public Domain for your edification and enjoyment.
     10   1.1       jtc  */
     11   1.1       jtc 
     12   1.5  christos #include <sys/cdefs.h>
     13   1.1       jtc #ifndef lint
     14  1.13        tv __RCSID("$NetBSD: getoldopt.c,v 1.13 2002/01/29 10:20:29 tv Exp $");
     15   1.1       jtc #endif /* not lint */
     16   1.1       jtc 
     17   1.1       jtc #include <stdio.h>
     18   1.1       jtc #include <string.h>
     19   1.2       cgd #include <unistd.h>
     20   1.5  christos #include <sys/stat.h>
     21   1.5  christos #include "pax.h"
     22   1.5  christos #include "extern.h"
     23  1.13        tv 
     24  1.13        tv /* After extern.h to pull in HAVE_CONFIG_H */
     25  1.13        tv #include <getopt.h>
     26   1.1       jtc 
     27   1.1       jtc int
     28  1.11     lukem getoldopt(int argc, char **argv, const char *optstring,
     29  1.12     lukem 	struct option *longopts, int *idx)
     30   1.1       jtc {
     31   1.1       jtc 	static char	*key;		/* Points to next keyletter */
     32   1.1       jtc 	static char	use_getopt;	/* !=0 if argv[1][0] was '-' */
     33   1.1       jtc 	char		c;
     34   1.1       jtc 	char		*place;
     35   1.1       jtc 
     36   1.1       jtc 	optarg = NULL;
     37   1.8     itohy 
     38   1.1       jtc 	if (key == NULL) {		/* First time */
     39   1.6     lukem 		if (argc < 2) return -1;
     40   1.1       jtc 		key = argv[1];
     41   1.1       jtc 		if (*key == '-')
     42   1.1       jtc 			use_getopt++;
     43   1.1       jtc 		else
     44   1.1       jtc 			optind = 2;
     45   1.1       jtc 	}
     46   1.1       jtc 
     47   1.1       jtc 	if (use_getopt)
     48  1.10   thorpej 		return ((longopts != NULL) ?
     49  1.12     lukem 			getopt_long(argc, argv, optstring, longopts, idx) :
     50  1.10   thorpej 			getopt(argc, argv, optstring));
     51   1.1       jtc 
     52   1.1       jtc 	c = *key++;
     53   1.1       jtc 	if (c == '\0') {
     54   1.1       jtc 		key--;
     55   1.7     lukem 		return -1;
     56   1.1       jtc 	}
     57   1.1       jtc 	place = strchr(optstring, c);
     58   1.1       jtc 
     59   1.1       jtc 	if (place == NULL || c == ':') {
     60   1.1       jtc 		fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
     61   1.1       jtc 		return('?');
     62   1.1       jtc 	}
     63   1.1       jtc 
     64   1.1       jtc 	place++;
     65   1.1       jtc 	if (*place == ':') {
     66   1.1       jtc 		if (optind < argc) {
     67   1.1       jtc 			optarg = argv[optind];
     68   1.1       jtc 			optind++;
     69   1.1       jtc 		} else {
     70   1.1       jtc 			fprintf(stderr, "%s: %c argument missing\n",
     71   1.1       jtc 				argv[0], c);
     72   1.1       jtc 			return('?');
     73   1.1       jtc 		}
     74   1.1       jtc 	}
     75   1.1       jtc 
     76   1.1       jtc 	return(c);
     77   1.1       jtc }
     78