You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
585 B

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
char* readFile(char* path) {
path++;
if (strlen(path) <= 1) {
printf("/ converted to /index.html\n");
path = (char*)malloc(11);
path = "index.html";
}
FILE* fp = fopen(path, "rb");
if (!(access(path, F_OK) == 0)) {
errno = -1;
return "";
}
char* buf = NULL;
fseek(fp, 0, SEEK_END);
int length = ftell(fp);
rewind(fp);
buf = (char*)malloc(sizeof(char) * (length + 1));
fread(buf, sizeof(char), length, fp);
buf[length] = '\0';
char* tmp = buf;
return tmp;
}