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