"Fossies" - the Fresh Open Source Software Archive

Member "getopt.c" (30 Sep 2004, 1436 Bytes) of package /linux/privat/old/dirsync-1_11.tar.gz:


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 /*
    2 **  GETOPT PROGRAM AND LIBRARY ROUTINE
    3 **
    4 **  I wrote main() and AT&T wrote getopt() and we both put our efforts into
    5 **  the public domain via mod.sources.
    6 **  Rich $alz
    7 **  Mirror Systems
    8 **  (mirror!rs, rs@mirror.TMC.COM)
    9 **  August 10, 1986
   10 ** 
   11 **  This is the public-domain AT&T getopt(3) code.  Hacked by Rich and by Jim.
   12 */
   13 static char rcsinfo[] = "$Id: getopt.c,v 1.2 2004/09/30 18:05:39 mviara Exp $";
   14 
   15 #include <stdio.h>
   16 #include <string.h>
   17 
   18 #define ERR(_s, _c) { if (opterr) fprintf (stderr, "%s %s %c\n", argv[0], _s, _c);}
   19 
   20 int opterr = 1;
   21 int optind = 1;
   22 int optopt;
   23 char    *optarg;
   24 
   25 int   getopt(int argc, char **argv, char *opts)
   26 {
   27     static int sp = 1;
   28     register int c;
   29     register char *cp;
   30 
   31     if(sp == 1)
   32         if(optind >= argc ||
   33            argv[optind][0] != '-' || argv[optind][1] == '\0')
   34             return(EOF);
   35         else if(strcmp(argv[optind], "--") == 0) {
   36             optind++;
   37             return(EOF);
   38         }
   39     optopt = c = argv[optind][sp];
   40     if(c == ':' ||
   41        (cp=strchr(opts, c)) == NULL) {
   42         ERR(": illegal option -- ", c);
   43         if(argv[optind][++sp] == '\0') {
   44             optind++;
   45             sp = 1;
   46         }
   47         return('?');
   48     }
   49     if(*++cp == ':') {
   50         if(argv[optind][sp+1] != '\0')
   51             optarg = &argv[optind++][sp+1];
   52         else if(++optind >= argc) {
   53             ERR(": option requires an argument -- ", c);
   54             sp = 1;
   55             return('?');
   56         } else
   57             optarg = argv[optind++];
   58         sp = 1;
   59     } else {
   60         if(argv[optind][++sp] == '\0') {
   61             sp = 1;
   62             optind++;
   63         }
   64         optarg = NULL;
   65     }
   66     return(c);
   67 }
   68