about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org>2020-08-21 00:27:10 +0200
committersternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org>2020-08-21 00:27:10 +0200
commitd87520f6a1cdb31526a4370bb06bb7cf7a993a35 (patch)
tree652ba912d37497c848c44a9f4fee26f93a4d68c5
parentaac560829418855192783680d2fa3f80f8b84013 (diff)
refactor(stringutil): add function catn_alloc to ease url building in main.c
* bitutil.{c,h} → stringutil.{c,h} (more appropriate)
  * new function catn_alloc(size_t n, ...)
    which concats `n` given strings into an dynamically allocated buffer,
    mostly useful for building URLs which is done in blog_rss.
    may be a possibility to use in entry.c, but code there might be too
    specific.
-rw-r--r--Makefile2
-rw-r--r--bitutil.c25
-rw-r--r--bitutil.h1
-rw-r--r--cgiutil.c2
-rw-r--r--main.c6
-rw-r--r--stringutil.c64
-rw-r--r--stringutil.h2
-rw-r--r--timeutil.c2
8 files changed, 73 insertions, 31 deletions
diff --git a/Makefile b/Makefile
index 165bcf9..6c4216d 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ include config.mk
 
 ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
 
-sternenblog.cgi: xml.o entry.o index.o bitutil.o cgiutil.o timeutil.o $(TEMPLATE).o main.o
+sternenblog.cgi: xml.o entry.o index.o stringutil.o cgiutil.o timeutil.o $(TEMPLATE).o main.o
 	$(CC) $(CFLAGS) -o $@ $^
 
 main.o: main.c core.h timeutil.h config.h
diff --git a/bitutil.c b/bitutil.c
deleted file mode 100644
index 8b2807f..0000000
--- a/bitutil.c
+++ /dev/null
@@ -1,25 +0,0 @@
-char nibble_hex(short h) {
-    switch(h) {
-        case 0:
-        case 1:
-        case 2:
-        case 3:
-        case 4:
-        case 5:
-        case 6:
-        case 7:
-        case 8:
-        case 9:
-            return (h + 48);
-        case 10:
-        case 11:
-        case 12:
-        case 13:
-        case 14:
-        case 15:
-            return (h + 55);
-        default:
-            return 0;
-    }
-}
-
diff --git a/bitutil.h b/bitutil.h
deleted file mode 100644
index 57d15e6..0000000
--- a/bitutil.h
+++ /dev/null
@@ -1 +0,0 @@
-char nibble_hex(short h);
diff --git a/cgiutil.c b/cgiutil.c
index 043dade..135028d 100644
--- a/cgiutil.c
+++ b/cgiutil.c
@@ -3,7 +3,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include "bitutil.h"
+#include "stringutil.h"
 
 void send_header(char key[], char val[]) {
     fputs(key, stdout);
diff --git a/main.c b/main.c
index fb73915..b180d3d 100644
--- a/main.c
+++ b/main.c
@@ -88,6 +88,7 @@
 #include "cgiutil.h"
 #include "entry.h"
 #include "index.h"
+#include "stringutil.h"
 #include "timeutil.h"
 #include "template.h"
 #include "xml.h"
@@ -298,12 +299,13 @@ void blog_rss(char script_name[]) {
         xml_close_tag(&ctx, "ttl");
     }
 
-    char rss_link[256];
-    if(snprintf(rss_link, sizeof rss_link, "%s%s/rss.xml", BLOG_SERVER_URL, script_name) > 0) {
+    char *rss_link = catn_alloc(3, BLOG_SERVER_URL, script_name, "/rss.xml");
+    if(rss_link != NULL) {
         xml_empty_tag(&ctx, "atom:link", 3,
                       "rel", "self",
                       "href", rss_link,
                       "type", "application/rss+xml");
+        free(rss_link);
     }
 
     for(int i = 0; i < count; i++) {
diff --git a/stringutil.c b/stringutil.c
new file mode 100644
index 0000000..baef713
--- /dev/null
+++ b/stringutil.c
@@ -0,0 +1,64 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+char nibble_hex(short h) {
+    switch(h) {
+        case 0:
+        case 1:
+        case 2:
+        case 3:
+        case 4:
+        case 5:
+        case 6:
+        case 7:
+        case 8:
+        case 9:
+            return (h + 48);
+        case 10:
+        case 11:
+        case 12:
+        case 13:
+        case 14:
+        case 15:
+            return (h + 55);
+        default:
+            return 0;
+    }
+}
+
+char *catn_alloc(size_t n, ...) {
+    va_list args;
+    size_t pos = 0;
+    char *buffer = NULL;
+    size_t buffer_size = 0;
+    va_start(args, n);
+
+    for(size_t i = 0; i < n; i++) {
+        char *str = va_arg(args, char *);
+        size_t copy_len = strlen(str) + (i + 1 == n ? 1 : 0);
+
+        char *tmp = realloc(buffer, buffer_size + copy_len);
+
+        if(tmp == NULL) {
+            break;
+        } else {
+            buffer = tmp;
+        }
+
+        buffer_size += copy_len;
+        memcpy(buffer + pos, str, copy_len);
+        pos += copy_len;
+    }
+
+    if(buffer != NULL) {
+        // ensure it's NUL terminated
+        buffer[buffer_size - 1] = '\0';
+    }
+
+    va_end(args);
+
+    return buffer;
+}
+
diff --git a/stringutil.h b/stringutil.h
new file mode 100644
index 0000000..ddfebfc
--- /dev/null
+++ b/stringutil.h
@@ -0,0 +1,2 @@
+char nibble_hex(short h);
+char *catn_alloc(size_t n, ...);
diff --git a/timeutil.c b/timeutil.c
index c695d54..c79b850 100644
--- a/timeutil.c
+++ b/timeutil.c
@@ -4,7 +4,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include "timeutil.h"
-#include "bitutil.h"
+#include "stringutil.h"
 
 #include <stdio.h>