home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / GETOPT.LIB < prev    next >
Text File  |  1995-09-28  |  2KB  |  74 lines

  1. // Getopt.lib - Cmm routines for using Unix like getopt
  2. // SYNTAX: int getopt(argc, argv, opt_string)
  3. // WHERE: opt_string is "abc:d")
  4. // RETURN: Returns an option (even an unlisted one) and global optarg
  5. //       a unlisted option or 1 with a ':' and no argument
  6.  
  7. _gopt_argc = 1;        // local static agrvc
  8. _gopt_index = 1;    // local static argv[argc][INDEX]
  9. optarg = "";
  10. optind = 0;
  11. opterr = 0;
  12.  
  13. getopt(ac,av,opts)
  14. {
  15. optarg = '\0';
  16. cur_opt = EOF
  17. opterr = 0;
  18. opt_found = 0;
  19.  
  20.  
  21.     if(_gopt_argc == ac) // if no more opts
  22.         return cur_opt;
  23.  
  24.     if(av[_gopt_argc][0] != '-') // if string starts without a -
  25.         return cur_opt;
  26.  
  27.     for(i = 0 ; i < strlen(opts) ; i++) {
  28.         if(av[_gopt_argc][_gopt_index] == opts[i]) {
  29.             opt_found = 1;
  30.             cur_opt = opts[i];
  31.             _gopt_index++;
  32.             optind++;
  33.             if(opts[i + 1] == ':') {
  34.                 if(strlen(av[_gopt_argc]) == _gopt_index) {
  35.                     optarg = av[++_gopt_argc];
  36.                 }
  37.                 else {    // build optarg (&av[x][y])
  38.                     k = 0;
  39.                     for(j = _gopt_index ; j < strlen(av[_gopt_argc]) ; j++)
  40.                         string[k++] = av[_gopt_argc][j];
  41.  
  42.                     string[k] = '\0';
  43.                     optarg = string;
  44.                 }
  45.                 if(strlen(optarg) < 1)
  46.                     opterr = -1;
  47.                 _gopt_argc++;
  48.                 _gopt_index = 1;
  49.                 return cur_opt;
  50.             }
  51.             if((_gopt_index) >= strlen(av[_gopt_argc])) {
  52.                 _gopt_argc++;
  53.                 _gopt_index = 1;
  54.             }
  55.             return cur_opt;
  56.         } /* end if opt[i] == av[] */
  57.  
  58.  
  59.     } /* end for */
  60.     if(opt_found == 0) {
  61.             cur_opt = av[_gopt_argc][_gopt_index];
  62.             _gopt_index++;
  63.             if((_gopt_index) >= strlen(av[_gopt_argc])) {
  64.                 _gopt_argc++;
  65.                 _gopt_index = 1;
  66.             }
  67.  
  68.             opterr = -1;
  69.     } /* end if an unlisted option */
  70.     return cur_opt;
  71. } /* end getopt() */
  72. /* ********************** end getopt.lib ******************************** */
  73.  
  74.