fix leftover protocmp warnings
[unleashed.git] / bin / pax / getoldopt.c
blob8ceb189eec2ca84f5b6ec45ddcfc0723be320c2e
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 $ */
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.
9 * Written 25 August 1985 by John Gilmore (ihnp4!hoptoad!gnu) and placed
10 * in the Public Domain for your edification and enjoyment.
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include "pax.h"
19 #include "extern.h"
21 int
22 getoldopt(int argc, char **argv, const char *optstring)
24 static char *key; /* Points to next keyletter */
25 static char use_getopt; /* !=0 if argv[1][0] was '-' */
26 char c;
27 char *place;
29 optarg = NULL;
31 if (key == NULL) { /* First time */
32 if (argc < 2)
33 return (-1);
34 key = argv[1];
35 if (*key == '-')
36 use_getopt++;
37 else
38 optind = 2;
41 if (use_getopt)
42 return (getopt(argc, argv, optstring));
44 c = *key++;
45 if (c == '\0') {
46 key--;
47 return (-1);
49 place = strchr(optstring, c);
51 if (place == NULL || c == ':') {
52 fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
53 return ('?');
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 ('?');
68 return (c);