]> rtime.felk.cvut.cz Git - orte.git/blob - orte/examples/spy/msvc/getopt.c
improved JAVA interface from Lukas, update makefiles from msvc from Jan
[orte.git] / orte / examples / spy / msvc / getopt.c
1 #include <assert.h>
2 #include <errno.h>
3 #include <stdio.h>
4 #include <string.h>
5
6 #define __P(x) x
7 #define _DIAGASSERT(x) assert(x)
8
9 #ifdef __weak_alias
10 __weak_alias(getopt,_getopt);
11 #endif
12
13
14 int     opterr = 1,             /* if error message should be printed */
15         optind = 1,             /* index into parent argv vector */
16         optopt,                 /* character checked for validity */
17         optreset;               /* reset getopt */
18 char    *optarg;                /* argument associated with option */
19
20 static char * _progname __P((char *));
21 int getopt_internal __P((int, char * const *, const char *));
22
23 static char *
24 _progname(nargv0)
25         char * nargv0;
26 {
27         char * tmp;
28
29         _DIAGASSERT(nargv0 != NULL);
30
31         tmp = strrchr(nargv0, '/');
32         if (tmp)
33                 tmp++;
34         else
35                 tmp = nargv0;
36         return(tmp);
37 }
38
39 #define BADCH   (int)'?'
40 #define BADARG  (int)':'
41 #define EMSG    ""
42
43 /*
44  * getopt --
45  *      Parse argc/argv argument vector.
46  */
47 int
48 getopt(nargc, nargv, ostr)
49         int nargc;
50         char * const nargv[];
51         const char *ostr;
52 {
53         static char *__progname = 0;
54         static char *place = EMSG;              /* option letter processing */
55         char *oli;                              /* option letter list index */
56         __progname = __progname?__progname:_progname(*nargv);
57
58         _DIAGASSERT(nargv != NULL);
59         _DIAGASSERT(ostr != NULL);
60
61         if (optreset || !*place) {              /* update scanning pointer */
62                 optreset = 0;
63                 if (optind >= nargc || *(place = nargv[optind]) != '-') {
64                         place = EMSG;
65                         return (-1);
66                 }
67                 if (place[1] && *++place == '-' /* found "--" */
68                     && place[1] == '\0') {
69                         ++optind;
70                         place = EMSG;
71                         return (-1);
72                 }
73         }                                       /* option letter okay? */
74         if ((optopt = (int)*place++) == (int)':' ||
75             !(oli = strchr(ostr, optopt))) {
76                 /*
77                  * if the user didn't specify '-' as an option,
78                  * assume it means -1.
79                  */
80                 if (optopt == (int)'-')
81                         return (-1);
82                 if (!*place)
83                         ++optind;
84                 if (opterr && *ostr != ':')
85                         (void)fprintf(stderr,
86                             "%s: illegal option -- %c\n", __progname, optopt);
87                 return (BADCH);
88         }
89         if (*++oli != ':') {                    /* don't need argument */
90                 optarg = NULL;
91                 if (!*place)
92                         ++optind;
93         }
94         else {                                  /* need an argument */
95                 if (*place)                     /* no white space */
96                         optarg = place;
97                 else if (nargc <= ++optind) {   /* no arg */
98                         place = EMSG;
99                         if (*ostr == ':')
100                                 return (BADARG);
101                         if (opterr)
102                                 (void)fprintf(stderr,
103                                     "%s: option requires an argument -- %c\n",
104                                     __progname, optopt);
105                         return (BADCH);
106                 }
107                 else                            /* white space */
108                         optarg = nargv[optind];
109                 place = EMSG;
110                 ++optind;
111         }
112         return (optopt);                        /* dump back option letter */
113 }
114