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