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

  1. Date: Mon, 8 Jul 85 12:51:54 EDT
  2. From: Keith Bostic <seismo!keith>
  3. To: ut-sally!std-unix
  4. Subject: Public domain version of getopt(3)
  5.  
  6.  
  7. John, you posted Henry Spencer's original version of getopt (message
  8. #11, I believe) unless you've posted two versions.  [ I've posted only
  9. one before, which was Henry Spencer's.  -mod ] The reason that I
  10. reimplemented after that version was:
  11.  
  12.     1: his used the stdio library printf call, thus forcing the
  13.         loading of some large routines even if your program
  14.         didn't need them.
  15.     2: his didn't have the same error messages that the SysV one had.
  16.     3: his didn't use certain external variables (optarg,
  17.         opterr, optind and optopt) that the SysV one had.
  18.  
  19. Obviously, #3 is the real reason, the others were bug fixes if
  20. anything.  In any case, the version I sent to you (it's included below
  21. if you've lost it) [ I have the one from net.sources, but I thought it
  22. best to get whatever your latest version is.  If you mailed me one
  23. before, it never arrived, possibly because my sun crashed about that
  24. time.  -mod ] is the version that will be released with 4.3BSD and, as
  25. far as I know, is totally compatible with SysV.  [ How about sending
  26. it to mod.sources, as well?  -mod ]
  27.  
  28. --keith
  29. ======================================================================
  30.  
  31. #include <stdio.h>
  32.  
  33. /*
  34.  * this is a public domain version of getopt(3).
  35.  * bugs, fixes to:
  36.  *        Keith Bostic
  37.  *            ARPA: keith@seismo
  38.  *            UUCP: seismo!keith
  39.  */
  40.  
  41. /*
  42.  * get option letter from argument vector
  43.  */
  44. int    opterr = 1,        /* useless, never set or used */
  45.     optind = 1,        /* index into parent argv vector */
  46.     optopt;            /* character checked for validity */
  47. char    *optarg;        /* argument associated with option */
  48.  
  49. #define BADCH    (int)'?'
  50. #define EMSG    ""
  51. #define tell(s)    fputs(*nargv,stderr);fputs(s,stderr); \
  52.         fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
  53.  
  54. getopt(nargc,nargv,ostr)
  55. int    nargc;
  56. char    **nargv,
  57.     *ostr;
  58. {
  59.     static char    *place = EMSG;    /* option letter processing */
  60.     register char    *oli;        /* option letter list index */
  61.     char    *index();
  62.  
  63.     if(!*place) {            /* update scanning pointer */
  64.         if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF);
  65.         if (*place == '-') {    /* found "--" */
  66.             ++optind;
  67.             return(EOF);
  68.         }
  69.     }                /* option letter okay? */
  70.     if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) {
  71.         if(!*place) ++optind;
  72.         tell(": illegal option -- ");
  73.     }
  74.     if (*++oli != ':') {        /* don't need argument */
  75.         optarg = NULL;
  76.         if (!*place) ++optind;
  77.     }
  78.     else {                /* need an argument */
  79.         if (*place) optarg = place;    /* no white space */
  80.         else if (nargc <= ++optind) {    /* no arg */
  81.             place = EMSG;
  82.             tell(": option requires an argument -- ");
  83.         }
  84.          else optarg = nargv[optind];    /* white space */
  85.         place = EMSG;
  86.         ++optind;
  87.     }
  88.     return(optopt);            /* dump back option letter */
  89. }
  90.  
  91. -- 
  92.  
  93. John Quarterman,   UUCP:  {ihnp4,seismo,harvard,gatech}!ut-sally!jsq
  94. ARPA Internet and CSNET:  jsq@ut-sally.ARPA, soon to be jsq@sally.UTEXAS.EDU
  95.  
  96. Volume-Number: Volume 1, Number 14
  97.  
  98.