sgetopt.c
changeset 0 068428edee47
equal deleted inserted replaced
-1:000000000000 0:068428edee47
       
     1 /* sgetopt.c, sgetopt.h: (yet another) improved getopt clone, outer layer
       
     2 D. J. Bernstein, djb@pobox.com.
       
     3 Depends on subgetopt.h, substdio.h, subfd.h.
       
     4 No system requirements.
       
     5 19970208: Cleanups.
       
     6 931201: Baseline.
       
     7 No known patent problems.
       
     8 
       
     9 Documentation in sgetopt.3.
       
    10 */
       
    11 
       
    12 #include "substdio.h"
       
    13 #include "subfd.h"
       
    14 #define SGETOPTNOSHORT
       
    15 #include "sgetopt.h"
       
    16 #define SUBGETOPTNOSHORT
       
    17 #include "subgetopt.h"
       
    18 
       
    19 #define getopt sgetoptmine
       
    20 #define optind subgetoptind
       
    21 #define opterr sgetopterr
       
    22 #define optproblem subgetoptproblem
       
    23 #define optprogname sgetoptprogname
       
    24 
       
    25 int opterr = 1;
       
    26 char *optprogname = 0;
       
    27 
       
    28 int getopt(argc,argv,opts)
       
    29 int argc;
       
    30 char **argv;
       
    31 char *opts;
       
    32 {
       
    33   int c;
       
    34   char *s;
       
    35 
       
    36   if (!optprogname) {
       
    37     optprogname = *argv;
       
    38     if (!optprogname) optprogname = "";
       
    39     for (s = optprogname;*s;++s) if (*s == '/') optprogname = s + 1;
       
    40   }
       
    41   c = subgetopt(argc,argv,opts);
       
    42   if (opterr)
       
    43     if (c == '?') {
       
    44       char chp[2]; chp[0] = optproblem; chp[1] = '\n';
       
    45       substdio_puts(subfderr,optprogname);
       
    46       if (argv[optind] && (optind < argc))
       
    47         substdio_puts(subfderr,": illegal option -- ");
       
    48       else
       
    49         substdio_puts(subfderr,": option requires an argument -- ");
       
    50       substdio_put(subfderr,chp,2);
       
    51       substdio_flush(subfderr);
       
    52     }
       
    53   return c;
       
    54 }