Repository navigation
Expand file tree
/
Copy pathgetopt.c
More file actions
166 lines (140 loc) · 2.85 KB
/
Copy pathgetopt.c
File metadata and controls
166 lines (140 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
Newsgroups: mod.std.unix
Subject: public domain AT&T getopt source
Date: 3 Nov 85 19:34:15 GMT
Here's something you've all been waiting for: the AT&T public domain
source for getopt(3). It is the code which was given out at the 1985
UNIFORUM conference in Dallas. I obtained it by electronic mail
directly from AT&T. The people there assure me that it is indeed
in the public domain.
*/
/*
* modified 20-Feb-2006 for postoffice (opt; == rest of current argument
* is optarg), and cripple ERR() so that it uses fprintf() to stderr.
*/
/*LINTLIBRARY*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#if HAVE_LIBGEN_H
#include <libgen.h>
#endif
#define ERR(s, c) if(z_opterr){ fprintf(stderr, "%s%s%c\n", pgm, s, c); }
extern char *pgm;
int z_opterr = 1;
int z_optind = 1;
int z_optopt;
char *z_optarg;
int
z_getopt(argc, argv, opts)
int argc;
char **argv, *opts;
{
static int sp = 1;
register int c;
register char *cp;
if(sp == 1) {
if(z_optind >= argc ||
argv[z_optind][0] != '-' || argv[z_optind][1] == '\0')
return(EOF);
else if(strcmp(argv[z_optind], "--") == 0) {
z_optind++;
return(EOF);
}
}
z_optopt = c = argv[z_optind][sp];
if(c == ':' || c == ';' || (cp=strchr(opts, c)) == 0) {
ERR(": illegal option -- ", c);
if(argv[z_optind][++sp] == '\0') {
z_optind++;
sp = 1;
}
return('?');
}
if(*++cp == ':') {
if(argv[z_optind][sp+1] != '\0')
z_optarg = &argv[z_optind++][sp+1];
else if(++z_optind >= argc) {
ERR(": option requires an argument -- ", c);
sp = 1;
return('?');
} else
z_optarg = argv[z_optind++];
sp = 1;
} else if (*cp == ';') {
if (argv[z_optind][sp+1] != '\0')
z_optarg = &argv[z_optind][sp+1];
else
z_optarg = 0;
z_optind++;
sp = 1;
} else {
if(argv[z_optind][++sp] == '\0') {
sp = 1;
z_optind++;
}
z_optarg = 0;
}
return(c);
}
#ifdef DEBUG
char *pgm;
void
quote(char *s, int mustquote)
{
char *q;
if (s == 0 || *s == 0) {
if (mustquote)
puts("''");
else
putchar('\n');
return;
}
for (q=s; *q; ++q)
if (!isalnum(*q))
break;
if (*q) {
putchar('\'');
for (q=s; *q; ++q)
if (*q == '\'')
printf("'\"'\"'");
else
putchar(*q);
putchar('\'');
putchar('\n');
}
else
puts(s);
}
long
main(int argc, char **argv)
{
int c;
char *optstr = argv[1];
#if HAVE_BASENAME
pgm = basename(argv[0]);
#else
{ char *avp = strrchr(argv[0], '/');
pgm = avp ? (1+avp) : argv[0];
}
#endif
if (argc < 2) {
fprintf(stderr, "usage: %s optstr [args]\n", pgm);
exit(1);
}
++argv, --argc;
while ((c = z_getopt(argc, argv, optstr)) != EOF) {
printf("-%c", c);
quote(z_optarg, 0);
}
printf("--\n");
argc -= z_optind;
argv += z_optind;
for (c=0; c < argc; c++)
quote(argv[c], 1);
exit(0);
}
#endif