sgetopt.c (1278B)
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 #define SGETOPTNOSHORT 13 #include "sgetopt.h" 14 15 #include "substdio.h" 16 #include "subfd.h" 17 #define SUBGETOPTNOSHORT 18 #include "subgetopt.h" 19 20 #define getopt sgetoptmine 21 #define optind subgetoptind 22 #define opterr sgetopterr 23 #define optproblem subgetoptproblem 24 #define optprogname sgetoptprogname 25 26 int opterr = 1; 27 char *optprogname = 0; 28 29 int getopt(argc,argv,opts) 30 int argc; 31 char **argv; 32 char *opts; 33 { 34 int c; 35 char *s; 36 37 if (!optprogname) { 38 optprogname = *argv; 39 if (!optprogname) optprogname = ""; 40 for (s = optprogname;*s;++s) if (*s == '/') optprogname = s + 1; 41 } 42 c = subgetopt(argc,argv,opts); 43 if (opterr) 44 if (c == '?') { 45 char chp[2]; chp[0] = optproblem; chp[1] = '\n'; 46 substdio_puts(subfderr,optprogname); 47 if (argv[optind] && (optind < argc)) 48 substdio_puts(subfderr,": illegal option -- "); 49 else 50 substdio_puts(subfderr,": option requires an argument -- "); 51 substdio_put(subfderr,chp,2); 52 substdio_flush(subfderr); 53 } 54 return c; 55 }