bpaul-http

http server that will eventually be used for bpaul.xyz
Log | Files | Refs | README

commit d00acd0e8faddabd28bb56a15c2dc022ecc667d8
parent 12b936afa0b195c86d6c1e97e59198c0f6b17e51
Author: benjamin paul <bpaul@bpaul.xyz>
Date:   Sun,  1 Aug 2021 17:51:33 +1000

file reading function

Diffstat:
Mfile.c | 14++++++++++++++
Mfile.h | 1+
Mmain.c | 6+-----
3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/file.c b/file.c @@ -1,4 +1,5 @@ #include <stdio.h> +#include <stdlib.h> size_t fsize(FILE *f) { @@ -10,3 +11,16 @@ fsize(FILE *f) { return length; } + +char * +read_file(const char *name) { + char *buf; + size_t length; + + FILE *f = fopen(name, "r"); + length = fsize(f); + buf = malloc(length); + fread(buf, 1, length, f); + + return buf; +} diff --git a/file.h b/file.h @@ -3,3 +3,4 @@ #include <stdio.h> size_t fsize(FILE *f); +char *read_file(const char *name); diff --git a/main.c b/main.c @@ -12,11 +12,7 @@ main() { sock = create_socket(); char *buf; - size_t length; - FILE *f = fopen("index.html", "r"); - length = fsize(f); - buf = malloc(length); - fread(buf, 1, length, f); + buf = read_file("index.html"); for (;;) { asock = accept_socket(sock);