commit 4c4faabeeecdbdbeb9af31fe1d755a43f0c55243
parent 5e52c764c498d83c9a2433dfd9feac6951f31475
Author: Paco Esteban <paco@e1e0.net>
Date: Tue, 12 May 2020 20:21:32 +0200
better (hopefully) handling and allocation of metadata
Diffstat:
M | ssnail.c | | | 33 | ++++++++++++++++++++------------- |
1 file changed, 20 insertions(+), 13 deletions(-)
diff --git a/ssnail.c b/ssnail.c
@@ -37,16 +37,16 @@ struct article {
size_t origz;
char *html_content;
size_t htmlz;
- char title[MAX_META];
- char author[MAX_META];
- char date[MAX_META];
+ char *title;
+ char *author;
+ char *date;
long long src_mtime;
long long dst_mtime;
SLIST_ENTRY(article) entries;
};
char *str_rep(const char *, const char *, const char *);
-int gen_html(char **, size_t *, char *, char *, char *, char *, size_t);
+int gen_html(char **, size_t *, char **, char **, char **, char *, size_t);
int build_full_path(char *, char *, char *);
int load_from_file(char **, char *);
void usage();
@@ -176,7 +176,7 @@ str_rep(const char *s, const char *oldW, const char *newW)
int
gen_html(char **dest, size_t *destz,
- char title[MAX_META], char author[MAX_META], char date[MAX_META],
+ char **title, char **author, char **date,
char *orig, size_t origz)
{
char *head_tpl = NULL, *foot_tpl = NULL,
@@ -205,15 +205,22 @@ gen_html(char **dest, size_t *destz,
LOWDOWN_HTML_SKIP_HTML;
lowdown_buf(&opts, orig, origz, dest, destz, &mq);
- /* printf("%s\n%zd\n", dest, *destz); */
TAILQ_FOREACH(md, &mq, entries) {
- if (strcmp(md->key, "title") == 0 )
- (void)strlcpy(title, md->value, MAX_META);
- if (strcmp(md->key, "author") == 0 )
- (void)strlcpy(author, md->value, MAX_META);
- if (strcmp(md->key, "date") == 0 )
- (void)strlcpy(date, md->value, MAX_META);
+ size_t valz = (strlen(md->value) > MAX_META)
+ ? MAX_META : strlen(md->value) + 1;
+ if (strcmp(md->key, "title") == 0 ) {
+ *title = malloc(valz);
+ (void)strlcpy(*title, md->value, valz);
+ }
+ if (strcmp(md->key, "author") == 0 ) {
+ *author = malloc(valz);
+ (void)strlcpy(*author, md->value, valz);
+ }
+ if (strcmp(md->key, "date") == 0 ) {
+ *date = malloc(valz);
+ (void)strlcpy(*date, md->value, valz);
+ }
}
/* if (strlen(title) == 0 || strlen(author) == 0 || strlen(date) == 0) */
@@ -269,7 +276,7 @@ populate_article_entry(struct article *ap)
goto out;
gen_html(&ap->html_content, &ap->htmlz,
- ap->title, ap->author, ap->date,
+ &ap->title, &ap->author, &ap->date,
ap->orig_content, ap->origz);
retval = 0;
out: