Commit | Line | Data |
---|---|---|
8c4ebc23 JH |
1 | /* Replacement for getopt() that can be used by tar. |
2 | Copyright (C) 1988 Free Software Foundation | |
3 | ||
4 | This file is part of GNU Tar. | |
5 | ||
6 | GNU Tar is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GNU Tar is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Tar; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | /* | |
21 | * Plug-compatible replacement for getopt() for parsing tar-like | |
22 | * arguments. If the first argument begins with "-", it uses getopt; | |
23 | * otherwise, it uses the old rules used by tar, dump, and ps. | |
24 | * | |
25 | * Written 25 August 1985 by John Gilmore (ihnp4!hoptoad!gnu) | |
26 | */ | |
27 | ||
28 | #include <stdio.h> | |
29 | #include "getopt.h" | |
30 | #include "tar.h" /* For msg() declaration if STDC_MSG. */ | |
31 | #include <sys/types.h> | |
32 | #include "port.h" | |
33 | ||
34 | int | |
35 | getoldopt (argc, argv, optstring, long_options, opt_index) | |
36 | int argc; | |
37 | char **argv; | |
38 | char *optstring; | |
39 | struct option *long_options; | |
40 | int *opt_index; | |
41 | { | |
42 | extern char *optarg; /* Points to next arg */ | |
43 | extern int optind; /* Global argv index */ | |
44 | static char *key; /* Points to next keyletter */ | |
45 | static char use_getopt; /* !=0 if argv[1][0] was '-' */ | |
46 | char c; | |
47 | char *place; | |
48 | ||
49 | optarg = NULL; | |
50 | ||
51 | if (key == NULL) | |
52 | { /* First time */ | |
53 | if (argc < 2) | |
54 | return EOF; | |
55 | key = argv[1]; | |
56 | if ((*key == '-') || (*key == '+')) | |
57 | use_getopt++; | |
58 | else | |
59 | optind = 2; | |
60 | } | |
61 | ||
62 | if (use_getopt) | |
63 | return getopt_long (argc, argv, optstring, | |
64 | long_options, opt_index); | |
65 | ||
66 | c = *key++; | |
67 | if (c == '\0') | |
68 | { | |
69 | key--; | |
70 | return EOF; | |
71 | } | |
72 | place = index (optstring, c); | |
73 | ||
74 | if (place == NULL || c == ':') | |
75 | { | |
76 | msg ("unknown option %c", c); | |
77 | return ('?'); | |
78 | } | |
79 | ||
80 | place++; | |
81 | if (*place == ':') | |
82 | { | |
83 | if (optind < argc) | |
84 | { | |
85 | optarg = argv[optind]; | |
86 | optind++; | |
87 | } | |
88 | else | |
89 | { | |
90 | msg ("%c argument missing", c); | |
91 | return ('?'); | |
92 | } | |
93 | } | |
94 | ||
95 | return (c); | |
96 | } |