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 |
|
|
{ |
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 |
|
1308 |
optarg = NULL; |
30 |
|
|
|
31 |
✓✓ |
654 |
if (key == NULL) { /* First time */ |
32 |
✗✓ |
192 |
if (argc < 2) |
33 |
|
|
return (-1); |
34 |
|
192 |
key = argv[1]; |
35 |
✓✓ |
192 |
if (*key == '-') |
36 |
|
51 |
use_getopt++; |
37 |
|
|
else |
38 |
|
141 |
optind = 2; |
39 |
|
|
} |
40 |
|
|
|
41 |
✓✓ |
654 |
if (use_getopt) |
42 |
|
187 |
return (getopt(argc, argv, optstring)); |
43 |
|
|
|
44 |
|
467 |
c = *key++; |
45 |
✓✓ |
467 |
if (c == '\0') { |
46 |
|
141 |
key--; |
47 |
|
141 |
return (-1); |
48 |
|
|
} |
49 |
|
326 |
place = strchr(optstring, c); |
50 |
|
|
|
51 |
✓✗✗✓
|
652 |
if (place == NULL || c == ':') { |
52 |
|
|
fprintf(stderr, "%s: unknown option %c\n", argv[0], c); |
53 |
|
|
return ('?'); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
326 |
place++; |
57 |
✓✓ |
326 |
if (*place == ':') { |
58 |
✓✗ |
141 |
if (optind < argc) { |
59 |
|
141 |
optarg = argv[optind]; |
60 |
|
141 |
optind++; |
61 |
|
|
} else { |
62 |
|
|
fprintf(stderr, "%s: %c argument missing\n", |
63 |
|
|
argv[0], c); |
64 |
|
|
return ('?'); |
65 |
|
|
} |
66 |
|
141 |
} |
67 |
|
|
|
68 |
|
326 |
return (c); |
69 |
|
654 |
} |