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