Debian Codegraph @

getoldopt.c

raw symbols SCIP
1 /* $OpenBSD: getoldopt.c,v 1.9 2009/10/27 23:59:22 deraadt Exp $ */
2 /* $NetBSD: getoldopt.c,v 1.3 1995/03/21 09:07:28 cgd Exp $ */
3
4 /*
5 * Plug-compatible replacement for getopt() for parsing tar-like
6 * arguments. If the first argument begins with "-", it uses getopt;
7 * otherwise, it uses the old rules used by tar, dump, and ps.
8 *
9 * Written 25 August 1985 by John Gilmore (ihnp4!hoptoad!gnu) and
10 * placed in the Public Domain for your edification and enjoyment,
11 * https://creativecommons.org/publicdomain/zero/1.0/legalcode (CC0)
12 * being an alternative licence, confirmed by eMail on 2019-02-10.
13 */
14
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <stdio.h>
18 #include <string.h>
19 #if HAVE_STRINGS_H
20 #include <strings.h>
21 #endif
22 #include <unistd.h>
23
24 #include "pax.h"
25 #include "extern.h"
26
27 __RCSID("$MirOS: src/bin/pax/getoldopt.c,v 1.7 2019/02/10 22:00:06 tg Exp $");
28
29 int
30 getoldopt(int argc, char **argv, const char *optstring)
31 {
32 static char *key; /* Points to next keyletter */
33 static char use_getopt; /* !=0 if argv[1][0] was '-' */
34 char c;
35 char *place;
36
37 optarg = NULL;
38
39 if (key == NULL) { /* First time */
40 if (argc < 2)
41 return (-1);
42 key = argv[1];
43 if (*key == '-')
44 use_getopt++;
45 else
46 optind = 2;
47 }
48
49 if (use_getopt)
50 return (getopt(argc, argv, optstring));
51
52 c = *key++;
53 if (c == '\0') {
54 key--;
55 return (-1);
56 }
57 place = strchr(optstring, c);
58
59 if (place == NULL || c == ':') {
60 fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
61 return ('?');
62 }
63
64 place++;
65 if (*place == ':') {
66 if (optind < argc) {
67 optarg = argv[optind];
68 optind++;
69 } else {
70 fprintf(stderr, "%s: %c argument missing\n",
71 argv[0], c);
72 return ('?');
73 }
74 }
75
76 return (c);
77 }