home *** CD-ROM | disk | FTP | other *** search
- /* getopt.c: */
-
- #include <stdio.h>
- #include <string.h>
-
- char *optarg;
- int optind = 0;
-
- int getopt (int argc, char *argv[], char *p_format)
- {
- static char *cp = 0;
- char c, *pos;
-
- while (1) {
- if (cp && *cp) {
- if (*cp != ':' && (pos = strchr (p_format, *cp))) {
- if (pos[1] != ':')
- return *cp++;
- if (cp[1])
- optarg = cp+1;
- else
- optarg = argv[++optind];
- if (optind == argc) {
- fprintf (stderr, "missing argument for option '-%c'", *cp);
- return -1;
- }
- c = *cp;
- cp = 0;
- return c;
- }
- fprintf (stderr, "-- unknown option character '%c'\n", *cp++);
- return '?';
- }
- if (++optind == argc)
- return -1;
- cp = argv[optind];
- if (cp[0] == '-') {
- if (cp[1] == '-' && cp[2] == 0) {
- optind++;
- return -1;
- }
- cp++;
- } else
- return -1;
- }
- }
-