home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / windows3 / mtlabsnd.zip / GETOPT.C < prev    next >
C/C++ Source or Header  |  1993-04-26  |  3KB  |  110 lines

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