diff --git a/Makefile b/Makefile index 8393bfc..e6b5f3e 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ CC_FLAGS := ${CC_FLAGS} -Ilib DESTDIR ?= / ifndef NO_GC -CC_FLAGS := ${CC_FLAGS} -lgc +CC_FLAGS := ${CC_FLAGS} -lgc -DNO_GC=1 endif .PHONY: all debug clean build-release install diff --git a/bin/httpsrv b/bin/httpsrv new file mode 100755 index 0000000..0617f48 Binary files /dev/null and b/bin/httpsrv differ diff --git a/lib/file.h b/lib/file.h index fbbb0d3..d22dd29 100644 --- a/lib/file.h +++ b/lib/file.h @@ -1,9 +1,13 @@ #include -#include #include #include #include +#ifdef NO_GC #include +#define malloc(n) GC_malloc(n) +#else +#include +#endif char* readFile(char* path) { path++; @@ -23,7 +27,7 @@ char* readFile(char* path) { int length = ftell(fp); rewind(fp); - buf = (char*)GC_MALLOC(sizeof(char) * (length + 1)); + buf = (char*)malloc(sizeof(char) * (length + 1)); fread(buf, sizeof(char), length, fp); buf[length] = '\0'; diff --git a/lib/log.h b/lib/log.h index e35af6d..a62b19c 100644 --- a/lib/log.h +++ b/lib/log.h @@ -1,14 +1,18 @@ #include #include #include -#include +#ifdef NO_GC #include +#define malloc(n) GC_malloc(n) +#else +#include +#endif #include "color.h" void info(char* ustr, ...) { char* tstr = GRN "INFO: " CRESET; - char* str = GC_MALLOC(strlen(tstr) + strlen(ustr) + 1); + char* str = malloc(strlen(tstr) + strlen(ustr) + 1); sprintf(str, "%s%s", tstr, ustr); //strcat(str, ustr); @@ -19,7 +23,7 @@ void info(char* ustr, ...) { void warn(char* ustr, ...) { char* tstr = YEL "WARN: " CRESET; - char* str = GC_MALLOC(strlen(tstr) + strlen(ustr) + 1); + char* str = malloc(strlen(tstr) + strlen(ustr) + 1); sprintf(str, "%s%s", tstr, ustr); va_list lst; @@ -29,7 +33,7 @@ void warn(char* ustr, ...) { void error(char* ustr, ...) { char* tstr = RED "ERR: " CRESET; - char* str = GC_MALLOC(strlen(tstr) + strlen(ustr) + 1); + char* str = malloc(strlen(tstr) + strlen(ustr) + 1); sprintf(str, "%s%s", tstr, ustr); va_list lst; @@ -39,7 +43,7 @@ void error(char* ustr, ...) { void reqlog(char* ustr, ...) { char* tstr = WHT "REQ: " CRESET; - char* str = GC_MALLOC(strlen(tstr) + strlen(ustr) + 1); + char* str = malloc(strlen(tstr) + strlen(ustr) + 1); sprintf(str, "%s%s", tstr, ustr); va_list lst; diff --git a/src/main.c b/src/main.c index 115bc30..711b126 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,12 @@ #define _GNU_SOURCE #include #include +#include +#ifdef NO_GC #include +#else +#warning GC is disabled. There *will* be memory leaks all over the place! +#endif #include "srv.c" @@ -18,7 +23,9 @@ void help(char* exe) { } int main(int argc, char** argv) { +#ifdef NO_GC GC_INIT(); +#endif char* ADDR = DEFAULT_ADDR; int PORT = DEFAULT_PORT; @@ -43,7 +50,7 @@ int main(int argc, char** argv) { printf("%s\n", longopt); if (!strcmp(longopt, "help")) { help(argv[0]); - exit(0); + _exit(0); } else if (!strcmp(longopt, "addr")) { ADDR = optarg; } else if (!strcmp(longopt, "port")) { @@ -63,7 +70,7 @@ int main(int argc, char** argv) { break; case 'h': help(argv[0]); - exit(0); + _exit(0); } } diff --git a/src/srv.c b/src/srv.c index 1dd092f..6aa9a80 100644 --- a/src/srv.c +++ b/src/srv.c @@ -6,9 +6,13 @@ #include #include #include -#include #include +#ifdef NO_GC #include +#define malloc(n) GC_malloc(n) +#else +#include +#endif #include "resHeader.h" #include "reqHeader.h" @@ -18,8 +22,10 @@ void safeExit(int sig) { info("Exiting...\n"); +#ifdef NO_GC GC_gcollect(); - exit(0); +#endif + _exit(0); } int srv(char* ADDR, int PORT) { @@ -104,7 +110,7 @@ funy: { reqlog("%d %s\n", rawHeader.status, request.path); char tmpHeader[1024]; makeHeader(tmpHeader, &rawHeader); - char* resHeader = GC_MALLOC(strlen(tmpHeader) + strlen(file) + 1); //= "HTTP/1.1 200 OK\r\n\nHello, world!"; + char* resHeader = malloc(strlen(tmpHeader) + strlen(file) + 1); //= "HTTP/1.1 200 OK\r\n\nHello, world!"; sprintf(resHeader, "%s%s", (char*)tmpHeader, file); send(client, resHeader, strlen(resHeader), 0); close(client);