start using bdwgc

master
Drake 2 years ago
parent b74d15ead8
commit d207a91c3f

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

Binary file not shown.

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

@ -2,50 +2,47 @@
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <gc.h>
#include "color.h"
void info(char* ustr, ...) {
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);
//strcat(str, ustr);
va_list lst;
va_start(lst, ustr);
vprintf(str, lst);
free(str);
}
void warn(char* ustr, ...) {
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);
va_list lst;
va_start(lst, ustr);
vprintf(str, lst);
free(str);
}
void error(char* ustr, ...) {
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);
va_list lst;
va_start(lst, ustr);
vprintf(str, lst);
free(str);
}
void reqlog(char* ustr, ...) {
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);
va_list lst;
va_start(lst, ustr);
vprintf(str, lst);
free(str);
}

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

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

Loading…
Cancel
Save