Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / network / stacks / AROSTCP / netlib / getopt.c
blobacd0f69183725ee728c69ea10a215cda5daa50bd
1 /* $Id$
3 * getopt.c - Unix compatible command line option parsing
4 */
6 /*
7 ** @(#)getopt.c 2.5 (smail) 9/15/87
8 */
11 * Here's something you've all been waiting for: the AT&T public domain
12 * source for getopt(3). It is the code which was given out at the 1985
13 * UNIFORUM conference in Dallas. I obtained it by electronic mail
14 * directly from AT&T. The people there assure me that it is indeed
15 * in the public domain.
17 * There is no manual page. That is because the one they gave out at
18 * UNIFORUM was slightly different from the current System V Release 2
19 * manual page. The difference apparently involved a note about the
20 * famous rules 5 and 6, recommending using white space between an option
21 * and its first argument, and not grouping options that have arguments.
22 * Getopt itself is currently lenient about both of these things White
23 * space is allowed, but not mandatory, and the last option in a group can
24 * have an argument. That particular version of the man page evidently
25 * has no official existence, and my source at AT&T did not send a copy.
26 * The current SVR2 man page reflects the actual behavor of this getopt.
27 * However, I am not about to post a copy of anything licensed by AT&T.
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
34 /*LINTLIBRARY*/
35 #define ERR(s, c)\
36 if(opterr) { fprintf(stderr, "%s%s%lc\n", argv[0], s, c); }
38 int opterr = 1;
39 int optind = 1;
40 int optopt;
41 char *optarg;
44 * Using the 'const' on the argv below increases code size on SAS/C 6.51 by
45 * 24 bytes, even when it should have no effect on the generated code!
47 int
48 getopt(int argc, char * /*const*/ argv[], char const *opts)
50 static int sp = 1;
51 register int c;
52 register char *cp;
54 if(sp == 1)
55 if(optind >= argc ||
56 argv[optind][0] != '-' || argv[optind][1] == '\0')
57 return(EOF);
58 else if(strcmp(argv[optind], "--") == NULL) {
59 optind++;
60 return(EOF);
62 optopt = c = argv[optind][sp];
63 if(c == ':' || (cp=index(opts, c)) == NULL) {
64 ERR(": illegal option -- ", c);
65 if(argv[optind][++sp] == '\0') {
66 optind++;
67 sp = 1;
69 return('?');
71 if(*++cp == ':') {
72 if(argv[optind][sp+1] != '\0')
73 optarg = &argv[optind++][sp+1];
74 else if(++optind >= argc) {
75 ERR(": option requires an argument -- ", c);
76 sp = 1;
77 return('?');
78 } else
79 optarg = argv[optind++];
80 sp = 1;
81 } else {
82 if(argv[optind][++sp] == '\0') {
83 sp = 1;
84 optind++;
86 optarg = NULL;
88 return(c);