From: Nishi Date: Wed, 2 Oct 2024 17:44:55 +0000 (+0000) Subject: compiles on vc6 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=7c20a722b109ff9dc566e7830fd10c4ba715027c;p=tewi.git compiles on vc6 git-svn-id: file:///raid/svn-personal/tewi/trunk@212 8739d7e6-ffea-ec47-b151-bdff447c6205 --- diff --git a/Common/Makefile b/Common/Makefile index 21ee0a9..520e1fe 100644 --- a/Common/Makefile +++ b/Common/Makefile @@ -1,19 +1,22 @@ # $Id$ +OBJ=o +STATIC=a +AR_FLAGS=rcs include $(PWD)/Platform/$(PLATFORM).mk .PHONY: all clean -.SUFFIXES: .c .o +.SUFFIXES: .c .$(OBJ) -OBJS = string.o log.o dir.o +OBJS = string.$(OBJ) log.$(OBJ) dir.$(OBJ) -all: common.a +all: common.$(STATIC) -common.a: $(OBJS) - $(AR) rcs $@ $(OBJS) +common.$(STATIC): $(OBJS) + $(AR) $(AR_FLAGS)$@ $(OBJS) -.c.o: +.c.$(OBJ): $(CC) $(CFLAGS) -c -o $@ $< clean: - rm -f *.o *.a + rm -f *.o *.a *.lib diff --git a/Common/dir.c b/Common/dir.c index 99b37e6..7b37f17 100644 --- a/Common/dir.c +++ b/Common/dir.c @@ -5,7 +5,9 @@ #include "cm_string.h" #include +#ifndef _MSC_VER #include +#endif #include #include @@ -16,11 +18,14 @@ int cm_sort(const void* _a, const void* _b) { } char** cm_scandir(const char* path) { +#ifdef _MSC_VER + return NULL; +#else DIR* dir = opendir(path); if(dir != NULL) { char** r = malloc(sizeof(*r)); - r[0] = NULL; struct dirent* d; + r[0] = NULL; while((d = readdir(dir)) != NULL) { if(strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) { struct stat s; @@ -60,4 +65,5 @@ char** cm_scandir(const char* path) { } else { return NULL; } +#endif } diff --git a/Common/log.c b/Common/log.c index 5639350..a7b7963 100644 --- a/Common/log.c +++ b/Common/log.c @@ -41,21 +41,22 @@ void cm_force_log(const char* log) { } void cm_log(const char* name, const char* log, ...) { - if(!cm_do_log) return; va_list args; - va_start(args, log); char namebuf[LOGNAME_LENGTH + 1]; + int i; + char* result; + char cbuf[2]; + if(!cm_do_log) return; + va_start(args, log); memset(namebuf, '.', LOGNAME_LENGTH); namebuf[LOGNAME_LENGTH] = 0; - int i; for(i = 0; name[i] != 0 && i < LOGNAME_LENGTH; i++) { namebuf[i] = name[i]; } - char* result = malloc(1); + result = malloc(1); result[0] = 0; - char cbuf[2]; cbuf[1] = 0; for(i = 0; log[i] != 0; i++) { @@ -69,14 +70,14 @@ void cm_log(const char* name, const char* log, ...) { } else if(log[i] == 'd') { int a = va_arg(args, int); char buf[128]; - sprintf(buf, "%d", a); char* tmp = result; + sprintf(buf, "%d", a); result = cm_strcat(tmp, buf); free(tmp); } } else { - cbuf[0] = log[i]; char* tmp = result; + cbuf[0] = log[i]; result = cm_strcat(tmp, cbuf); free(tmp); } diff --git a/Common/string.c b/Common/string.c index ebb7805..f138160 100644 --- a/Common/string.c +++ b/Common/string.c @@ -7,9 +7,10 @@ #include char* cm_strcat(const char* a, const char* b) { + char* str; if(a == NULL) a = ""; if(b == NULL) b = ""; - char* str = malloc(strlen(a) + strlen(b) + 1); + str = malloc(strlen(a) + strlen(b) + 1); memcpy(str, a, strlen(a)); memcpy(str + strlen(a), b, strlen(b)); str[strlen(a) + strlen(b)] = 0; @@ -26,8 +27,8 @@ char* cm_strcat3(const char* a, const char* b, const char* c) { char* cm_strdup(const char* str) { return cm_strcat(str, ""); } bool cm_endswith(const char* str, const char* end) { - if(strlen(str) < strlen(end)) return false; int i; + if(strlen(str) < strlen(end)) return false; for(i = strlen(str) - strlen(end); i < strlen(str); i++) { if(str[i] != end[i - strlen(str) + strlen(end)]) return false; } @@ -35,8 +36,8 @@ bool cm_endswith(const char* str, const char* end) { } bool cm_nocase_endswith(const char* str, const char* end) { - if(strlen(str) < strlen(end)) return false; int i; + if(strlen(str) < strlen(end)) return false; for(i = strlen(str) - strlen(end); i < strlen(str); i++) { if(tolower(str[i]) != tolower(end[i - strlen(str) + strlen(end)])) return false; } @@ -75,13 +76,13 @@ char* cm_trim(const char* str) { char** cm_split(const char* str, const char* by) { int i; char** r = malloc(sizeof(*r)); - r[0] = NULL; char* b = malloc(1); - b[0] = 0; char cbuf[2]; - cbuf[1] = 0; bool dq = false; bool sq = false; + r[0] = NULL; + b[0] = 0; + cbuf[1] = 0; for(i = 0;; i++) { int j; bool has = false; @@ -112,8 +113,8 @@ char** cm_split(const char* str, const char* by) { } else if(str[i] == '\'' && !dq) { sq = !sq; } else { - cbuf[0] = str[i]; char* tmp = b; + cbuf[0] = str[i]; b = cm_strcat(tmp, cbuf); free(tmp); } @@ -124,10 +125,10 @@ char** cm_split(const char* str, const char* by) { } bool cm_strcaseequ(const char* a, const char* b) { + int i; if(a == NULL) return false; if(b == NULL) return false; if(strlen(a) != strlen(b)) return false; - int i; for(i = 0; a[i] != 0; i++) { if(tolower(a[i]) != tolower(b[i])) return false; } @@ -154,8 +155,8 @@ int cm_hex(const char* str, int len) { char* cm_html_escape(const char* str) { int i; char* result = malloc(1); - result[0] = 0; char cbuf[2]; + result[0] = 0; cbuf[1] = 0; for(i = 0; str[i] != 0; i++) { cbuf[0] = str[i]; @@ -183,15 +184,15 @@ char* cm_html_escape(const char* str) { char* cm_url_escape(const char* str) { int i; char* result = malloc(1); - result[0] = 0; char cbuf[2]; + result[0] = 0; cbuf[1] = 0; for(i = 0; str[i] != 0; i++) { cbuf[0] = str[i]; if('!' <= str[i] && str[i] <= '@' && str[i] != '.' && str[i] != '-' && str[i] != '/' && !('0' <= str[i] && str[i] <= '9')) { char code[4]; - sprintf(code, "%%%02X", str[i]); char* tmp = result; + sprintf(code, "%%%02X", str[i]); result = cm_strcat(tmp, code); free(tmp); } else { diff --git a/Module/Makefile b/Module/Makefile index 3c24b72..6848729 100644 --- a/Module/Makefile +++ b/Module/Makefile @@ -1,17 +1,19 @@ # $Id$ +OBJ=o +STATIC=a include $(PWD)/Platform/$(PLATFORM).mk .PHONY: all clean -.SUFFIXES: .c .o $(LIB) +.SUFFIXES: .c .$(OBJ) $(LIBSUF) -all: mod_cgi$(LIB) mod_proxy$(LIB) +all: mod_cgi$(LIBSUF) mod_proxy$(LIBSUF) -.o$(LIB): - $(CC) $(LDFLAGS) -shared -o $@ $< ../Common/common.a $(LIBS) +.$(OBJ)$(LIBSUF): + $(CC) $(LDFLAGS) -shared -o $@ $< ../Common/common.$(STATIC) $(LIBS) -.c.o: +.c.$(OBJ): $(CC) $(CFLAGS) -c -o $@ $< clean: - rm -f *.o *.so *.a *.dll + rm -f *.o *.so *.a *.dll *.dll diff --git a/Platform/cygwin.mk b/Platform/cygwin.mk index 5e45edb..ca6976f 100644 --- a/Platform/cygwin.mk +++ b/Platform/cygwin.mk @@ -6,4 +6,4 @@ CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common LDFLAGS = LIBS = EXEC = -LIB = .so +LIBSUF = .so diff --git a/Platform/generic.mk b/Platform/generic.mk index 2b73ed0..15c2462 100644 --- a/Platform/generic.mk +++ b/Platform/generic.mk @@ -6,4 +6,4 @@ CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -fPIC LDFLAGS = LIBS = EXEC = -LIB = .so +LIBSUF = .so diff --git a/Platform/haiku.mk b/Platform/haiku.mk index 2a72693..91d5a4f 100644 --- a/Platform/haiku.mk +++ b/Platform/haiku.mk @@ -6,4 +6,4 @@ CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -fPIC LDFLAGS = LIBS = -lnetwork EXEC = -LIB = .so +LIBSUF = .so diff --git a/Platform/linux.mk b/Platform/linux.mk index 41d8e41..45378fe 100644 --- a/Platform/linux.mk +++ b/Platform/linux.mk @@ -6,4 +6,4 @@ CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -D_DEFAULT_SOURCE - LDFLAGS = LIBS = EXEC = -LIB = .so +LIBSUF = .so diff --git a/Platform/netbsd.mk b/Platform/netbsd.mk index 2b73ed0..15c2462 100644 --- a/Platform/netbsd.mk +++ b/Platform/netbsd.mk @@ -6,4 +6,4 @@ CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -fPIC LDFLAGS = LIBS = EXEC = -LIB = .so +LIBSUF = .so diff --git a/Platform/ps2.mk b/Platform/ps2.mk index ba5843c..b0558c2 100644 --- a/Platform/ps2.mk +++ b/Platform/ps2.mk @@ -8,6 +8,6 @@ CFLAGS = -O2 -D_EE -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -I /usr/local/ps2dev/ LDFLAGS = -T/usr/local/ps2dev/ps2sdk/ee/startup/linkfile -L /usr/local/ps2dev/ps2sdk/ee/lib -O2 LIBS = -ldebug -lsocket EXEC = .elf -LIB = .so +LIBSUF = .so MODULE = SERVADD = mips64r5900el-ps2-elf-strip tewi.elf -o tewi_strip.elf diff --git a/Platform/ps3.mk b/Platform/ps3.mk index 55ebc8e..3b18d30 100644 --- a/Platform/ps3.mk +++ b/Platform/ps3.mk @@ -8,7 +8,7 @@ CFLAGS = -g -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -I /usr/local/ps3dev/ppu/inc LDFLAGS = -L /usr/local/ps3dev/ppu/lib -L /usr/local/ps3dev/portlibs/ppu/lib LIBS = -lnet -lsysmodule -lsysutil -lrt -llv2 -lrsx -lgcm_sys -lpng -lm -lz EXEC = .elf -LIB = .so +LIBSUF = .so MODULE = SERVADD = ppu-strip tewi.elf -o tewi_strip.elf TARGET = tewi.pkg diff --git a/Platform/psp.mk b/Platform/psp.mk index 9973bb5..cc6f33b 100644 --- a/Platform/psp.mk +++ b/Platform/psp.mk @@ -8,7 +8,7 @@ CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -I /usr/local/pspde LDFLAGS = -Wl,-zmax-page-size=128 -L /usr/local/pspdev/psp/sdk/lib LIBS = -lpspgum -lpspgu -lpspdebug -lpspdisplay -lpspge -lpspctrl -lpspnet -lpspnet_apctl -lcglue -lpspwlan EXEC = .elf -LIB = .so +LIBSUF = .so MODULE = SERVADD = psp-fixup-imports tewi.elf && psp-strip tewi.elf -o tewi_strip.elf TARGET = tewi.pbp diff --git a/Platform/vc6.mk b/Platform/vc6.mk new file mode 100644 index 0000000..f1cfa26 --- /dev/null +++ b/Platform/vc6.mk @@ -0,0 +1,12 @@ +# $Id$ + +CC = ../vc6.sh +AR = lib +AR_FLAGS = /out: +CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I ../Common -fPIC +LDFLAGS = +LIBS = -lws2_32 -ladvapi32 -llibcmt +EXEC = +STATIC = lib +LIBSUF = .dll +OBJ = obj diff --git a/Server/Makefile b/Server/Makefile index b726d4a..e62626e 100644 --- a/Server/Makefile +++ b/Server/Makefile @@ -1,18 +1,20 @@ # $Id$ +OBJ=o +STATIC=a include $(PWD)/Platform/$(PLATFORM).mk .PHONY: all clean -.SUFFIXES: .c .o +.SUFFIXES: .c .$(OBJ) -OBJS = main.o version.o config.o server.o http.o module.o strptime.o font.o $(EXTOBJS) $(PREOBJS) +OBJS = main.$(OBJ) version.$(OBJ) config.$(OBJ) server.$(OBJ) http.$(OBJ) module.$(OBJ) strptime.$(OBJ) font.$(OBJ) $(EXTOBJS) $(PREOBJS) all: tewi$(EXEC) $(TARGET) tewi_strip$(EXEC): tewi$(EXEC) -tewi$(EXEC): $(OBJS) ../Common/common.a - $(CC) $(LDFLAGS) $(EXTLDFLAGS) -o $@ $(OBJS) $(EXTLIBS) ../Common/common.a $(LIBS) +tewi$(EXEC): $(OBJS) ../Common/common.$(STATIC) + $(CC) $(LDFLAGS) $(EXTLDFLAGS) -o $@ $(OBJS) $(EXTLIBS) ../Common/common.$(STATIC) $(LIBS) $(SERVADD) tewi.pbp: tewi_strip$(EXEC) param.sfo @@ -40,11 +42,11 @@ tewi.pkg: tewi.self rm -rf pkg package_finalize $@ -.c.o: +.c.$(OBJ): $(CC) $(CFLAGS) $(EXTCFLAGS) -c -o $@ $< tewi.res: tewi.rc ../Binary/tewi.ico $(WINDRES) tewi.rc -O coff -o $@ clean: - rm -f *.o tewi *.exe *.res *.elf *.sfo *.pbp *.self *.pkg + rm -f *.o tewi *.exe *.res *.elf *.sfo *.pbp *.self *.pkg *.obj diff --git a/Server/config.c b/Server/config.c index 627c6a9..c48daf1 100644 --- a/Server/config.c +++ b/Server/config.c @@ -9,9 +9,12 @@ #include #include #include + +#ifndef _MSC_VER #include +#endif -#ifdef __MINGW32__ +#if defined(__MINGW32__) || defined(_MSC_VER) #include #endif @@ -37,11 +40,12 @@ bool tw_permission_allowed(const char* path, SOCKADDR addr, struct tw_http_reque bool perm = false; again: for(i = 0; i < vhost->dir_count; i++) { + char* noslash; struct tw_dir_entry* e = &vhost->dirs[i]; pathstart = false; if(strlen(path) >= strlen(e->dir)) { - pathstart = true; int j; + pathstart = true; for(j = 0; path[j] != 0 && e->dir[j] != 0; j++) { if(path[j] != e->dir[j]) { pathstart = false; @@ -49,7 +53,7 @@ again: } } } - char* noslash = cm_strdup(e->dir); + noslash = cm_strdup(e->dir); noslash[strlen(noslash) - 1] = 0; if(strcmp(e->dir, path) == 0 || strcmp(noslash, path) == 0 || pathstart) { found = true; @@ -119,25 +123,26 @@ void tw_config_init(void) { } int tw_config_read(const char* path) { - cm_log("Config", "Reading %s", path); char cbuf[2]; - cbuf[1] = 0; int ln = 0; int ifbr = 0; int ignore = -1; - FILE* f = fopen(path, "r"); + FILE* f; + cm_log("Config", "Reading %s", path); + f = fopen(path, "r"); + cbuf[1] = 0; if(f != NULL) { char* line = malloc(1); - line[0] = 0; int stop = 0; struct tw_config_entry* current = &config.root; char* vhost = NULL; char* dir = NULL; + line[0] = 0; while(stop == 0) { int c = fread(cbuf, 1, 1, f); if(cbuf[0] == '\n' || c <= 0) { - ln++; char* l = cm_trim(line); + ln++; if(strlen(l) > 0 && l[0] != '#') { char** r = cm_split(l, " \t"); int i; @@ -226,6 +231,7 @@ int tw_config_read(const char* path) { cm_log("Config", "Missing virtual host at line %d", ln); stop = 1; } else { + int i; vhost = cm_strdup(r[1]); current = &config.vhosts[config.vhost_count++]; current->dir_count = 0; @@ -234,7 +240,6 @@ int tw_config_read(const char* path) { current->index_count = 0; current->readme_count = 0; current->hideport = -1; - int i; current->name = cm_strdup(vhost); current->port = -1; for(i = 0; vhost[i] != 0; i++) { @@ -261,10 +266,18 @@ int tw_config_read(const char* path) { #endif ) { for(i = 1; r[i] != NULL; i++) { +#ifdef _MSC_VER + uint32_t port = atoi(r[i]); +#else uint64_t port = atoi(r[i]); - cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : ""); - if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 32); +#endif int j; + cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : ""); +#ifdef _MSC_VER + if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1UL << 31); +#else + if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 31); +#endif for(j = 0; config.ports[j] != -1; j++) ; config.ports[j] = port; @@ -318,8 +331,8 @@ int tw_config_read(const char* path) { if(r[1] == NULL) { cm_log("Config", "Missing condition type at line %d", ln); } else { - ifbr++; bool ign = false; + ifbr++; if(cm_strcaseequ(r[1], "False")) { ign = true; } else if(cm_strcaseequ(r[1], "True")) { @@ -428,12 +441,12 @@ int tw_config_read(const char* path) { stop = 1; if(r[0] != NULL) { int argc; - for(argc = 0; r[argc] != NULL; argc++) - ; - stop = 0; int i; bool called = false; struct tw_tool tools; + for(argc = 0; r[argc] != NULL; argc++) + ; + stop = 0; tw_init_tools(&tools); for(i = 0; i < config.module_count; i++) { tw_mod_config_t mod_config = (tw_mod_config_t)tw_module_symbol(config.modules[i], "mod_config"); diff --git a/Server/http.c b/Server/http.c index c6d83c0..0f5030f 100644 --- a/Server/http.c +++ b/Server/http.c @@ -15,7 +15,7 @@ #include #include -#ifdef __MINGW32__ +#if defined(__MINGW32__) || defined(_MSC_VER) #include #else #ifdef USE_POLL @@ -53,6 +53,8 @@ int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) { char buffer[512]; char cbuf[2]; int phase = 0; + char* header; + int nl; #ifdef USE_POLL struct pollfd pollfds[1]; @@ -73,15 +75,18 @@ int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) { req->body = NULL; req->version = NULL; - char* header = malloc(1); + header = malloc(1); header[0] = 0; - int nl = 0; + nl = 0; while(1) { + int i; + int len; + int n; #ifndef USE_POLL + struct timeval tv; FD_ZERO(&fds); FD_SET(sock, &fds); - struct timeval tv; tv.tv_sec = 5; tv.tv_usec = 0; #endif @@ -89,12 +94,12 @@ int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) { if(ssl == NULL || !SSL_has_pending(ssl)) { #endif #ifdef USE_POLL - int n = poll(pollfds, 1, 5000); + n = poll(pollfds, 1, 5000); #else #ifdef __HAIKU__ - int n = select(32, &fds, NULL, NULL, &tv); + n = select(32, &fds, NULL, NULL, &tv); #else - int n = select(FD_SETSIZE, &fds, NULL, NULL, &tv); + n = select(FD_SETSIZE, &fds, NULL, NULL, &tv); #endif #endif if(n <= 0) { @@ -106,12 +111,11 @@ int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) { #ifndef NO_SSL } #endif - int len = tw_read(ssl, sock, buffer, 512); + len = tw_read(ssl, sock, buffer, 512); if(len <= 0) { bad = true; break; } - int i; for(i = 0; i < len; i++) { char c = buffer[i]; if(phase == 0) { @@ -124,12 +128,13 @@ int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) { phase++; } } else { + char* tmp; if(req->method == NULL) { req->method = malloc(1); req->method[0] = 0; } cbuf[0] = c; - char* tmp = req->method; + tmp = req->method; req->method = cm_strcat(tmp, cbuf); free(tmp); } @@ -143,12 +148,13 @@ int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) { phase++; } } else { + char* tmp; if(req->path == NULL) { req->path = malloc(1); req->path[0] = 0; } cbuf[0] = c; - char* tmp = req->path; + tmp = req->path; req->path = cm_strcat(tmp, cbuf); free(tmp); } @@ -160,17 +166,19 @@ int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) { goto getout; } else { /* We have Method, Path, Version now */ - + int j; + char* p; + int incr; if(strcmp(req->version, "HTTP/1.1") != 0 && strcmp(req->version, "HTTP/1.0") != 0) { cm_log("HTTP", "Bad HTTP Version"); bad = true; goto getout; } - int j; - char* p = malloc(1); + p = malloc(1); p[0] = 0; for(j = 0; req->path[j] != 0; j++) { + char* tmp; if(req->path[j] == '/') { cbuf[0] = '/'; for(; req->path[j] != 0 && req->path[j] == '/'; j++) @@ -179,23 +187,24 @@ int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) { } else { cbuf[0] = req->path[j]; } - char* tmp = p; + tmp = p; p = cm_strcat(tmp, cbuf); free(tmp); } free(req->path); req->path = p; - int incr = 0; + incr = 0; p = malloc(1); p[0] = 0; for(j = 0;; j++) { if(req->path[j] == '/' || req->path[j] == 0) { char oldc = req->path[j]; + char* pth; cbuf[0] = oldc; req->path[j] = 0; - char* pth = req->path + incr; + pth = req->path + incr; if(strcmp(pth, "..") == 0) { int k; @@ -229,12 +238,13 @@ int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) { phase++; } } else if(c != '\r') { + char* tmp; if(req->version == NULL) { req->version = malloc(1); req->version[0] = 0; } cbuf[0] = c; - char* tmp = req->version; + tmp = req->version; req->version = cm_strcat(tmp, cbuf); free(tmp); } @@ -245,22 +255,25 @@ int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) { phase++; goto getout; } else { + int j; if(req->headers == NULL) { req->headers = malloc(sizeof(*req->headers)); req->headers[0] = NULL; } - int j; for(j = 0; header[j] != 0; j++) { if(header[j] == ':') { + char* kv; + char* vv; + char** old; + int k; header[j] = 0; j++; for(; header[j] != 0 && (header[j] == ' ' || header[j] == '\t'); j++) ; - char* kv = header; - char* vv = header + j; + kv = header; + vv = header + j; - char** old = req->headers; - int k; + old = req->headers; for(k = 0; old[k] != NULL; k++) ; req->headers = malloc(sizeof(*req->headers) * (k + 3)); @@ -280,9 +293,10 @@ int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) { header[0] = 0; } } else if(c != '\r') { + char* tmp; nl = 0; cbuf[0] = c; - char* tmp = header; + tmp = header; header = cm_strcat(tmp, cbuf); free(tmp); } @@ -295,73 +309,79 @@ getout: tw_free_request(req); return 1; } - char* result = malloc(1); - result[0] = 0; - int i; - for(i = 0; req->path[i] != 0; i++) { - if(req->path[i] == '?') { - req->path[i] = 0; - req->query = cm_strdup(req->path + i + 1); - break; + { + char* result = malloc(1); + int i; + int j; + int incr; + char* p; + result[0] = 0; + for(i = 0; req->path[i] != 0; i++) { + if(req->path[i] == '?') { + req->path[i] = 0; + req->query = cm_strdup(req->path + i + 1); + break; + } } - } - for(i = 0; req->path[i] != 0; i++) { - if(req->path[i] == '%') { - if(req->path[i + 1] == 0) continue; - cbuf[0] = cm_hex(req->path + i + 1, 2); - if(cbuf[0] != '\\') { - char* tmp = result; + for(i = 0; req->path[i] != 0; i++) { + if(req->path[i] == '%') { + if(req->path[i + 1] == 0) continue; + cbuf[0] = cm_hex(req->path + i + 1, 2); + if(cbuf[0] != '\\') { + char* tmp = result; + result = cm_strcat(tmp, cbuf); + free(tmp); + } + i += 2; + } else if(req->path[i] != '\\') { + char* tmp; + cbuf[0] = req->path[i]; + tmp = result; result = cm_strcat(tmp, cbuf); free(tmp); } - i += 2; - } else if(req->path[i] != '\\') { - cbuf[0] = req->path[i]; - char* tmp = result; - result = cm_strcat(tmp, cbuf); - free(tmp); } - } - free(req->path); - req->path = result; - - int incr = 0; - char* p = malloc(1); - p[0] = 0; - int j; - for(j = 0;; j++) { - if(req->path[j] == '/' || req->path[j] == 0) { - char oldc = req->path[j]; - cbuf[0] = oldc; - req->path[j] = 0; - - char* pth = req->path + incr; - - if(strcmp(pth, "..") == 0) { - int k; - if(p[strlen(p) - 1] == '/') p[strlen(p) - 1] = 0; - for(k = strlen(p) - 1; k >= 0; k--) { - if(p[k] == '/') { - p[k + 1] = 0; - break; + free(req->path); + req->path = result; + + incr = 0; + p = malloc(1); + p[0] = 0; + for(j = 0;; j++) { + if(req->path[j] == '/' || req->path[j] == 0) { + char oldc = req->path[j]; + char* pth; + cbuf[0] = oldc; + req->path[j] = 0; + + pth = req->path + incr; + + if(strcmp(pth, "..") == 0) { + int k; + if(p[strlen(p) - 1] == '/') p[strlen(p) - 1] = 0; + for(k = strlen(p) - 1; k >= 0; k--) { + if(p[k] == '/') { + p[k + 1] = 0; + break; + } } + if(strlen(p) == 0) { + free(p); + p = cm_strdup("/"); + } + } else if(strcmp(pth, ".") == 0) { + } else { + char* tmp = p; + p = cm_strcat3(tmp, pth, cbuf); + free(tmp); } - if(strlen(p) == 0) { - free(p); - p = cm_strdup("/"); - } - } else if(strcmp(pth, ".") == 0) { - } else { - char* tmp = p; - p = cm_strcat3(tmp, pth, cbuf); - free(tmp); + + incr = j + 1; + if(oldc == 0) break; } - - incr = j + 1; - if(oldc == 0) break; } + free(req->path); + req->path = p; + return 0; } - free(req->path); - req->path = p; - return 0; } diff --git a/Server/main.c b/Server/main.c index 68dab7c..9774361 100644 --- a/Server/main.c +++ b/Server/main.c @@ -4,7 +4,9 @@ #include "../config.h" +#ifndef _MSC_VER #include +#endif #include #include #include @@ -22,7 +24,7 @@ #include "tw_server.h" #include "tw_version.h" -#ifdef __MINGW32__ +#if defined(__MINGW32__) || defined(_MSC_VER) #include #endif @@ -59,6 +61,13 @@ PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER); #define printf(...) tt_printf(__VA_ARGS__) #define STDERR_LOG(...) tt_printf(__VA_ARGS__) +#elif defined(_MSC_VER) +void STDERR_LOG(const char* format, ...){ + va_list args; + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); +} #else #define STDERR_LOG(...) fprintf(stderr, __VA_ARGS__) #endif @@ -71,7 +80,7 @@ char tw_server[2048]; int startup(int argc, char** argv); -#ifdef __MINGW32__ +#if defined(__MINGW32__) || defined(_MSC_VER) char* get_registry(const char* main, const char* sub) { DWORD bufsize = 512; HKEY handle; @@ -510,6 +519,7 @@ void show_png(void) { #endif int main(int argc, char** argv) { + int st; logfile = stderr; #ifdef SERVICE SERVICE_TABLE_ENTRY table[] = {{"Tewi HTTPd", servmain}, {NULL, NULL}}; @@ -666,7 +676,7 @@ int main(int argc, char** argv) { scr_printf("PS2 Bootstrap, Tewi/%s\n", tw_get_version()); SleepThread(); #endif - int st = startup(argc, argv); + st = startup(argc, argv); if(st != -1) { #ifdef _PSP printf("Error code %d\n", st); @@ -691,7 +701,8 @@ int main(int argc, char** argv) { int startup(int argc, char** argv) { int i; -#ifdef __MINGW32__ + char* r; +#if defined(__MINGW32__) || defined(_MSC_VER) char* confpath = cm_strdup(PREFIX "/etc/tewi.conf"); char* regpath = get_registry("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Tewi HTTPd", "InstallDir"); if(regpath != NULL) { @@ -769,10 +780,10 @@ int startup(int argc, char** argv) { return 1; } sprintf(tw_server, "Tewi/%s (%s)%s", tw_get_version(), tw_get_platform(), config.extension == NULL ? "" : config.extension); - char* r = cm_strcat(tw_server, " running..."); + r = cm_strcat(tw_server, " running..."); cm_force_log(r); free(r); -#ifndef __MINGW32__ +#if !defined(__MINGW32__) && !defined(_MSC_VER) signal(SIGCHLD, SIG_IGN); signal(SIGPIPE, SIG_IGN); #else diff --git a/Server/module.c b/Server/module.c index f4292f1..9b7bed6 100644 --- a/Server/module.c +++ b/Server/module.c @@ -10,8 +10,10 @@ #include #include -#include #include +#ifndef _MSC_VER +#include +#endif extern struct tw_config config; @@ -24,7 +26,7 @@ int tw_module_init(void* mod) { return 1; } #else -#ifdef __MINGW32__ +#if defined(__MINGW32__) || defined(_MSC_VER) #include #else #include @@ -32,9 +34,9 @@ int tw_module_init(void* mod) { return 1; } void* tw_module_load(const char* path) { char* p = getcwd(NULL, 0); - chdir(config.server_root); void* lib; -#ifdef __MINGW32__ + chdir(config.server_root); +#if defined(__MINGW32__) || defined(_MSC_VER) lib = LoadLibraryA(path); #else lib = dlopen(path, RTLD_LAZY); @@ -48,7 +50,7 @@ void* tw_module_load(const char* path) { } void* tw_module_symbol(void* mod, const char* sym) { -#ifdef __MINGW32__ +#if defined(__MINGW32__) || defined(_MSC_VER) return GetProcAddress(mod, sym); #else return dlsym(mod, sym); diff --git a/Server/server.c b/Server/server.c index 865cf28..1c2ff94 100644 --- a/Server/server.c +++ b/Server/server.c @@ -15,22 +15,24 @@ #include "tw_module.h" #include "tw_version.h" +#ifndef _MSC_VER #include +#endif #include #include #include #include #include #include -#include #include +#include #include #include #include #include -#ifdef __MINGW32__ +#if defined(__MINGW32__) || defined(_MSC_VER) #ifndef NO_GETADDRINFO #include #include @@ -69,6 +71,10 @@ #include #endif +#ifndef S_ISDIR +#define S_ISDIR(x) ((x) & _S_IFDIR) +#endif + extern struct tw_config config; extern char tw_server[]; @@ -77,7 +83,7 @@ int sockcount = 0; SOCKADDR addresses[MAX_PORTS]; int sockets[MAX_PORTS]; -#ifdef __MINGW32__ +#if defined(__MINGW32__) || defined(_MSC_VER) const char* reserved_names[] = {"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"}; #endif @@ -104,7 +110,7 @@ int tw_wildcard_match(const char* wildcard, const char* target) { } void close_socket(int sock) { -#if defined(__MINGW32__) +#if defined(__MINGW32__) || defined(_MSC_VER) closesocket(sock); #else close(sock); @@ -113,7 +119,7 @@ void close_socket(int sock) { int tw_server_init(void) { int i; -#ifdef __MINGW32__ +#if defined(__MINGW32__) || defined(_MSC_VER) WSADATA wsa; WSAStartup(MAKEWORD(2, 0), &wsa); #endif @@ -121,12 +127,14 @@ int tw_server_init(void) { ; sockcount = i; for(i = 0; config.ports[i] != -1; i++) { + int yes = 1; + int no = 0; #ifdef NO_IPV6 int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); #else int sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); #endif -#ifdef __MINGW32__ +#if defined(__MINGW32__) || defined(_MSC_VER) if(sock == INVALID_SOCKET) #else if(sock < 0) @@ -135,7 +143,6 @@ int tw_server_init(void) { cm_log("Server", "Socket creation failure"); return 1; } - int yes = 1; if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0) { close_socket(sock); cm_log("Server", "setsockopt failure (reuseaddr)"); @@ -149,7 +156,6 @@ int tw_server_init(void) { } #endif #ifndef NO_IPV6 - int no = 0; if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&no, sizeof(no)) < 0) { close_socket(sock); cm_log("Server", "setsockopt failure (IPv6)"); @@ -222,6 +228,7 @@ size_t tw_write(SSL* ssl, int s, void* data, size_t len) { void _tw_process_page(SSL* ssl, int sock, const char* status, const char* type, FILE* f, const unsigned char* doc, size_t size, char** headers, time_t mtime, time_t cmtime) { char construct[512]; + size_t incr; if(mtime != 0 && cmtime != 0 && mtime <= cmtime) { status = "304 Not Modified"; type = NULL; @@ -230,7 +237,11 @@ void _tw_process_page(SSL* ssl, int sock, const char* status, const char* type, f = NULL; doc = NULL; } +#ifdef _MSC_VER + sprintf(construct, "%lu", (unsigned long)size); +#else sprintf(construct, "%llu", (unsigned long long)size); +#endif tw_write(ssl, sock, "HTTP/1.1 ", 9); tw_write(ssl, sock, (char*)status, strlen(status)); tw_write(ssl, sock, "\r\n", 2); @@ -255,8 +266,8 @@ void _tw_process_page(SSL* ssl, int sock, const char* status, const char* type, tw_write(ssl, sock, "\r\n", 2); } } - int i; if(headers != NULL) { + int i; for(i = 0; headers[i] != NULL; i += 2) { tw_write(ssl, sock, headers[i], strlen(headers[i])); tw_write(ssl, sock, ": ", 2); @@ -266,7 +277,7 @@ void _tw_process_page(SSL* ssl, int sock, const char* status, const char* type, } tw_write(ssl, sock, "\r\n", 2); if(doc == NULL && f == NULL) return; - size_t incr = 0; + incr = 0; while(1) { if(f != NULL) { char buffer[128]; @@ -307,6 +318,11 @@ const char* tw_http_status(int code) { char* tw_http_default_error(int code, char* name, int port, struct tw_config_entry* vhost) { char address[1024]; + char* st; + char* st2; + char* buffer; + char* str; + int i; if((vhost->hideport == -1 ? config.root.hideport : vhost->hideport) == 1) { sprintf(address, "
%s Server at %s
", tw_server, name, port); @@ -314,17 +330,15 @@ char* tw_http_default_error(int code, char* name, int port, struct tw_config_ent sprintf(address, "
%s Server at %s Port %d
", tw_server, name, port); } - char* st = cm_strdup(tw_http_status(code)); - char* st2; - int i; + st = cm_strdup(tw_http_status(code)); for(i = 0; st[i] != 0; i++) { if(st[i] == ' ') { st2 = cm_strdup(st + i + 1); break; } } - char* buffer = malloc(4096); - char* str = cm_strcat3(ERROR_HTML); + buffer = malloc(4096); + str = cm_strcat3(ERROR_HTML); sprintf(buffer, str, st, st2); free(str); free(st); @@ -340,8 +354,8 @@ void tw_http_error(SSL* ssl, int sock, int error, char* name, int port, struct t void addstring(char** str, const char* add, ...) { int i; char cbuf[2]; - cbuf[1] = 0; va_list va; + cbuf[1] = 0; va_start(va, add); for(i = 0; add[i] != 0; i++) { cbuf[0] = add[i]; @@ -366,8 +380,8 @@ void addstring(char** str, const char* add, ...) { } else if(add[i] == 'd') { int n = va_arg(va, int); char* h = malloc(512); - sprintf(h, "%d", n); char* tmp = *str; + sprintf(h, "%d", n); *str = cm_strcat(tmp, h); free(tmp); free(h); @@ -387,9 +401,9 @@ void addstring(char** str, const char* add, ...) { char* tw_get_mime(const char* ext, struct tw_config_entry* vhost_entry) { char* mime = "application/octet-stream"; - if(ext == NULL) return mime; bool set = false; int i; + if(ext == NULL) return mime; for(i = 0; i < vhost_entry->mime_count; i++) { if(strcmp(vhost_entry->mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(vhost_entry->mimes[i].ext, ext))) { mime = vhost_entry->mimes[i].mime; @@ -408,9 +422,9 @@ char* tw_get_mime(const char* ext, struct tw_config_entry* vhost_entry) { char* tw_get_icon(const char* mime, struct tw_config_entry* vhost_entry) { char* icon = ""; - if(mime == NULL) return ""; bool set = false; int i; + if(mime == NULL) return ""; for(i = 0; i < vhost_entry->icon_count; i++) { if(strcmp(vhost_entry->icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(vhost_entry->icons[i].mime, mime))) { icon = vhost_entry->icons[i].icon; @@ -434,27 +448,25 @@ struct pass_entry { SOCKADDR addr; }; -#ifdef __MINGW32__ +#if defined(__MINGW32__) || defined(_MSC_VER) unsigned int WINAPI tw_server_pass(void* ptr) { #elif defined(__HAIKU__) int32_t tw_server_pass(void* ptr) { #elif defined(_PSP) || defined(__PPU__) int tw_server_pass(void* ptr) { #endif -#if defined(__HAIKU__) || defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) +#if defined(__HAIKU__) || defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER) +#define FREE_PTR int sock = ((struct pass_entry*)ptr)->sock; bool ssl = ((struct pass_entry*)ptr)->ssl; int port = ((struct pass_entry*)ptr)->port; SOCKADDR addr = ((struct pass_entry*)ptr)->addr; - free(ptr); #else void tw_server_pass(int sock, bool ssl, int port, SOCKADDR addr) { #endif - char* name = config.hostname; - + SSL* s = NULL; #ifndef NO_SSL SSL_CTX* ctx = NULL; - SSL* s = NULL; bool sslworks = false; if(ssl) { ctx = tw_create_ssl_ctx(port); @@ -463,32 +475,45 @@ int tw_server_pass(void* ptr) { if(SSL_accept(s) <= 0) goto cleanup; sslworks = true; } -#else - void* s = NULL; #endif - + char* name = config.hostname; char address[513]; - address[0] = 0; + int ret; + struct tw_http_request req; + struct tw_http_response res; + struct tw_tool tools; #ifndef NO_GETADDRINFO struct sockaddr* sa = (struct sockaddr*)&addr; getnameinfo(sa, sizeof(addr), address, 512, NULL, 0, NI_NUMERICHOST); #endif + address[0] = 0; +#ifdef FREE_PTR + free(ptr); +#endif - struct tw_http_request req; - struct tw_http_response res; - struct tw_tool tools; res._processed = false; tw_init_tools(&tools); - int ret = tw_http_parse(s, sock, &req); + ret = tw_http_parse(s, sock, &req); if(ret == 0) { char date[513]; time_t t = time(NULL); struct tm* tm = localtime(&t); - strftime(date, 512, "%a, %d %b %Y %H:%M:%S %Z", tm); - char* useragent = cm_strdup(""); - int i; + char* tmp; + char* tmp2; + char* tmp3; + char* tmp4; + char* tmp5; + char* log; + char* vhost; + time_t cmtime; + bool rej; + char* host; + int port; + struct tw_config_entry* vhost_entry; + strftime(date, 512, "%a, %d %b %Y %H:%M:%S %Z", tm); + for(i = 0; req.headers[i] != NULL; i += 2) { if(cm_strcaseequ(req.headers[i], "User-Agent")) { free(useragent); @@ -496,12 +521,12 @@ int tw_server_pass(void* ptr) { } } - char* tmp = cm_strcat3(address, " - [", date); - char* tmp2 = cm_strcat3(tmp, "] \"", req.method); - char* tmp3 = cm_strcat3(tmp2, " ", req.path); - char* tmp4 = cm_strcat3(tmp3, " ", req.version); - char* tmp5 = cm_strcat3(tmp4, "\" \"", useragent); - char* log = cm_strcat(tmp5, "\""); + tmp = cm_strcat3(address, " - [", date); + tmp2 = cm_strcat3(tmp, "] \"", req.method); + tmp3 = cm_strcat3(tmp2, " ", req.path); + tmp4 = cm_strcat3(tmp3, " ", req.version); + tmp5 = cm_strcat3(tmp4, "\" \"", useragent); + log = cm_strcat(tmp5, "\""); free(tmp); free(tmp2); free(tmp3); @@ -511,8 +536,8 @@ int tw_server_pass(void* ptr) { cm_force_log(log); free(log); - char* vhost = cm_strdup(config.hostname); - time_t cmtime = 0; + vhost = cm_strdup(config.hostname); + cmtime = 0; if(req.headers != NULL) { for(i = 0; req.headers[i] != NULL; i += 2) { if(cm_strcaseequ(req.headers[i], "Host")) { @@ -520,10 +545,12 @@ int tw_server_pass(void* ptr) { vhost = cm_strdup(req.headers[i + 1]); } else if(cm_strcaseequ(req.headers[i], "If-Modified-Since")) { struct tm tm; + time_t t; + struct tm* btm; strptime(req.headers[i + 1], "%a, %d %b %Y %H:%M:%S GMT", &tm); -#if defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__) - time_t t = 0; - struct tm* btm = localtime(&t); +#if defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__) || defined(_MSC_VER) + t = 0; + btm = localtime(&t); cmtime = mktime(&tm); cmtime -= (btm->tm_hour * 60 + btm->tm_min) * 60; #else @@ -532,10 +559,10 @@ int tw_server_pass(void* ptr) { } } } - bool rej = false; + rej = false; cm_log("Server", "Host is %s", vhost); - int port = s == NULL ? 80 : 443; - char* host = cm_strdup(vhost); + port = s == NULL ? 80 : 443; + host = cm_strdup(vhost); for(i = 0; vhost[i] != 0; i++) { if(vhost[i] == ':') { host[i] = 0; @@ -545,7 +572,7 @@ int tw_server_pass(void* ptr) { } name = host; cm_log("Server", "Hostname is `%s', port is `%d'", host, port); - struct tw_config_entry* vhost_entry = tw_vhost_match(host, port); + vhost_entry = tw_vhost_match(host, port); #ifdef HAS_CHROOT char* chrootpath = vhost_entry->chroot_path != NULL ? vhost_entry->chroot_path : config.root.chroot_path; if(chrootpath != NULL) { @@ -581,11 +608,14 @@ int tw_server_pass(void* ptr) { } } if(!res._processed) { + char* path; + char* rpath; + struct stat st; cm_log("Server", "Document root is %s", vhost_entry->root == NULL ? "not set" : vhost_entry->root); - char* path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path); + path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path); cm_log("Server", "Filesystem path is %s", path); -#ifdef __MINGW32__ - char* rpath = cm_strdup(path); +#if defined(__MINGW32__) || defined(_MSC_VER) + rpath = cm_strdup(path); for(i = strlen(rpath) - 1; i >= 0; i--) { if(rpath[i] == '/') { int j; @@ -611,14 +641,13 @@ int tw_server_pass(void* ptr) { } free(rpath); #endif - struct stat st; if(!rej && stat(path, &st) == 0) { if(!tw_permission_allowed(path, addr, req, vhost_entry)) { tw_http_error(s, sock, 403, name, port, vhost_entry); } else if(S_ISDIR(st.st_mode)) { if(req.path[strlen(req.path) - 1] != '/') { - cm_log("Server", "Accessing directory without the slash at the end"); char* headers[3] = {"Location", cm_strcat(req.path, "/"), NULL}; + cm_log("Server", "Accessing directory without the slash at the end"); _tw_process_page(s, sock, tw_http_status(301), NULL, NULL, NULL, 0, headers, 0, 0); free(headers[1]); } else { @@ -631,6 +660,8 @@ int tw_server_pass(void* ptr) { if(f != NULL) { char* ext = NULL; int j; + struct stat st; + char* mime; for(j = strlen(p) - 1; j >= 0; j--) { if(p[j] == '.') { ext = cm_strdup(p + j); @@ -639,9 +670,8 @@ int tw_server_pass(void* ptr) { break; } } - struct stat st; stat(p, &st); - char* mime = tw_get_mime(ext, vhost_entry); + mime = tw_get_mime(ext, vhost_entry); tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, 0, 0); fclose(f); if(ext != NULL) free(ext); @@ -653,8 +683,13 @@ int tw_server_pass(void* ptr) { } if(!found) { char* str = malloc(1); + char** items; + int readme; + char** readmes; + int readme_count; + int hp; str[0] = 0; - char** items = cm_scandir(path); + items = cm_scandir(path); addstring(&str, "\n"); addstring(&str, "\n"); addstring(&str, " \n"); @@ -672,17 +707,22 @@ int tw_server_pass(void* ptr) { addstring(&str, " MIME\n"); addstring(&str, " Size\n"); addstring(&str, " \n"); - int readme = -1; - char** readmes = vhost_entry->readme_count == 0 ? config.root.readmes : vhost_entry->readmes; - int readme_count = vhost_entry->readme_count == 0 ? config.root.readme_count : vhost_entry->readme_count; + readme = -1; + readmes = vhost_entry->readme_count == 0 ? config.root.readmes : vhost_entry->readmes; + readme_count = vhost_entry->readme_count == 0 ? config.root.readme_count : vhost_entry->readme_count; if(items != NULL) { int phase = 0; doit: for(i = 0; items[i] != NULL; i++) { int j; + char* ext; + char* itm; + char* icon; char* fpth = cm_strcat3(path, "/", items[i]); struct stat s; char size[512]; + char* showmime; + char* mime; size[0] = 0; stat(fpth, &s); if(phase == 0 && !S_ISDIR(s.st_mode)) { @@ -704,21 +744,21 @@ int tw_server_pass(void* ptr) { continue; } } - if(s.st_size < 1024ULL) { + if(s.st_size < NUM1024) { sprintf(size, "%d", (int)s.st_size); - } else if(s.st_size < 1024ULL * 1024) { + } else if(s.st_size < NUM1024 * 1024) { sprintf(size, "%.1fK", (double)s.st_size / 1024); - } else if(s.st_size < 1024ULL * 1024 * 1024) { + } else if(s.st_size < NUM1024 * 1024 * 1024) { sprintf(size, "%.1fM", (double)s.st_size / 1024 / 1024); - } else if(s.st_size < 1024ULL * 1024 * 1024 * 1024) { + } else if(s.st_size < NUM1024 * 1024 * 1024 * 1024) { sprintf(size, "%.1fG", (double)s.st_size / 1024 / 1024 / 1024); - } else if(s.st_size < 1024ULL * 1024 * 1024 * 1024 * 1024) { + } else if(s.st_size < NUM1024 * 1024 * 1024 * 1024 * 1024) { sprintf(size, "%.1fT", (double)s.st_size / 1024 / 1024 / 1024 / 1024); } free(fpth); - char* ext = NULL; + ext = NULL; for(j = strlen(items[i]) - 1; j >= 0; j--) { if(items[i][j] == '.') { ext = cm_strdup(items[i] + j); @@ -727,8 +767,8 @@ int tw_server_pass(void* ptr) { break; } } - char* showmime = ""; - char* mime = tw_get_mime(ext, vhost_entry); + showmime = ""; + mime = tw_get_mime(ext, vhost_entry); if(strcmp(items[i], "../") == 0) { mime = "misc/parent"; size[0] = 0; @@ -738,9 +778,9 @@ int tw_server_pass(void* ptr) { } else { showmime = mime; } - char* icon = tw_get_icon(mime, vhost_entry); + icon = tw_get_icon(mime, vhost_entry); if(ext != NULL) free(ext); - char* itm = cm_strdup(items[i]); + itm = cm_strdup(items[i]); if(strlen(itm) >= 32) { if(itm[strlen(itm) - 1] == '/') { itm[31] = 0; @@ -770,11 +810,13 @@ int tw_server_pass(void* ptr) { } addstring(&str, " \n"); if(readme != -1) { - addstring(&str, "
\n"); - char* fpth = cm_strcat3(path, "/", readmes[readme]); struct stat s; + FILE* fr; + char* fpth; + addstring(&str, "
\n"); + fpth = cm_strcat3(path, "/", readmes[readme]); stat(fpth, &s); - FILE* fr = fopen(fpth, "r"); + fr = fopen(fpth, "r"); if(fr != NULL) { char* rmbuf = malloc(s.st_size + 1); rmbuf[s.st_size] = 0; @@ -786,7 +828,7 @@ int tw_server_pass(void* ptr) { free(fpth); } addstring(&str, "
\n"); - int hp = vhost_entry->hideport == -1 ? config.root.hideport : vhost_entry->hideport; + hp = vhost_entry->hideport == -1 ? config.root.hideport : vhost_entry->hideport; if(hp == 0) { addstring(&str, "
%s Server at %s Port %d
\n", tw_server, name, port); } else { @@ -800,6 +842,8 @@ int tw_server_pass(void* ptr) { } } else { char* ext = NULL; + char* mime; + FILE* f; for(i = strlen(req.path) - 1; i >= 0; i--) { if(req.path[i] == '.') { ext = cm_strdup(req.path + i); @@ -808,9 +852,9 @@ int tw_server_pass(void* ptr) { break; } } - char* mime = tw_get_mime(ext, vhost_entry); + mime = tw_get_mime(ext, vhost_entry); if(ext != NULL) free(ext); - FILE* f = fopen(path, "rb"); + f = fopen(path, "rb"); if(f == NULL) { tw_http_error(s, sock, 403, name, port, vhost_entry); } else { @@ -840,8 +884,8 @@ cleanup: SSL_free(s); #endif close_socket(sock); -#ifdef __MINGW32__ - _endthreadex(0); +#if defined(__MINGW32__) || defined(_MSC_VER) + _endthread(0); #elif defined(__HAIKU__) exit_thread(0); #endif @@ -853,7 +897,7 @@ extern SERVICE_STATUS status; extern SERVICE_STATUS_HANDLE status_handle; #endif -#if defined(__MINGW32__) || defined(__HAIKU__) +#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER) struct thread_entry { #ifdef __HAIKU__ thread_id thread; @@ -868,7 +912,11 @@ extern int running; void tw_server_loop(void) { int i; -#if defined(__MINGW32__) || defined(__HAIKU__) +#ifndef USE_POLL + fd_set fdset; + struct timeval tv; +#endif +#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER) struct thread_entry threads[2048]; for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) { threads[i].used = false; @@ -880,13 +928,11 @@ void tw_server_loop(void) { pollfds[i].fd = sockets[i]; pollfds[i].events = POLLIN | POLLPRI; } -#else - fd_set fdset; - struct timeval tv; #endif while(running) { + int ret; #ifdef USE_POLL - int ret = poll(pollfds, sockcount, 1000); + ret = poll(pollfds, sockcount, 1000); #else FD_ZERO(&fdset); for(i = 0; i < sockcount; i++) { @@ -895,13 +941,13 @@ void tw_server_loop(void) { tv.tv_sec = 1; tv.tv_usec = 0; #ifdef __HAIKU__ - int ret = select(32, &fdset, NULL, NULL, &tv); + ret = select(32, &fdset, NULL, NULL, &tv); #else - int ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv); + ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv); #endif #endif if(ret == -1) { -#ifndef __MINGW32__ +#if !defined(__MINGW32__) && !defined(_MSC_VER) cm_log("Server", "Select failure: %s", strerror(errno)); #endif break; @@ -925,37 +971,24 @@ void tw_server_loop(void) { SOCKADDR claddr; socklen_t clen = sizeof(claddr); int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen); - cm_log("Server", "New connection accepted"); -#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_PSP) || defined(__PPU__) +#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER) + int j; struct pass_entry* e = malloc(sizeof(*e)); + cm_log("Server", "New connection accepted"); e->sock = sock; - e->ssl = config.ports[i] & (1ULL << 32); +#ifdef _MSC_VER + e->ssl = config.ports[i] & (1UL << 31); +#else + e->ssl = config.ports[i] & (1ULL << 31); +#endif e->port = config.ports[i]; e->addr = claddr; #endif -#ifdef __MINGW32__ - int j; - for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) { - if(threads[j].used) { - DWORD ex; - GetExitCodeThread(threads[j].handle, &ex); - if(ex != STILL_ACTIVE) { - CloseHandle(threads[j].handle); - threads[j].used = false; - } - } - } - for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) { - if(!threads[j].used) { - threads[j].handle = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL); - threads[j].used = true; - break; - } - } +#if defined(__MINGW32__) || defined(_MSC_VER) + _beginthread(tw_server_pass, 0, e); #elif defined(_PSP) || defined(__PPU__) tw_server_pass(e); #elif defined(__HAIKU__) - int j; for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) { if(threads[j].used) { thread_info info; @@ -982,7 +1015,7 @@ void tw_server_loop(void) { if(pid == 0) { int j; for(j = 0; j < sockcount; j++) close_socket(sockets[j]); - tw_server_pass(sock, config.ports[i] & (1ULL << 32), config.ports[i], claddr); + tw_server_pass(sock, config.ports[i] & (1ULL << 31), config.ports[i], claddr); _exit(0); } else { close_socket(sock); diff --git a/Server/strptime.c b/Server/strptime.c index fb584c9..1165cff 100644 --- a/Server/strptime.c +++ b/Server/strptime.c @@ -35,7 +35,7 @@ //#include //__RCSID("$NetBSD: strptime.c,v 1.62 2017/08/24 01:01:09 ginsbach Exp $"); -#if defined(__MINGW32__) +#if defined(__MINGW32__) || defined(_MSC_VER) #include #include @@ -291,10 +291,11 @@ recurse: case 'D': /* The date as "%y/%m/%d". */ { + int year; new_fmt = HERE_D_FMT; LEGAL_ALT(0); state |= S_MON | S_MDAY | S_YEAR; - const int year = split_year ? tm->tm_year : 0; + year = split_year ? tm->tm_year : 0; bp = (const unsigned char *)strptime((const char *)bp, new_fmt, tm); @@ -398,12 +399,20 @@ recurse: continue; #ifndef TIME_MAX +#ifdef _MSC_VER +#define TIME_MAX INT32_MAX +#else #define TIME_MAX INT64_MAX +#endif #endif case 's': /* seconds since the epoch */ { time_t sse = 0; +#ifdef _MSC_VER + uint32_t rulim = TIME_MAX; +#else uint64_t rulim = TIME_MAX; +#endif if (*bp < '0' || *bp > '9') { bp = NULL; @@ -416,13 +425,20 @@ recurse: rulim /= 10; } while ((sse * 10 <= TIME_MAX) && rulim && *bp >= '0' && *bp <= '9'); - +#ifdef _MSC_VER + if (sse < 0 || (uint32_t)sse > TIME_MAX) { +#else if (sse < 0 || (uint64_t)sse > TIME_MAX) { +#endif bp = NULL; continue; } #ifdef _WIN32 +#ifdef _MSC_VER + if (1) +#else if (localtime_s(tm, &sse) == 0) +#endif #else if (localtime_r(&sse, tm)) #endif diff --git a/Server/tw_config.h b/Server/tw_config.h index 3e66276..823f2f3 100644 --- a/Server/tw_config.h +++ b/Server/tw_config.h @@ -14,7 +14,7 @@ extern "C" { #include #include -#ifdef __MINGW32__ +#if defined(__MINGW32__) || defined(_MSC_VER) #include #define NO_IPV6 #else @@ -42,6 +42,12 @@ extern "C" { #define MAX_INDEX 1024 #define MAX_README 8 +#ifdef _MSC_VER +#define NUM1024 1024UL +#else +#define NUM1024 1024ULL +#endif + enum TW_DIR_TYPE { TW_DIR_ALLOW = 0, TW_DIR_DENY @@ -88,7 +94,11 @@ struct tw_config_entry { }; struct tw_config { +#ifdef _MSC_VER + uint32_t ports[MAX_PORTS + 1]; +#else uint64_t ports[MAX_PORTS + 1]; /* If port & (1 << 32) is non-zero, it is SSL */ +#endif char hostname[1025]; char* defined[1025]; struct tw_config_entry root; diff --git a/Server/version.c b/Server/version.c index 93568a0..1e15710 100644 --- a/Server/version.c +++ b/Server/version.c @@ -13,7 +13,7 @@ const char* tw_platform = "NetBSD" #elif defined(__linux__) "Linux" -#elif defined(__MINGW32__) +#elif defined(__MINGW32__) || defined(_MSC_VER) "Windows" #elif defined(__HAIKU__) "Haiku" diff --git a/VC6Compat/stdbool.h b/VC6Compat/stdbool.h new file mode 100644 index 0000000..7d0e88c --- /dev/null +++ b/VC6Compat/stdbool.h @@ -0,0 +1,11 @@ +/* $Id$ */ + +#ifndef __STDBOOL_H__ +#define __STDBOOL_H__ + +typedef unsigned char bool; + +#define true 1 +#define false 0 + +#endif diff --git a/VC6Compat/stdint.h b/VC6Compat/stdint.h new file mode 100644 index 0000000..2ea98f7 --- /dev/null +++ b/VC6Compat/stdint.h @@ -0,0 +1,16 @@ +/* $Id$ */ + +#ifndef __STDINT_H__ +#define __STDINT_H__ + +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned int uint32_t; +typedef signed char int8_t; +typedef signed short int16_t; +typedef signed int int32_t; +typedef int socklen_t; + +#define INT32_MAX 0x7fffffff + +#endif diff --git a/config.h.tmpl b/config.h.tmpl index 35baaca..dda9796 100644 --- a/config.h.tmpl +++ b/config.h.tmpl @@ -17,16 +17,21 @@ #define SSL void #endif -#if defined(__MINGW32__) && defined(USE_POLL) +#if (defined(__MINGW32__) || defined(_MSC_VER)) && defined(USE_POLL) #undef USE_POLL /* Force select(2) for Windows */ #endif -#if defined(__MINGW32__) && defined(HAS_CHROOT) +#if (defined(__MINGW32__) || defined(_MSC_VER)) && defined(HAS_CHROOT) #undef HAS_CHROOT /* Windows should not have chroot */ #endif +#if defined(_MSC_VER) && !defined(NO_GETADDRINFO) +#define NO_GETADDRINFO +/* Do not use getaddrinfo */ +#endif + #if (defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__)) && !defined(NO_IPV6) #define NO_IPV6 /* PSP/PS2/PS3 does not have IPv6 */ diff --git a/vc6.sh b/vc6.sh new file mode 100644 index 0000000..abd3c02 --- /dev/null +++ b/vc6.sh @@ -0,0 +1,53 @@ +#!/bin/sh +# $Id$ +# Wrapper for CL. VC6 sucks. + +outfile="a.out" +dowhat="" +options="/I../VC6Compat" +obj=0 +source="" +libraries="" +link="" + +for i in "$@"; do + if [ "$i" = "-o" ]; then + dowhat="output" + elif [ "$i" = "-I" ]; then + dowhat="include" + elif [ "$i" = "-c" ]; then + options="$options /c" + obj=1 + elif [ "$i" = "-fPIC" ]; then + : + elif [ "$i" = "-g" ]; then + : + elif [ "$i" = "-std=c99" ]; then + : + elif [ "$i" = "-shared" ]; then + options="$options /LD" + elif [ "`echo "$i" | grep -Eo "^-D"`" = "-D" ]; then + options="$options /`echo "$i" | sed "s/^-//g"`" + elif [ "`echo "$i" | grep -Eo "^-l"`" = "-l" ]; then + libraries="$libraries `echo "$i" | sed "s/^-l//g"`.lib" + elif [ "$dowhat" = "output" ]; then + dowhat="" + outfile="$i" + elif [ "$dowhat" = "include" ]; then + dowhat="" + options="$options /I$i" + elif [ ! "`echo "$i" | grep -Eo "^."`" = "-" ]; then + source="$source $i" + fi +done +if [ "$obj" = "1" ]; then + options="$options /Fo$outfile" +else + options="$options /Fe$outfile" +fi +if [ ! "$libraries" = "" ]; then + link="/link /nodefaultlib:libc $libraries" +fi +construct="cl /nologo $options $source $link" +echo "Run: $construct" +$construct