Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / apps / JAWS / clients / WebSTONE / src / getopt.c
blob7d8d78e27c698ee34eb0040ae0e81f558b66575f
1 /* this is a public domain version of getopt */
2 /* FTP Site: ftp.uu.net/pub/OS/unix/bsd-sources/lib/librpc/etc/getopt.c */
4 #include <stdio.h>
5 #include <string.h>
7 #define MYNULL 0
8 #define ERR(s, c) if(opterr){\
9 extern size_t strlen();\
10 extern int write();\
11 char errbuf[2];\
12 errbuf[0] = c; errbuf[1] = '\n';\
13 (void) write(2, argv[0], strlen(argv[0]));\
14 (void) write(2, s, strlen(s));\
15 (void) write(2, errbuf, 2);}
17 int opterr = 1;
18 int optind = 1;
19 int optopt;
20 char *optarg;
22 int
23 getopt(argc, argv, opts)
24 int argc;
25 char **argv, *opts;
27 static int sp = 1;
28 register int c;
29 register char *cp;
31 if(sp == 1)
32 if(optind >= argc ||
33 argv[optind][0] != '-' || argv[optind][1] == '\0')
34 return(EOF);
35 else if(strcmp(argv[optind], "--") == MYNULL) {
36 optind++;
37 return(EOF);
39 optopt = c = argv[optind][sp];
40 if(c == ':' || (cp=strchr(opts, c)) == 0) {
41 ERR(": unknown option, -", c);
42 if(argv[optind][++sp] == '\0') {
43 optind++;
44 sp = 1;
46 return('?');
48 if(*++cp == ':') {
49 if(argv[optind][sp+1] != '\0')
50 optarg = &argv[optind++][sp+1];
51 else if(++optind >= argc) {
52 ERR(": argument missing for -", c);
53 sp = 1;
54 return('?');
55 } else
56 optarg = argv[optind++];
57 sp = 1;
58 } else {
59 if(argv[optind][++sp] == '\0') {
60 sp = 1;
61 optind++;
63 optarg = 0;
65 return(c);