"Fossies" - the Fresh Open Source Software Archive

Member "c_count-7.20/porting/getopt.c" (7 Oct 2013, 2371 Bytes) of package /linux/privat/c_count-7.20.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "getopt.c" see the Fossies "Dox" file reference documentation.

    1 /* ::[[ @(#) getopt.c 1.5 89/03/11 05:40:23 ]]:: */
    2 #ifndef LINT
    3 static const char Id[] = "$Id: getopt.c,v 1.3 2013/10/07 16:44:22 tom Exp $";
    4 #endif
    5 
    6 /*
    7  * Here's something you've all been waiting for:  the AT&T public domain
    8  * source for getopt(3).  It is the code which was given out at the 1985
    9  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
   10  * directly from AT&T.  The people there assure me that it is indeed
   11  * in the public domain.
   12  *
   13  * There is no manual page.  That is because the one they gave out at
   14  * UNIFORUM was slightly different from the current System V Release 2
   15  * manual page.  The difference apparently involved a note about the
   16  * famous rules 5 and 6, recommending using white space between an option
   17  * and its first argument, and not grouping options that have arguments.
   18  * Getopt itself is currently lenient about both of these things White
   19  * space is allowed, but not mandatory, and the last option in a group can
   20  * have an argument.  That particular version of the man page evidently
   21  * has no official existence, and my source at AT&T did not send a copy.
   22  * The current SVR2 man page reflects the actual behavor of this getopt.
   23  * However, I am not about to post a copy of anything licensed by AT&T.
   24  */
   25 
   26 #include <stdio.h>
   27 #include <string.h>
   28 #include <getopt.h>
   29 
   30 #define ERR(szz,czz) if(opterr){fprintf(stderr,"%s%s%c\n",argv[0],szz,czz);}
   31 
   32 int opterr = 1;
   33 int optind = 1;
   34 int optopt;
   35 char *optarg;
   36 
   37 int
   38 getopt(int argc, char **argv, const char *opts)
   39 {
   40     static int sp = 1;
   41     register int c;
   42     register char *cp;
   43 
   44     if (sp == 1) {
   45     if (optind >= argc ||
   46         argv[optind][0] != '-' || argv[optind][1] == '\0')
   47         return (EOF);
   48     else if (strcmp(argv[optind], "--") == 0) {
   49         optind++;
   50         return (EOF);
   51     }
   52     }
   53     optopt = c = argv[optind][sp];
   54     if (c == ':' || (cp = strchr(opts, c)) == NULL) {
   55     ERR(": illegal option -- ", c);
   56     if (argv[optind][++sp] == '\0') {
   57         optind++;
   58         sp = 1;
   59     }
   60     return ('?');
   61     }
   62     if (*++cp == ':') {
   63     if (argv[optind][sp + 1] != '\0')
   64         optarg = &argv[optind++][sp + 1];
   65     else if (++optind >= argc) {
   66         ERR(": option requires an argument -- ", c);
   67         sp = 1;
   68         return ('?');
   69     } else
   70         optarg = argv[optind++];
   71     sp = 1;
   72     } else {
   73     if (argv[optind][++sp] == '\0') {
   74         sp = 1;
   75         optind++;
   76     }
   77     optarg = NULL;
   78     }
   79     return (c);
   80 }