Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / externals / figlet / getopt.c
blob421899152c99976008f6515feb54e1cd359597f5
1 /*
2 * * @(#)getopt.c 2.3 (smail) 5/30/87
3 */
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.
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.
25 #ifdef BSD
26 #include <strings.h>
27 #else
28 #define index strchr
29 #include <string.h>
30 #endif
32 /* LINTLIBRARY */
33 #ifndef NULL
34 #define NULL 0
35 #endif
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);}
46 extern char *index();
48 int opterr = 1;
49 int optind = 1;
50 int optopt;
51 char *optarg;
53 int getopt(int argc, char *argv[], char *opts)
55 static int sp = 1;
56 register int c;
57 register char *cp;
59 if (sp == 1)
61 if (optind >= argc || argv[optind][0] != '-' ||
62 argv[optind][1] == '\0')
63 return (EOF);
64 else if (strcmp(argv[optind], "--") == 0)
66 optind++;
67 return (EOF);
70 optopt = c = argv[optind][sp];
71 if (c == ':' || (cp = index(opts, c)) == NULL)
73 ERR(": illegal option -- ", c);
74 if (argv[optind][++sp] == '\0')
76 optind++;
77 sp = 1;
79 return ('?');
81 if (*++cp == ':')
83 if (argv[optind][sp + 1] != '\0')
84 optarg = &argv[optind++][sp + 1];
85 else if (++optind >= argc)
87 ERR(": option requires an argument -- ", c);
88 sp = 1;
89 return ('?');
91 else optarg = argv[optind++];
92 sp = 1;
94 else
96 if (argv[optind][++sp] == '\0')
98 sp = 1;
99 optind++;
101 optarg = NULL;
103 return (c);