home *** CD-ROM | disk | FTP | other *** search
/ FreeWare Collection 2 / FreeSoftwareCollection2pd199x-jp.img / fmge / getopt.c < prev    next >
Text File  |  1990-06-14  |  1KB  |  66 lines

  1. /*
  2.     GETOPT
  3. */
  4.  
  5. #include <stdio.h>
  6.  
  7. #define ARGCH (int)':'
  8. #define BADCH (int)'?'
  9. #define EMSG ""
  10. #define ENDARGS "--"
  11.  
  12. static char *index( s, c ) 
  13. register char *s ;
  14. register int c ;
  15. {
  16.     while(*s)
  17.         if(c == *s) return(s) ;
  18.         else s++ ;
  19.     return(NULL ) ;
  20. }
  21.  
  22. int opterr = 1 ;
  23. int optind = 1 ;
  24. int optopt ;
  25. char *optarg ;
  26.  
  27. #define tell(s) fputs(*nargv,stderr);fputs(s,stderr);fputc(optopt,stderr);fputc('\n',stderr);return(BADCH) ;
  28.  
  29. getopt(nargc,nargv,ostr)
  30. int nargc ;
  31. char **nargv, *ostr ;
  32. {
  33.     static char *place = EMSG ;
  34.     char *oli ;
  35.  
  36.     if(!*place) {
  37.         if(optind >= nargc || 
  38.             *(place = nargv[optind]) != '-' ||
  39.             !*++place ) return(EOF); 
  40.         if(*place == '-') {
  41.             ++optind ;
  42.             return(EOF) ;
  43.         }
  44.     }
  45.     if((optopt =(int)*place++) == ARGCH || !(oli = index(ostr,optopt))) {
  46.         if(!*place) ++optind ;
  47.         tell(": illegal option -- ") ;
  48.     }
  49.     if(*++oli != ARGCH) {
  50.         optarg = NULL ;
  51.         if( !*place) ++optind ;
  52.     }else {
  53.         if( *place) optarg = place ;
  54.         else if ( nargc <= ++optind ) {
  55.             place = EMSG ;
  56.             tell( ":option requires an argument -- " ) ;
  57.         }
  58.         else optarg = nargv[optind] ;
  59.         place = EMSG ;
  60.         ++optind ;
  61.     }
  62.     return( optopt) ;
  63. }
  64.  
  65.  
  66.