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 placed |
10 |
|
|
* in the Public Domain for your edification and enjoyment. |
11 |
|
|
*/ |
12 |
|
|
|
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" |
20 |
|
|
|
21 |
|
|
int |
22 |
|
|
getoldopt(int argc, char **argv, const char *optstring) |
23 |
|
74 |
{ |
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; |
28 |
|
|
|
29 |
|
74 |
optarg = NULL; |
30 |
|
|
|
31 |
✓✓ |
74 |
if (key == NULL) { /* First time */ |
32 |
✗✓ |
22 |
if (argc < 2) |
33 |
|
|
return (-1); |
34 |
|
22 |
key = argv[1]; |
35 |
✓✓ |
22 |
if (*key == '-') |
36 |
|
6 |
use_getopt++; |
37 |
|
|
else |
38 |
|
16 |
optind = 2; |
39 |
|
|
} |
40 |
|
|
|
41 |
✓✓ |
74 |
if (use_getopt) |
42 |
|
22 |
return (getopt(argc, argv, optstring)); |
43 |
|
|
|
44 |
|
52 |
c = *key++; |
45 |
✓✓ |
52 |
if (c == '\0') { |
46 |
|
16 |
key--; |
47 |
|
16 |
return (-1); |
48 |
|
|
} |
49 |
|
36 |
place = strchr(optstring, c); |
50 |
|
|
|
51 |
✗✓ |
36 |
if (place == NULL || c == ':') { |
52 |
|
|
fprintf(stderr, "%s: unknown option %c\n", argv[0], c); |
53 |
|
|
return ('?'); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
36 |
place++; |
57 |
✓✓ |
36 |
if (*place == ':') { |
58 |
✓✗ |
16 |
if (optind < argc) { |
59 |
|
16 |
optarg = argv[optind]; |
60 |
|
16 |
optind++; |
61 |
|
|
} else { |
62 |
|
|
fprintf(stderr, "%s: %c argument missing\n", |
63 |
|
|
argv[0], c); |
64 |
|
|
return ('?'); |
65 |
|
|
} |
66 |
|
|
} |
67 |
|
|
|
68 |
|
36 |
return (c); |
69 |
|
|
} |