From 6cc006e821030853640030ded24b11e27f2d4ec7 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Thu, 18 Jul 2019 19:57:32 -0400 Subject: moving towards correct --- index.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/index.c b/index.c index 87aeaa3..b617b91 100644 --- a/index.c +++ b/index.c @@ -1,7 +1,60 @@ #define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include +#include #include "blog.h" +#include + void add_to_index(const char *path, const char *title) { + printf("Status: 200 OK\r\n"); + printf("Content-Type: text/plain\r\n\r\n"); + + int blogdir = open(DATA_DIRECTORY, O_DIRECTORY); + if (blogdir == -1) { + printf("open(DATA_DIRECTORY): %s\n", strerror(errno)); + return; + } + + char *dir = strdup(path); + if (dir == NULL) { + printf("strdup(path): %s\n", strerror(errno)); + return; + } + + char index_path[FILENAME_MAX] = { 0 }; + snprintf(index_path, sizeof(index_path), "%s/index.html", dirname(dir)); + + /* open index.html */ + int index_fd = openat(blogdir, index_path, O_RDWR | O_CREAT, 0644); + if (index_fd == -1) { + printf("open(index_path): %s\n", strerror(errno)); + return; + } + + /* find the end of text */ + off_t eot = 0; + + if (eot == 0) { + dprintf(index_fd, HTML_HEAD, "index"); + } else { + ftruncate(index_fd, eot); + } + + /* add link */ + dprintf(index_fd, "%s
", path, title); + + /* (re-)add footer */ + dprintf(index_fd, HTML_TAIL, current_year()); + + close(index_fd); + + + free(dir); + close(blogdir); } -- cgit v1.2.1