home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / kit / part01 / des / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  1.6 KB  |  66 lines

  1. /* got this off net.sources */
  2. /*
  3.  * $Id: getopt.c,v 2.0.1.1 91/04/01 15:39:52 ram Exp $
  4.  *
  5.  * $Log:    getopt.c,v $
  6.  * Revision 2.0.1.1  91/04/01  15:39:52  ram
  7.  * patch1: created
  8.  * 
  9.  */
  10.  
  11. #include "../config.h"
  12. #ifndef GETOPT
  13. #include <stdio.h>
  14.  
  15. /*
  16.  * get option letter from argument vector
  17.  */
  18. int    opterr = 1,        /* useless, never set or used */
  19.     optind = 1,        /* index into parent argv vector */
  20.     optopt;            /* character checked for validity */
  21. char    *optarg;        /* argument associated with option */
  22.  
  23. #define BADCH    (int)'?'
  24. #define EMSG    ""
  25. #define tell(s)    fputs(*nargv,stderr);fputs(s,stderr); \
  26.         fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
  27.  
  28. getopt(nargc,nargv,ostr)
  29. int    nargc;
  30. char    **nargv,
  31.     *ostr;
  32. {
  33.     static char    *place = EMSG;    /* option letter processing */
  34.     register char    *oli;        /* option letter list index */
  35.     char    *index();
  36.  
  37.     if(!*place) {            /* update scanning pointer */
  38.         if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF);
  39.         if (*place == '-') {    /* found "--" */
  40.             ++optind;
  41.             return(EOF);
  42.         }
  43.     }                /* option letter okay? */
  44.     if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) {
  45.         if(!*place) ++optind;
  46.         tell(": illegal option -- ");
  47.     }
  48.     if (*++oli != ':') {        /* don't need argument */
  49.         optarg = NULL;
  50.         if (!*place) ++optind;
  51.     }
  52.     else {                /* need an argument */
  53.         if (*place) optarg = place;    /* no white space */
  54.         else if (nargc <= ++optind) {    /* no arg */
  55.             place = EMSG;
  56.             tell(": option requires an argument -- ");
  57.         }
  58.          else optarg = nargv[optind];    /* white space */
  59.         place = EMSG;
  60.         ++optind;
  61.     }
  62.     return(optopt);            /* dump back option letter */
  63. }
  64. #endif
  65.  
  66.