home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume1 / getopt < prev    next >
Internet Message Format  |  1986-11-30  |  3KB

  1. Date: Tue, 25 Dec 84 19:20:50 EST
  2. From: Keith Bostic <harvard!seismo!keith>
  3. To: genrad!sources
  4. Subject: public domain getopt(3)
  5.  
  6. There have recently been several requests for a public
  7. domain version of getopt(3), recently.  Thought this
  8. might be worth reposting.
  9.  
  10.         Keith Bostic
  11.             ARPA: keith@seismo 
  12.             UUCP: seismo!keith
  13.  
  14. ======================================================================
  15. In April of this year, Henry Spencer (utzoo!henry) released a public
  16. domain version of getopt (USG, getopt(3)).  Well, I've been trying to
  17. port some USG dependent software and it didn't seem to work.  The problem
  18. ended up being that the USG version of getopt has some external variables
  19. that aren't mentioned in the documentation.  Anyway, to fix these problems,
  20. I rewrote the public version of getopt.  It has the following advantages:
  21.  
  22.     -- it includes those "unknown" variables
  23.     -- it's smaller/faster 'cause it doesn't use the formatted
  24.         output conversion routines in section 3 of the UNIX manual.
  25.     -- the error messages are the same as S5's.
  26.     -- it has the same side-effects that S5's has.
  27.     -- the posted bug on how the error messages are flushed has been
  28.         implemented.  (posting by Tony Hansen; pegasus!hansen)
  29.  
  30. I won't post the man pages since Henry already did; a special note,
  31. it's not documented in the S5 manual that the options ':' and '?' are
  32. illegal.  It should be obvious, but I thought I'd mention it...
  33. This software was derived from binaries of S5 and the S5 man page, and is
  34. (I think?) totally (I'm pretty sure?) compatible with S5 and backward
  35. compatible to Henry's version.
  36.  
  37.         Keith Bostic
  38.             ARPA: keith@seismo 
  39.             UUCP: seismo!keith
  40.  
  41. *UNIX is a trademark of Bell Laboratories
  42.  
  43. .. cut along the dotted line .........................................
  44.  
  45. #include <stdio.h>
  46.  
  47. /*
  48.  * get option letter from argument vector
  49.  */
  50. int    opterr = 1,        /* useless, never set or used */
  51.     optind = 1,        /* index into parent argv vector */
  52.     optopt;            /* character checked for validity */
  53. char    *optarg;        /* argument associated with option */
  54.  
  55. #define BADCH    (int)'?'
  56. #define EMSG    ""
  57. #define tell(s)    fputs(*nargv,stderr);fputs(s,stderr); \
  58.         fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
  59.  
  60. getopt(nargc,nargv,ostr)
  61. int    nargc;
  62. char    **nargv,
  63.     *ostr;
  64. {
  65.     static char    *place = EMSG;    /* option letter processing */
  66.     register char    *oli;        /* option letter list index */
  67.     char    *index();
  68.  
  69.     if(!*place) {            /* update scanning pointer */
  70.         if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF);
  71.         if (*place == '-') {    /* found "--" */
  72.             ++optind;
  73.             return(EOF);
  74.         }
  75.     }                /* option letter okay? */
  76.     if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) {
  77.         if(!*place) ++optind;
  78.         tell(": illegal option -- ");
  79.     }
  80.     if (*++oli != ':') {        /* don't need argument */
  81.         optarg = NULL;
  82.         if (!*place) ++optind;
  83.     }
  84.     else {                /* need an argument */
  85.         if (*place) optarg = place;    /* no white space */
  86.         else if (nargc <= ++optind) {    /* no arg */
  87.             place = EMSG;
  88.             tell(": option requires an argument -- ");
  89.         }
  90.          else optarg = nargv[optind];    /* white space */
  91.         place = EMSG;
  92.         ++optind;
  93.     }
  94.     return(optopt);            /* dump back option letter */
  95. }
  96.  
  97.  
  98.  
  99.