/*1* * @(#)getopt.c 2.3 (smail) 5/30/872*/34/*5* Here's something you've all been waiting for: the AT&T public domain6* source for getopt(3). It is the code which was given out at the 19857* UNIFORUM conference in Dallas. I obtained it by electronic mail directly8* from AT&T. The people there assure me that it is indeed in the public9* domain.10*11* There is no manual page. That is because the one they gave out at UNIFORUM12* was slightly different from the current System V Release 2 manual page.13* The difference apparently involved a note about the famous rules 5 and 6,14* recommending using white space between an option and its first argument,15* and not grouping options that have arguments. Getopt itself is currently16* lenient about both of these things White space is allowed, but not17* mandatory, and the last option in a group can have an argument. That18* particular version of the man page evidently has no official existence,19* and my source at AT&T did not send a copy. The current SVR2 man page20* reflects the actual behavior of this getopt. However, I am not about to21* post a copy of anything licensed by AT&T.22*/2324#ifdef BSD25#include <strings.h>26#else27#define index strchr28#include <string.h>29#endif3031/* LINTLIBRARY */32#ifndef NULL33#define NULL 034#endif3536#define EOF (-1)37#define ERR(s, c) if(opterr){\38extern int write(int, void *, unsigned);\39char errbuf[2];\40errbuf[0] = (char)c; errbuf[1] = '\n';\41(void) write(2, strlwr(argv[0]), (unsigned)strlen(argv[0]));\42(void) write(2, s, (unsigned)strlen(s));\43(void) write(2, errbuf, 2);}4445extern char *index();4647int opterr = 1;48int optind = 1;49int optopt;50char *optarg;5152int getopt(int argc, char *argv[], char *opts)53{54static int sp = 1;55register int c;56register char *cp;5758if (sp == 1)59{60if (optind >= argc || argv[optind][0] != '-' ||61argv[optind][1] == '\0')62return (EOF);63else if (strcmp(argv[optind], "--") == 0)64{65optind++;66return (EOF);67}68}69optopt = c = argv[optind][sp];70if (c == ':' || (cp = index(opts, c)) == NULL)71{72ERR(": illegal option -- ", c);73if (argv[optind][++sp] == '\0')74{75optind++;76sp = 1;77}78return ('?');79}80if (*++cp == ':')81{82if (argv[optind][sp + 1] != '\0')83optarg = &argv[optind++][sp + 1];84else if (++optind >= argc)85{86ERR(": option requires an argument -- ", c);87sp = 1;88return ('?');89}90else optarg = argv[optind++];91sp = 1;92}93else94{95if (argv[optind][++sp] == '\0')96{97sp = 1;98optind++;99}100optarg = NULL;101}102return (c);103}104105106