start using bdwgc

master
Drake 2 years ago
parent b74d15ead8
commit d207a91c3f

@ -4,6 +4,10 @@ CC_FLAGS ?=
CC_FLAGS := ${CC_FLAGS} -Ilib CC_FLAGS := ${CC_FLAGS} -Ilib
DESTDIR ?= / DESTDIR ?= /
ifndef NO_GC
CC_FLAGS := ${CC_FLAGS} -lgc
endif
.PHONY: all debug clean build-release install .PHONY: all debug clean build-release install
all: all:
@$(shell mkdir -p bin) @$(shell mkdir -p bin)

Binary file not shown.

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <gc.h>
char* readFile(char* path) { char* readFile(char* path) {
path++; path++;
@ -22,11 +23,10 @@ char* readFile(char* path) {
int length = ftell(fp); int length = ftell(fp);
rewind(fp); rewind(fp);
buf = (char*)malloc(sizeof(char) * (length + 1)); buf = (char*)GC_MALLOC(sizeof(char) * (length + 1));
fread(buf, sizeof(char), length, fp); fread(buf, sizeof(char), length, fp);
buf[length] = '\0'; buf[length] = '\0';
char* tmp = buf; char* tmp = buf;
free(buf);
return tmp; return tmp;
} }

@ -2,50 +2,47 @@
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <gc.h>
#include "color.h" #include "color.h"
void info(char* ustr, ...) { void info(char* ustr, ...) {
char* tstr = GRN "INFO: " CRESET; char* tstr = GRN "INFO: " CRESET;
char* str = malloc(strlen(tstr) + strlen(ustr) + 1); char* str = GC_MALLOC(strlen(tstr) + strlen(ustr) + 1);
sprintf(str, "%s%s", tstr, ustr); sprintf(str, "%s%s", tstr, ustr);
//strcat(str, ustr); //strcat(str, ustr);
va_list lst; va_list lst;
va_start(lst, ustr); va_start(lst, ustr);
vprintf(str, lst); vprintf(str, lst);
free(str);
} }
void warn(char* ustr, ...) { void warn(char* ustr, ...) {
char* tstr = YEL "WARN: " CRESET; char* tstr = YEL "WARN: " CRESET;
char* str = malloc(strlen(tstr) + strlen(ustr) + 1); char* str = GC_MALLOC(strlen(tstr) + strlen(ustr) + 1);
sprintf(str, "%s%s", tstr, ustr); sprintf(str, "%s%s", tstr, ustr);
va_list lst; va_list lst;
va_start(lst, ustr); va_start(lst, ustr);
vprintf(str, lst); vprintf(str, lst);
free(str);
} }
void error(char* ustr, ...) { void error(char* ustr, ...) {
char* tstr = RED "ERR: " CRESET; char* tstr = RED "ERR: " CRESET;
char* str = malloc(strlen(tstr) + strlen(ustr) + 1); char* str = GC_MALLOC(strlen(tstr) + strlen(ustr) + 1);
sprintf(str, "%s%s", tstr, ustr); sprintf(str, "%s%s", tstr, ustr);
va_list lst; va_list lst;
va_start(lst, ustr); va_start(lst, ustr);
vprintf(str, lst); vprintf(str, lst);
free(str);
} }
void reqlog(char* ustr, ...) { void reqlog(char* ustr, ...) {
char* tstr = WHT "REQ: " CRESET; char* tstr = WHT "REQ: " CRESET;
char* str = malloc(strlen(tstr) + strlen(ustr) + 1); char* str = GC_MALLOC(strlen(tstr) + strlen(ustr) + 1);
sprintf(str, "%s%s", tstr, ustr); sprintf(str, "%s%s", tstr, ustr);
va_list lst; va_list lst;
va_start(lst, ustr); va_start(lst, ustr);
vprintf(str, lst); vprintf(str, lst);
free(str);
} }

@ -1,6 +1,7 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include <stdio.h> #include <stdio.h>
#include <getopt.h> #include <getopt.h>
#include <gc.h>
#include "srv.c" #include "srv.c"
@ -17,6 +18,7 @@ void help(char* exe) {
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
GC_INIT();
char* ADDR = DEFAULT_ADDR; char* ADDR = DEFAULT_ADDR;
int PORT = DEFAULT_PORT; int PORT = DEFAULT_PORT;

@ -1,3 +1,4 @@
#include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
@ -7,12 +8,20 @@
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <gc.h>
#include "resHeader.h" #include "resHeader.h"
#include "reqHeader.h" #include "reqHeader.h"
#include "file.h" #include "file.h"
#include "log.h" #include "log.h"
void safeExit(int sig) {
info("Exiting...\n");
GC_gcollect();
exit(0);
}
int srv(char* ADDR, int PORT) { int srv(char* ADDR, int PORT) {
int srvSocket; int srvSocket;
struct sockaddr_in srvAddr; struct sockaddr_in srvAddr;
@ -37,6 +46,8 @@ int srv(char* ADDR, int PORT) {
return 1; return 1;
} }
signal(SIGINT, safeExit);
info("Server (probably) listening on http://%s:%d\n", ADDR, PORT); info("Server (probably) listening on http://%s:%d\n", ADDR, PORT);
while(1) { while(1) {
@ -93,11 +104,10 @@ funy: {
reqlog("%d %s\n", rawHeader.status, request.path); reqlog("%d %s\n", rawHeader.status, request.path);
char tmpHeader[1024]; char tmpHeader[1024];
makeHeader(tmpHeader, &rawHeader); makeHeader(tmpHeader, &rawHeader);
char* resHeader = malloc(strlen(tmpHeader) + strlen(file) + 1); //= "HTTP/1.1 200 OK\r\n\nHello, world!"; char* resHeader = GC_MALLOC(strlen(tmpHeader) + strlen(file) + 1); //= "HTTP/1.1 200 OK\r\n\nHello, world!";
sprintf(resHeader, "%s%s", (char*)tmpHeader, file); sprintf(resHeader, "%s%s", (char*)tmpHeader, file);
send(client, resHeader, strlen(resHeader), 0); send(client, resHeader, strlen(resHeader), 0);
close(client); close(client);
free(resHeader);
} }
} }
} }

Loading…
Cancel
Save