home *** CD-ROM | disk | FTP | other *** search
- /*
- ** This getopt behaves pretty much like you would expect.
- ** It does handle arguments like '-ab-' a little differently
- ** then normal; I think the -- 'stop option processing' should
- ** be treated like just another option, so that's what mine does.
- ** Other getopts seem to ignore the second '-' in '-ab-'.
- **
- ** I hereby place this version of getopt in
- ** the public domain. Do with this what you will.
- ** I'm sure there is a nicer and faster version out there
- ** somewhere but I don't care!
- **
- ** Robert Osborne, May 1991.
- */
- #include <stdio.h>
-
- int optind = 1;
- int opterr = 1;
- char *optarg = (char *) 0;
-
- static char *next_arg = (char *) 0;
-
- #define NO_OPT 0
- #define OPT_PLAIN 1
- #define OPT_ARG 2
-
-
- int
- getopt(argc, argv, optstring)
- int argc;
- char *argv[];
- char *optstring;
- {
- int ret;
- int which = NO_OPT;
-
- if( next_arg == (char *)0 )
- {
- if( argv[optind] == (char *) 0 || argv[optind][0] != '-' )
- return -1;
- next_arg = &argv[optind][1];
- }
- if( *next_arg == '\0' || *next_arg == '-' )
- {
- optind++;
- return -1;
- }
-
- while(*optstring)
- if( *next_arg == *optstring++ )
- which = (*optstring == ':') ? OPT_ARG : OPT_PLAIN;
-
- switch( which )
- {
- case NO_OPT:
- case OPT_PLAIN:
- ret = *next_arg++;
- if( *next_arg == '\0' )
- {
- optind++;
- next_arg = (char *)0;
- }
- if( which == OPT_PLAIN )
- return ret;
- if( opterr )
- fprintf(stderr, "%s: illegal option -- %c\n", argv[0], ret);
- return '?';
- case OPT_ARG:
- ret = *next_arg++;
- optind++;
- if( *next_arg != '\0' )
- {
- optarg = next_arg;
- next_arg = (char *)0;
- return ret;
- }
- else if( argv[optind] != (char *) 0 )
- {
- optarg = argv[optind];
- optind++;
- next_arg = (char *) 0;
- return ret;
- }
- else
- {
- next_arg = (char *) 0;
- if( opterr )
- fprintf(stderr, "%s: option requires an option -- %c\n",
- argv[0], ret);
- return '?';
- }
- }
- }
-