|
20 | 20 | #include <ctype.h> |
21 | 21 | #include <stdio.h> |
22 | 22 | #include <stdlib.h> |
23 | | -#include <unistd.h> |
| 23 | + |
24 | 24 | #include <apr.h> |
25 | 25 | #include <apr_errno.h> |
26 | 26 | #include <apr_general.h> |
|
32 | 32 | #define VERSION "v1.0" |
33 | 33 |
|
34 | 34 | #ifndef WIN32 |
| 35 | +#include <unistd.h> |
35 | 36 | # define v(fmt, ARGS...) do { if (verbose) printf("%s:%d:%s(): " fmt, __FILE__, \ |
36 | 37 | __LINE__, __func__, ## ARGS); } while (0) |
37 | 38 | # define p(fmt, ARGS...) do { printf(fmt, ## ARGS); } while (0) |
38 | 39 | #endif |
39 | 40 | #ifdef WIN32 |
| 41 | +#include <windows.h> |
| 42 | +#include <string.h> |
| 43 | + |
| 44 | +int opterr = 1, /* if error message should be printed */ |
| 45 | + optind = 1, /* index into parent argv vector */ |
| 46 | + optopt, /* character checked for validity */ |
| 47 | + optreset; /* reset getopt */ |
| 48 | +char *optarg; /* argument associated with option */ |
| 49 | + |
| 50 | +#define BADCH (int)'?' |
| 51 | +#define BADARG (int)':' |
| 52 | +#define EMSG "" |
| 53 | + |
| 54 | +/* |
| 55 | +* getopt -- |
| 56 | +* Parse argc/argv argument vector. |
| 57 | +*/ |
| 58 | +int |
| 59 | + getopt(int nargc, char * const nargv[], const char *ostr) |
| 60 | +{ |
| 61 | + static char *place = EMSG; /* option letter processing */ |
| 62 | + const char *oli; /* option letter list index */ |
| 63 | + |
| 64 | + if (optreset || !*place) { /* update scanning pointer */ |
| 65 | + optreset = 0; |
| 66 | + if (optind >= nargc || *(place = nargv[optind]) != '-') { |
| 67 | + place = EMSG; |
| 68 | + return (-1); |
| 69 | + } |
| 70 | + if (place[1] && *++place == '-') { /* found "--" */ |
| 71 | + ++optind; |
| 72 | + place = EMSG; |
| 73 | + return (-1); |
| 74 | + } |
| 75 | + } /* option letter okay? */ |
| 76 | + if ((optopt = (int)*place++) == (int)':' || |
| 77 | + !(oli = strchr(ostr, optopt))) { |
| 78 | + /* |
| 79 | + * if the user didn't specify '-' as an option, |
| 80 | + * assume it means -1. |
| 81 | + */ |
| 82 | + if (optopt == (int)'-') |
| 83 | + return (-1); |
| 84 | + if (!*place) |
| 85 | + ++optind; |
| 86 | + if (opterr && *ostr != ':') |
| 87 | + (void)printf("illegal option -- %c\n", optopt); |
| 88 | + return (BADCH); |
| 89 | + } |
| 90 | + if (*++oli != ':') { /* don't need argument */ |
| 91 | + optarg = NULL; |
| 92 | + if (!*place) |
| 93 | + ++optind; |
| 94 | + } |
| 95 | + else { /* need an argument */ |
| 96 | + if (*place) /* no white space */ |
| 97 | + optarg = place; |
| 98 | + else if (nargc <= ++optind) { /* no arg */ |
| 99 | + place = EMSG; |
| 100 | + if (*ostr == ':') |
| 101 | + return (BADARG); |
| 102 | + if (opterr) |
| 103 | + (void)printf("option requires an argument -- %c\n", optopt); |
| 104 | + return (BADCH); |
| 105 | + } |
| 106 | + else /* white space */ |
| 107 | + optarg = nargv[optind]; |
| 108 | + place = EMSG; |
| 109 | + ++optind; |
| 110 | + } |
| 111 | + return (optopt); /* dump back option letter */ |
| 112 | +} |
| 113 | + |
| 114 | +#define strndup strdup |
40 | 115 | #define p printf |
41 | 116 | #define v if (verbose) printf |
42 | 117 | #endif |
|
0 commit comments