commit 4da4a5f92e802fd16f16433713789734dd7431da
parent c4bcb9123cfa7c8cdaa69e87c37ce67fc8cab59b
Author: Paco Esteban <paco@e1e0.net>
Date: Wed, 1 Jul 2020 11:30:44 +0200
better initialize and free article struct
Diffstat:
M | ssnail.c | | | 74 | ++++++++++++++++++++++++++++++++++++++++++++++---------------------------- |
1 file changed, 46 insertions(+), 28 deletions(-)
diff --git a/ssnail.c b/ssnail.c
@@ -55,8 +55,10 @@ struct article {
LIST_HEAD(listhead, article) head;
__dead static void usage(void);
+static void free_article(struct article *);
static void free_articleq(struct listhead *);
+struct article *init_article(void);
static struct article *populate_article_entry(char *, char *);
static const struct ssnail_error *process_dir(char *, char *, int);
@@ -163,18 +165,24 @@ free_articleq(struct listhead *h)
while (!LIST_EMPTY(h)) {
a = LIST_FIRST(h);
LIST_REMOVE(a, entries);
- free(a->src_path);
- free(a->dst_path);
- free(a->orig_content);
- free(a->html_content);
- free(a->title);
- free(a->author);
- free(a->date);
- free(a->type);
- free(a);
+ free_article(a);
}
}
+static void
+free_article(struct article *a)
+{
+ free(a->src_path);
+ free(a->dst_path);
+ free(a->orig_content);
+ free(a->html_content);
+ free(a->title);
+ free(a->author);
+ free(a->date);
+ free(a->type);
+ free(a);
+}
+
static const struct ssnail_error *
gen_html(struct article *a)
{
@@ -320,13 +328,12 @@ out:
static struct article *
populate_article_entry(char *src_path, char *dst_path)
{
- struct article *ap = malloc(sizeof(struct article));
+ struct article *ap = init_article();
struct stat st;
int len = 0;
- if (ap == NULL) {
- goto error;
- }
+ if (ap == NULL)
+ return NULL;
ap->src_path = strndup(src_path, PATH_MAX);
if (ap->src_path == NULL)
@@ -336,6 +343,7 @@ populate_article_entry(char *src_path, char *dst_path)
fbuf = str_rep(dst_path, ".md", ".html");
ap->dst_path = strndup(fbuf, PATH_MAX);
+ free(fbuf);
if (ap->dst_path == NULL)
goto error;
@@ -356,23 +364,10 @@ populate_article_entry(char *src_path, char *dst_path)
if (gen_html(ap) != 0)
goto error;
-out:
- free(fbuf);
- fbuf = NULL;
+
return ap;
error:
- free(fbuf);
- fbuf = NULL;
- if (ap->src_path != NULL)
- free(ap->src_path);
- if (ap->dst_path != NULL)
- free(ap->dst_path);
- /* if (ap->orig_content != NULL) */
- /* free(ap->orig_content); */
- /* if (ap->html_content != NULL) */
- /* free(ap->html_content); */
- if (ap != NULL)
- free(ap);
+ free_article(ap);
return NULL;
}
@@ -469,3 +464,26 @@ sort_articleq(struct listhead *h)
return NULL;
}
+
+struct article *
+init_article(void)
+{
+ struct article *a = malloc(sizeof(struct article));
+
+ if (a) {
+ a->src_mtime = 0;
+ a->dst_mtime = 0;
+ a->htmlz = 0;
+ a->origz = 0;
+ a->src_path = NULL;
+ a->dst_path = NULL;
+ a->orig_content = NULL;
+ a->html_content = NULL;
+ a->title = NULL;
+ a->author = NULL;
+ a->date = NULL;
+ a->type = NULL;
+ }
+
+ return a;
+}