"Fossies" - the Fresh Open Source Software Archive

Member "apg-2.2.3/getopt.c" (7 Aug 2003, 2195 Bytes) of package /linux/privat/old/apg-2.2.3.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.

    1 /*
    2  * Modified by Adel I. Mirzazhanov 2002, 2003
    3  * getopt - get option letter from argv
    4  *
    5  * This is a version of the public domain getopt() implementation by
    6  * Henry Spencer, changed for 4.3BSD compatibility (in addition to System V).
    7  * It allows rescanning of an option list by setting optind to 0 before
    8  * calling, which is why we use it even if the system has its own (in fact,
    9  * this one has a unique name so as not to conflict with the system's).
   10  * Thanks to Dennis Ferguson for the appropriate modifications.
   11  *
   12  * This file is in the Public Domain.
   13  */
   14 
   15 #include <stdio.h>
   16 #include "getopt.h"
   17 
   18 static int badopt(const char *mess,int ch);
   19 
   20 
   21 char    *apg_optarg;    /* Global argument pointer. */
   22 int apg_optind = 0; /* Global argv index. */
   23 int apg_opterr = 1; /* for compatibility, should error be printed? */
   24 int apg_optopt; /* for compatibility, option character checked */
   25 
   26 static char *scan = NULL;   /* Private scan pointer. */
   27 static const char   *prog = "apg";
   28 
   29 /*
   30  * Print message about a bad option.
   31  */
   32 static int
   33 badopt(const char *mess,int ch)
   34 {
   35     if (apg_opterr) {
   36         fprintf(stderr,"%s%s%c\n", prog, mess, ch);
   37         fflush(stderr);
   38     }
   39     return ('?');
   40 }
   41 
   42 int
   43 apg_getopt(int argc,char *argv[],const char *optstring)
   44 {
   45     register char c;
   46     register const char *place;
   47 
   48     prog = argv[0];
   49     apg_optarg = NULL;
   50 
   51     if (apg_optind == 0) {
   52         scan = NULL;
   53         apg_optind++;
   54     }
   55     
   56     if (scan == NULL || *scan == '\0') {
   57         if (apg_optind >= argc
   58             || argv[apg_optind][0] != '-'
   59             || argv[apg_optind][1] == '\0') {
   60             return (EOF);
   61         }
   62         if (argv[apg_optind][1] == '-'
   63             && argv[apg_optind][2] == '\0') {
   64             apg_optind++;
   65             return (EOF);
   66         }
   67     
   68         scan = argv[apg_optind++]+1;
   69     }
   70 
   71     c = *scan++;
   72     apg_optopt = c & 0377;
   73     for (place = optstring; place != NULL && *place != '\0'; ++place)
   74         if (*place == c)
   75         break;
   76 
   77     if (place == NULL || *place == '\0' || c == ':' || c == '?') {
   78         return (badopt(": unknown option -", c));
   79     }
   80 
   81     place++;
   82     if (*place == ':') {
   83         if (*scan != '\0') {
   84             apg_optarg = scan;
   85             scan = NULL;
   86         } else if (apg_optind >= argc) {
   87             return (badopt(": option requires an argument -", c));
   88         } else {
   89             apg_optarg = argv[apg_optind++];
   90         }
   91     }
   92 
   93     return (c & 0377);
   94 }