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.

68 lines
1.2 KiB

#pragma once
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#ifdef NO_GC
#include <gc.h>
#define malloc(n) GC_malloc(n)
#else
#include <stdlib.h>
#endif
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;
fseek(fp, 0, SEEK_END);
int length = ftell(fp);
rewind(fp);
buf = (char*)malloc(sizeof(char) * (length + 1));
fread(buf, sizeof(char), length, fp);
fclose(fp);
char* tmp = buf;
return tmp;
}
void* readFileBin(int* len, 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 "";
}
void* buf;
struct stat st;
stat(path, &st);
int length = st.st_size;
*len = length;
buf = (void*)malloc(sizeof(int) * (length + 1));
fread(buf, sizeof(int), length, fp);
fclose(fp);
void* tmp = buf;
return tmp;
}