From f87be61844fed4988ed90863854b19d047297a37 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Thu, 18 Jul 2019 19:37:23 -0400 Subject: add current_year() function for use in address --- blog.c | 18 +++++++++++++----- blog.h | 2 ++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/blog.c b/blog.c index 5ff100b..2685eb2 100644 --- a/blog.c +++ b/blog.c @@ -9,6 +9,17 @@ #include #include "blog.h" +int current_year(void) +{ + static int year = 0; + if (year == 0) { + time_t t = time(NULL); + struct tm *tm = localtime(&t); + year = tm->tm_year + 1900; + } + return year; +} + int handle_post(void) { read_post_data(); @@ -60,14 +71,13 @@ int handle_post(void) if (write(newpost, body, strlen(body)) != strlen(body)) { return 1; } - dprintf(newpost, HTML_TAIL, tm->tm_year + 1900); + dprintf(newpost, HTML_TAIL, current_year()); close(newpost); close(blogdir); add_to_index(uri, title); - printf("Status: 302 Found\r\n"); printf("Location: http%s://%s/%s\r\n\r\n", getenv("HTTPS") ? "s" : "", getenv("HTTP_HOST"), uri); return 0; @@ -93,9 +103,7 @@ int main(void) puts(""); puts(""); - time_t t = time(NULL); - struct tm *tm = localtime(&t); - printf(HTML_TAIL, tm->tm_year + 1900); + printf(HTML_TAIL, current_year()); return 0; } diff --git a/blog.h b/blog.h index d61d670..590c18b 100644 --- a/blog.h +++ b/blog.h @@ -29,4 +29,6 @@ int show_entry(const char *path); void add_to_index(const char *path, const char *title); +int current_year(void); + #endif -- cgit v1.2.1