home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / std_unix / mod.std.unix.v1 / text0020.txt < prev    next >
Encoding:
Internet Message Format  |  1987-06-30  |  3.4 KB

  1. From: John Chambers (guest moderator) <ut-sally!std-unix>
  2.  
  3. Topic: getopt and command line arguments continued
  4.  
  5. ----------------------------------------------------------------------
  6.  
  7. Date: Wed, 10 Jul 85 15:14:05 edt
  8. From: harvard!talcott!wjh12!mirror!rs@ut-sally.ARPA (Rich Salz)
  9. Subject: Re;  bostic's getopt
  10.  
  11.  
  12. I have modified Keith Bostic's getopt code to make three changes:
  13.     1.  my own (warped) esthetics.
  14.     2.  add non-stdio code compile-time flag.
  15.     3.  add trivial code to handle the opterr variable.
  16. This last change is the most important; opterr, if set to 0 in
  17. mainline code, suppresses printing all error messages.
  18.  
  19. i also cleaned up a couple of things.
  20.  
  21. feel free to re-post this to std-unix, or even {net,mod}.sourceds
  22. if you think it will be worthwhile...
  23.  
  24. --
  25. Rich $alz    {mit-eddie, ihnp4!inmet, wjh12, cca, datacube} !mirror!rs
  26. Mirror Systems    2067 Massachusetts Ave.
  27. 617-661-0777    Cambridge, MA, 02140
  28.  
  29. /*
  30. **  This is a public domain version of getopt(3).
  31. **  Bugs, fixes to:
  32. **        Keith Bostic
  33. **            ARPA: keith@seismo
  34. **            UUCP: seismo!keith
  35. **  Added NO_STDIO, opterr handling, Rich $alz (mirror!rs).
  36. */
  37.  
  38. #include <stdio.h>
  39.  
  40. /*
  41. **  Error macro.  Maybe we want stdio, maybe we don't.
  42. **  The (undocumented?) variable opterr tells us whether or not
  43. **  to print errors.
  44. */
  45.  
  46. #ifdef    NO_STDIO
  47.  
  48. #define tell(s)                                \
  49.     if (opterr)                            \
  50.     {                                \
  51.         char    ebuf[2];                    \
  52.         (void)write(2, nargv, (unsigned int)strlen(nargv));        \
  53.         (void)write(2, s, (unsigned int)strlen(s));            \
  54.         ebuf[0] = optopt;                        \
  55.         ebuf[1] = '\n';                        \
  56.         (void)write(2, ebuf, 2);                    \
  57.     }
  58.  
  59. #else
  60.  
  61. #define tell(s)                                \
  62.     if (opterr)                            \
  63.         (void)fputs(*nargv, stderr),                \
  64.         (void)fputs(s,stderr),                    \
  65.         (void)fputc(optopt, stderr),                \
  66.         (void)fputc('\n', stderr)
  67.  
  68. #endif
  69.  
  70.  
  71. /* Global variables. */
  72. static char     EMSG[] = "";
  73. int         opterr = 1;        /* undocumented error-suppressor*/
  74. int         optind = 1,        /* index into argv vector    */
  75. int         optopt;        /* char checked for validity    */
  76. char        *optarg;        /* arg associated with option    */
  77.  
  78.  
  79. /* Linked in later. */
  80. extern char    *index();        /* This may be strchr        */
  81.  
  82.  
  83. getopt(nargc, nargv, ostr)
  84.     int              nargc;
  85.     char        **nargv;
  86.     char         *ostr;
  87. {
  88.     static char         *place = EMSG;    /* option letter processing    */
  89.     register char     *oli;        /* option letter list index    */
  90.  
  91.     if (!*place)            /* update scanning pointer    */
  92.     {
  93.     if (optind >= nargc || *(place = nargv[optind]) != '-' || !*++place)
  94.         return(EOF);
  95.     if (*place == '-')        /* found "--"            */
  96.     {
  97.         optind++;
  98.         return(EOF);
  99.     }
  100.     }
  101.     /* option letter okay? */
  102.     if ((optopt = *place++) == ':' || (oli = index(ostr, optopt)) == NULL)
  103.     {
  104.     if (!*place)
  105.         optind++;
  106.     tell(": illegal option -- ");
  107.     goto Bad;
  108.     }
  109.     if (*++oli != ':')            /* don't need argument        */
  110.     {
  111.     optarg = NULL;
  112.     if (!*place)
  113.         optind++;
  114.     }
  115.     else                /* need an argument        */
  116.     {
  117.     if (*place)
  118.         optarg = place;        /* no white space        */
  119.     else
  120.         if (nargc <= ++optind)
  121.         {
  122.         place = EMSG;
  123.         tell(": option requires an argument -- ");
  124.         goto Bad;
  125.         }
  126.         else
  127.         optarg = nargv[optind];    /* white space            */
  128.     place = EMSG;
  129.     optind++;
  130.     }
  131.     return(optopt);            /* dump back option letter    */
  132. Bad:
  133.     return('?');
  134. }
  135.  
  136.  
  137.  
  138. ----------------------------------------------------------------------
  139.  
  140. -- 
  141.  
  142. John B. Chambers, Microelectronics and Computer Technology Corp., Austin, TX
  143. {ihnp4,seismo,ctvax}!ut-sally!mcc-db!jbc, jbc@ut-sally.ARPA, chambers@mcc.ARPA
  144.  
  145. Volume-Number: Volume 1, Number 21
  146.  
  147.