home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 182.lha / Calls / getopt.c < prev    next >
C/C++ Source or Header  |  1988-04-28  |  1KB  |  67 lines

  1. /* @(#)getopt.c */
  2.  
  3. #include <stdio.h>
  4.  
  5. #include "getopt.h"
  6.  
  7. /*
  8.  * get option letter from argument vector
  9.  */
  10. int
  11.     optind = 1,        /* index into parent argv vector */
  12.     optopt;            /* character checked for validity */
  13. char    *optarg;        /* argument associated with option */
  14.  
  15. int
  16. getopt(nargc, nargv, ostr)
  17. int nargc;
  18. char **nargv, *ostr;
  19. {
  20.     extern char    *index();
  21.     register char    *oli;        /* option letter list index */
  22.     static char    *place = EMSG;    /* option letter processing */
  23.  
  24.     if(!*place) {            /* update scanning pointer */
  25.         if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF);
  26.         if (*place == '-') {    /* found "--" */
  27.             ++optind;
  28.             return EOF;
  29.         }
  30.     }                /* option letter okay? */
  31.     if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) {
  32.         if(!*place) ++optind;
  33.         tell(": illegal option -- ");
  34.     }
  35.     if (*++oli != ':') {        /* don't need argument */
  36.         optarg = NULL;
  37.         if (!*place)
  38.             ++optind;
  39.     } else {                /* need an argument */
  40.         if (*place) {            /* no white space */
  41.             optarg = place;
  42.         } else if (nargc <= ++optind) {    /* no arg */
  43.             place = EMSG;
  44.             tell(": option requires an argument -- ");
  45.         } else {
  46.             optarg = nargv[optind];    /* white space */
  47.         }
  48.         place = EMSG;
  49.         ++optind;
  50.     }
  51.     return optopt;            /* dump back option letter */
  52. }
  53.  
  54. int
  55. getarg(nargc, nargv)
  56. int nargc;
  57. char **nargv;
  58. {
  59.     if (nargc <= optind) {
  60.         optarg = (char *) 0;
  61.         return EOF;
  62.     } else {
  63.         optarg = nargv[optind++];
  64.         return 0;
  65.     }
  66. }
  67.