1 /* 2 * This is an unpublished work copyright (c) 1998 HELIOS Software GmbH 3 * 30827 Garbsen, Germany 4 */ 5 6 7 #include <stdio.h> 8 #include <string.h> 9 #ifdef HAS_UNISTD 10 # include <unistd.h> 11 #endif 12 13 char *optarg; 14 int optind = 1; 15 int opterr = 1; 16 int optopt; 17 static int subopt; 18 static int suboptind = 1; 19 20 int getopt(int argc, char *const argv[], const char * optstring) 21 { 22 char *curopt; 23 char *p; 24 int cursubopt; 25 26 if (suboptind == optind-1 && argv[suboptind][subopt] != '\0') { 27 curopt = (char *)argv[suboptind]; 28 } else { 29 curopt = (char *)argv[optind]; 30 if (curopt == NULL || curopt[0] != '-' || strcmp(curopt, "-") == 0) 31 return -1; 32 suboptind = optind; 33 subopt = 1; 34 optind++; 35 if (strcmp(curopt, "--") == 0) 36 return -1; 37 } 38 cursubopt = subopt++; 39 if ((p = strchr(optstring, curopt[cursubopt])) == NULL) { 40 optopt = curopt[cursubopt]; 41 if (opterr) 42 fprintf(stderr, "%s: illegal option -- %c\n", argv[0], optopt); 43 return '?'; 44 } 45 if (p[1] == ':') { 46 if (curopt[cursubopt+1] != '\0') { 47 optarg = curopt+cursubopt+1; 48 suboptind++; 49 return p[0]; 50 } 51 if (argv[optind] == NULL) { 52 optopt = p[0]; 53 if (opterr) 54 fprintf(stderr, "%s: option requires an argument -- %c\n", argv[0], optopt); 55 if (*optstring == ':') 56 return ':'; 57 return '?'; 58 } 59 optarg = argv[optind++]; 60 } 61 return p[0]; 62 } 63