]> Dogcows Code - chaz/tar/blob - src/getoldopt.c
Initial revision
[chaz/tar] / src / getoldopt.c
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
32 int
33 getoldopt(argc, argv, optstring, long_options, opt_index)
34 int argc;
35 char **argv;
36 char *optstring;
37 struct option *long_options;
38 int *opt_index;
39 {
40 extern char *optarg; /* Points to next arg */
41 extern int optind; /* Global argv index */
42 static char *key; /* Points to next keyletter */
43 static char use_getopt; /* !=0 if argv[1][0] was '-' */
44 extern char *index();
45 char c;
46 char *place;
47
48 optarg = NULL;
49
50 if (key == NULL) { /* First time */
51 if (argc < 2) return EOF;
52 key = argv[1];
53 if ((*key == '-') || (*key == '+'))
54 use_getopt++;
55 else
56 optind = 2;
57 }
58
59 if (use_getopt)
60 return getopt_long(argc, argv, optstring,
61 long_options, opt_index);
62
63 c = *key++;
64 if (c == '\0') {
65 key--;
66 return EOF;
67 }
68 place = index(optstring, c);
69
70 if (place == NULL || c == ':') {
71 msg("unknown option %c", c);
72 return('?');
73 }
74
75 place++;
76 if (*place == ':') {
77 if (optind < argc) {
78 optarg = argv[optind];
79 optind++;
80 } else {
81 msg("%c argument missing", c);
82 return('?');
83 }
84 }
85
86 return(c);
87 }
This page took 0.037815 seconds and 5 git commands to generate.