commit df564f898566b8744738e5bbbb3f2d18862917df
parent 84779e82ee102cbd9de52dbccda021738a20f44d
Author: benjamin paul <bpaul@bpaul.xyz>
Date: Thu, 5 Aug 2021 15:12:26 +1000
index.html for directory and also improved Makefile
Diffstat:
5 files changed, 52 insertions(+), 5 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,3 +1,3 @@
.ccls-cache
*.o
-prog
+bpaul-http
diff --git a/Makefile b/Makefile
@@ -1,2 +1,24 @@
-all:
- ${CC} -g *.c -o prog
+.POSIX:
+
+NAME = bpaul-http
+
+CC = cc
+CFLAGS = -W -O
+all: $(NAME)
+
+$(NAME): file.o http.o tcp.o main.o
+ $(CC) $(LDFLAGS) -o $(NAME) file.o http.o tcp.o main.o
+
+file.o: file.c file.h
+http.o: http.c http.h
+tcp.o: tcp.c tcp.h
+main.o: main.c file.h http.h tcp.h
+
+clean:
+ rm -f prog file.o http.o tcp.o main.o
+
+install:
+ echo 'i have not set this up yet sorry'
+
+uninstall:
+ echo 'i have not set this up yet sorry'
diff --git a/file.c b/file.c
@@ -1,7 +1,9 @@
#include <string.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
+#include <unistd.h>
size_t
fsize(const char *name) {
@@ -12,6 +14,20 @@ fsize(const char *name) {
return st.st_size;
}
+bool
+valid_path(const char *name) {
+ return access(name, F_OK) == 0;
+}
+
+bool
+is_directory(const char *name) {
+ struct stat st;
+
+ stat (name, &st);
+
+ return S_ISDIR(st.st_mode);
+}
+
char *
read_file(const char *name) {
char *buf;
diff --git a/file.h b/file.h
@@ -1,6 +1,9 @@
#pragma once
#include <stdio.h>
+#include <stdbool.h>
size_t fsize(FILE *f);
+bool valid_path(const char *name);
+bool is_directory(const char *name);
char *read_file(const char *name);
diff --git a/main.c b/main.c
@@ -11,6 +11,7 @@ main() {
int sock, asock;
sock = create_socket();
+
char *buf;
for (;;) {
@@ -24,8 +25,13 @@ main() {
file_name++;
/* If file is not found */
- if (access(file_name, F_OK) == 0) {
- buf = read_file(file_name);
+ if (valid_path(file_name)) {
+ if (is_directory(file_name)) {
+ strncat(file_name, "index.html", 11);
+ buf = read_file(file_name);
+ } else {
+ buf = read_file(file_name);
+ }
} else {
buf = read_file("index.html");
}