"Fossies" - the Fresh Open Source Software Archive

Member "ngrep-1_47/win32/support/getopt.c" (7 Sep 2017, 4687 Bytes) of package /linux/misc/ngrep-1_47.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 and the latest Fossies "Diffs" side-by-side code changes report: 1_45_vs_1_47.

    1 
    2 #include <stdio.h>                  /* for EOF */ 
    3 #include <string.h>                 /* for strchr() */ 
    4 
    5 #include <getopt.h> 
    6  
    7 /* static (global) variables that are specified as exported by getopt() */ 
    8 char *optarg = NULL;    /* pointer to the start of the option argument  */ 
    9 int   optind = 1;       /* number of the next argv[] to be evaluated    */ 
   10 int   opterr = 1;       /* non-zero if a question mark should be returned 
   11                            when a non-valid option character is detected */
   12 
   13 int getopt(int argc, char *argv[], char *opstring) { 
   14   static char *pIndexPosition = NULL; /* place inside current argv string */ 
   15   char *pArgString = NULL;        /* where to start from next */ 
   16   char *pOptString;               /* the string in our program */ 
   17  
   18   if (pIndexPosition != NULL) { 
   19     /* we last left off inside an argv string */ 
   20     if (*(++pIndexPosition)) { 
   21       /* there is more to come in the most recent argv */ 
   22       pArgString = pIndexPosition; 
   23     } 
   24   } 
   25  
   26   if (pArgString == NULL) { 
   27     /* we didn't leave off in the middle of an argv string */ 
   28     if (optind >= argc) { 
   29       /* more command-line arguments than the argument count */ 
   30       pIndexPosition = NULL;  /* not in the middle of anything */ 
   31       return EOF;             /* used up all command-line arguments */ 
   32     } 
   33  
   34     /*--------------------------------------------------------------------- 
   35      * If the next argv[] is not an option, there can be no more options. 
   36      *-------------------------------------------------------------------*/ 
   37     pArgString = argv[optind++]; /* set this to the next argument ptr */ 
   38  
   39     if (('/' != *pArgString) && /* doesn't start with a slash or a dash? */ 
   40     ('-' != *pArgString)) { 
   41       --optind;               /* point to current arg once we're done */ 
   42       optarg = NULL;          /* no argument follows the option */ 
   43       pIndexPosition = NULL;  /* not in the middle of anything */ 
   44       return EOF;             /* used up all the command-line flags */ 
   45     } 
   46 
   47     /* check for special end-of-flags markers */ 
   48     if ((strcmp(pArgString, "-") == 0) || 
   49     (strcmp(pArgString, "--") == 0)) { 
   50       optarg = NULL;          /* no argument follows the option */ 
   51       pIndexPosition = NULL;  /* not in the middle of anything */ 
   52       return EOF;             /* encountered the special flag */ 
   53     } 
   54  
   55     pArgString++;               /* look past the / or - */ 
   56   } 
   57  
   58   if (':' == *pArgString) {       /* is it a colon? */ 
   59     /*--------------------------------------------------------------------- 
   60      * Rare case: if opterr is non-zero, return a question mark; 
   61      * otherwise, just return the colon we're on. 
   62      *-------------------------------------------------------------------*/ 
   63     return (opterr ? (int)'?' : (int)':'); 
   64   } else if ((pOptString = strchr(opstring, *pArgString)) == 0) { 
   65     /*--------------------------------------------------------------------- 
   66      * The letter on the command-line wasn't any good. 
   67      *-------------------------------------------------------------------*/ 
   68     optarg = NULL;              /* no argument follows the option */ 
   69     pIndexPosition = NULL;      /* not in the middle of anything */ 
   70     return (opterr ? (int)'?' : (int)*pArgString); 
   71   } else { 
   72     /*--------------------------------------------------------------------- 
   73      * The letter on the command-line matches one we expect to see 
   74      *-------------------------------------------------------------------*/ 
   75     if (':' == _next_char(pOptString)) { /* is the next letter a colon? */ 
   76       /* It is a colon.  Look for an argument string. */ 
   77       if ('\0' != _next_char(pArgString))  /* argument in this argv? */ 
   78     optarg = &pArgString[1];   /* Yes, it is */ 
   79       else { 
   80     /*------------------------------------------------------------- 
   81      * The argument string must be in the next argv. 
   82      * But, what if there is none (bad input from the user)? 
   83      * In that case, return the letter, and optarg as NULL. 
   84      *-----------------------------------------------------------*/ 
   85     if (optind < argc) 
   86       optarg = argv[optind++]; 
   87     else { 
   88       optarg = NULL; 
   89       return (opterr ? (int)'?' : (int)*pArgString); 
   90     } 
   91       } 
   92 
   93       pIndexPosition = NULL;  /* not in the middle of anything */ 
   94     } else { 
   95       /* it's not a colon, so just return the letter */ 
   96       optarg = NULL;          /* no argument follows the option */ 
   97       pIndexPosition = pArgString;    /* point to the letter we're on */ 
   98     } 
   99     return (int)*pArgString;    /* return the letter that matched */ 
  100   } 
  101 }