"Fossies" - the Fresh Open Source Software Archive

Member "heaplayers-351/benchmarks/espresso/getopt.c" (12 Aug 2004, 1095 Bytes) of package /linux/misc/old/heaplayers_3_5_1.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 #include "espresso.h"
    2 #include "port.h"
    3 /*  File   : getopt.c
    4     Author : Henry Spencer, University of Toronto
    5     Updated: 28 April 1984
    6     Purpose: get option letter from argv.
    7 */
    8 #define NullS   ((char *) 0)
    9 
   10 char    *optarg;    /* Global argument pointer. */
   11 int optind = 0; /* Global argv index. */
   12 
   13 int getopt(int argc, char * const * argv, const char * optstring)
   14     {
   15     register int c;
   16     register char *place;
   17     static char *scan = NullS;  /* Private scan pointer. */
   18 
   19     optarg = NullS;
   20 
   21     if (scan == NullS || *scan == '\0') {
   22         if (optind == 0) optind++;
   23         if (optind >= argc) return EOF;
   24         place = argv[optind];
   25         if (place[0] != '-' || place[1] == '\0') return EOF;
   26         optind++;
   27         if (place[1] == '-' && place[2] == '\0') return EOF;
   28         scan = place+1;
   29     }
   30 
   31     c = *scan++;
   32     place = strchr(optstring, c);
   33     if (place == NullS || c == ':') {
   34         fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
   35         return '?';
   36     }
   37     if (*++place == ':') {
   38         if (*scan != '\0') {
   39         optarg = scan, scan = NullS;
   40         } else {
   41         optarg = argv[optind], optind++;
   42         }
   43     }
   44     return c;
   45     }