home *** CD-ROM | disk | FTP | other *** search
- // Getopt.lib - Cmm routines for using Unix like getopt
- // SYNTAX: int getopt(argc, argv, opt_string)
- // WHERE: opt_string is "abc:d")
- // RETURN: Returns an option (even an unlisted one) and global optarg
- // a unlisted option or 1 with a ':' and no argument
-
- _gopt_argc = 1; // local static agrvc
- _gopt_index = 1; // local static argv[argc][INDEX]
- optarg = "";
- optind = 0;
- opterr = 0;
-
- getopt(ac,av,opts)
- {
- optarg = '\0';
- cur_opt = EOF
- opterr = 0;
- opt_found = 0;
-
-
- if(_gopt_argc == ac) // if no more opts
- return cur_opt;
-
- if(av[_gopt_argc][0] != '-') // if string starts without a -
- return cur_opt;
-
- for(i = 0 ; i < strlen(opts) ; i++) {
- if(av[_gopt_argc][_gopt_index] == opts[i]) {
- opt_found = 1;
- cur_opt = opts[i];
- _gopt_index++;
- optind++;
- if(opts[i + 1] == ':') {
- if(strlen(av[_gopt_argc]) == _gopt_index) {
- optarg = av[++_gopt_argc];
- }
- else { // build optarg (&av[x][y])
- k = 0;
- for(j = _gopt_index ; j < strlen(av[_gopt_argc]) ; j++)
- string[k++] = av[_gopt_argc][j];
-
- string[k] = '\0';
- optarg = string;
- }
- if(strlen(optarg) < 1)
- opterr = -1;
- _gopt_argc++;
- _gopt_index = 1;
- return cur_opt;
- }
- if((_gopt_index) >= strlen(av[_gopt_argc])) {
- _gopt_argc++;
- _gopt_index = 1;
- }
- return cur_opt;
- } /* end if opt[i] == av[] */
-
-
- } /* end for */
- if(opt_found == 0) {
- cur_opt = av[_gopt_argc][_gopt_index];
- _gopt_index++;
- if((_gopt_index) >= strlen(av[_gopt_argc])) {
- _gopt_argc++;
- _gopt_index = 1;
- }
-
- opterr = -1;
- } /* end if an unlisted option */
- return cur_opt;
- } /* end getopt() */
- /* ********************** end getopt.lib ******************************** */
-
-