Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/externals/figlet/getopt.c
12240 views
1
/*
2
* * @(#)getopt.c 2.3 (smail) 5/30/87
3
*/
4
5
/*
6
* Here's something you've all been waiting for: the AT&T public domain
7
* source for getopt(3). It is the code which was given out at the 1985
8
* UNIFORUM conference in Dallas. I obtained it by electronic mail directly
9
* from AT&T. The people there assure me that it is indeed in the public
10
* domain.
11
*
12
* There is no manual page. That is because the one they gave out at UNIFORUM
13
* was slightly different from the current System V Release 2 manual page.
14
* The difference apparently involved a note about the famous rules 5 and 6,
15
* recommending using white space between an option and its first argument,
16
* and not grouping options that have arguments. Getopt itself is currently
17
* lenient about both of these things White space is allowed, but not
18
* mandatory, and the last option in a group can have an argument. That
19
* particular version of the man page evidently has no official existence,
20
* and my source at AT&T did not send a copy. The current SVR2 man page
21
* reflects the actual behavior of this getopt. However, I am not about to
22
* post a copy of anything licensed by AT&T.
23
*/
24
25
#ifdef BSD
26
#include <strings.h>
27
#else
28
#define index strchr
29
#include <string.h>
30
#endif
31
32
/* LINTLIBRARY */
33
#ifndef NULL
34
#define NULL 0
35
#endif
36
37
#define EOF (-1)
38
#define ERR(s, c) if(opterr){\
39
extern int write(int, void *, unsigned);\
40
char errbuf[2];\
41
errbuf[0] = (char)c; errbuf[1] = '\n';\
42
(void) write(2, strlwr(argv[0]), (unsigned)strlen(argv[0]));\
43
(void) write(2, s, (unsigned)strlen(s));\
44
(void) write(2, errbuf, 2);}
45
46
extern char *index();
47
48
int opterr = 1;
49
int optind = 1;
50
int optopt;
51
char *optarg;
52
53
int getopt(int argc, char *argv[], char *opts)
54
{
55
static int sp = 1;
56
register int c;
57
register char *cp;
58
59
if (sp == 1)
60
{
61
if (optind >= argc || argv[optind][0] != '-' ||
62
argv[optind][1] == '\0')
63
return (EOF);
64
else if (strcmp(argv[optind], "--") == 0)
65
{
66
optind++;
67
return (EOF);
68
}
69
}
70
optopt = c = argv[optind][sp];
71
if (c == ':' || (cp = index(opts, c)) == NULL)
72
{
73
ERR(": illegal option -- ", c);
74
if (argv[optind][++sp] == '\0')
75
{
76
optind++;
77
sp = 1;
78
}
79
return ('?');
80
}
81
if (*++cp == ':')
82
{
83
if (argv[optind][sp + 1] != '\0')
84
optarg = &argv[optind++][sp + 1];
85
else if (++optind >= argc)
86
{
87
ERR(": option requires an argument -- ", c);
88
sp = 1;
89
return ('?');
90
}
91
else optarg = argv[optind++];
92
sp = 1;
93
}
94
else
95
{
96
if (argv[optind][++sp] == '\0')
97
{
98
sp = 1;
99
optind++;
100
}
101
optarg = NULL;
102
}
103
return (c);
104
}
105
106