Skip to content

Commit 99d631e

Browse files
author
nono303
committed
Fix MSVC Windows Build
1 parent e02b4a1 commit 99d631e

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

modsec-sdbm-util.c

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <ctype.h>
2121
#include <stdio.h>
2222
#include <stdlib.h>
23-
#include <unistd.h>
23+
2424
#include <apr.h>
2525
#include <apr_errno.h>
2626
#include <apr_general.h>
@@ -32,11 +32,86 @@
3232
#define VERSION "v1.0"
3333

3434
#ifndef WIN32
35+
#include <unistd.h>
3536
# define v(fmt, ARGS...) do { if (verbose) printf("%s:%d:%s(): " fmt, __FILE__, \
3637
__LINE__, __func__, ## ARGS); } while (0)
3738
# define p(fmt, ARGS...) do { printf(fmt, ## ARGS); } while (0)
3839
#endif
3940
#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
40115
#define p printf
41116
#define v if (verbose) printf
42117
#endif

0 commit comments

Comments
 (0)