diff --git a/HTML/index.html b/HTML/index.html new file mode 100644 index 0000000..ea4f298 --- /dev/null +++ b/HTML/index.html @@ -0,0 +1,25 @@ + + + + + + + + + Hello, world! + + + + + + + + + + + +

Hello, world!

+ + + + diff --git a/WebServer b/WebServer new file mode 100755 index 0000000..fa23060 Binary files /dev/null and b/WebServer differ diff --git a/include/fileReader.h b/include/fileReader.h new file mode 100644 index 0000000..646d366 --- /dev/null +++ b/include/fileReader.h @@ -0,0 +1,7 @@ +#ifndef FILEREADER_H +#define FILEREADER_H + +char *ReadHTML(char path[]); +int GetFilesize(FILE *file_ptr); + +#endif \ No newline at end of file diff --git a/makefile b/makefile index 14a478e..f44e6cf 100644 --- a/makefile +++ b/makefile @@ -3,14 +3,25 @@ CC = gcc # C compiler CFLAGS = -O2 # Compiler flags (optimization level) -main: main.o +SRC_DIR = src +BUILD_DIR = build +BIN_DIR = bin +EXECUTABLE = WebServer + +# Create directories +$(shell mkdir -p $(BUILD_DIR) $(BIN_DIR)) + +SOURCES = $(wildcard $(SRC_DIR)/*.c) +OBJECT_FILES = $(SOURCES:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.o) + +$(EXECUTABLE): $(OBJECT_FILES) $(CC) $(CFLAGS) -o $@ $^ -main.o: main.c - $(CC) $(CFLAGS) -c $< +$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c + $(CC) $(CFLAGS) -c $< -o $@ run: - ./main + ./$(EXECUTABLE) clean: - rm -f *.o + rm -f $(BUILD_DIR)/*.o $(BIN_DIR)/* diff --git a/src/fileReader.c b/src/fileReader.c new file mode 100644 index 0000000..342e255 --- /dev/null +++ b/src/fileReader.c @@ -0,0 +1,34 @@ +#include +#include "../include/fileReader.h" + +char *ReadHTML(char path[]) { + FILE *file_ptr = fopen(path, "r"); + char file[GetFilesize(file_ptr)]; + char ch; + int i = 0; + + if (NULL == file_ptr) { + printf("File can't be opened \n"); + } + + while ((ch = fgetc(file_ptr)) != EOF) { + file[i] = ch; + i++; + } + + fclose(file_ptr); + + return file; +} + +int GetFilesize(FILE *file_ptr) { + int prev = ftell(file_ptr); + + fseek(file_ptr, 0L, SEEK_END); + + int size = ftell(file_ptr); + + fseek(file_ptr, prev, SEEK_SET); + + return size; +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index 3350b8e..4aa87b5 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,7 @@ #include +#include "../include/fileReader.h" int main() { - - - - + char *file = ReadHTML("/home/skingging/Documents/Projects/C/C-Webserver/HTML/index.html"); + printf("Size: %d", sizeof(file)); } \ No newline at end of file