diff --git a/dirl.c b/dirl.c
index 711b3c8..900ecd0 100644
--- a/dirl.c
+++ b/dirl.c
@@ -31,59 +31,10 @@ suffix(int t)
return "";
}
-static char*
-dirl_read(char* tpl)
-{
- FILE* tpl_fp;
-
- if (!(tpl_fp = fopen(tpl, "r"))) {
- return NULL;
- }
-
- /* Get size of template */
- if (fseek(tpl_fp, 0L, SEEK_END) < 0) {
- fclose(tpl_fp);
- return NULL;
- }
-
- long tpl_size;
- if ((tpl_size = ftell(tpl_fp)) < 0) {
- fclose(tpl_fp);
- return NULL;
- }
-
- rewind(tpl_fp);
-
- /* Read template into tpl_buf */
- char* tpl_buf = (char*)calloc(sizeof(char), tpl_size);
-
- if (tpl_buf == NULL) {
- fclose(tpl_fp);
- free(tpl_buf);
- return NULL;
- }
-
- if (fread(tpl_buf, 1, tpl_size, tpl_fp) < tpl_size) {
- if (feof(tpl_fp)) {
- warn("Reached end of template %s prematurely", tpl);
- } else if (ferror(tpl_fp)) {
- warn("Error while reading template %s", tpl);
- }
-
- fclose(tpl_fp);
- free(tpl_buf);
- clearerr(tpl_fp);
-
- return NULL;
- }
-
- return tpl_buf;
-}
-
/* Try to find templates up until root
*
* Iterates the directory hierarchy upwards. Returns the closest path containing
- * a template file or NULL if none could be found.
+ * one of the template files or NULL if none could be found.
*
* Note that we are chrooted.
*/
@@ -115,52 +66,47 @@ dirl_find_templ_dir(const char* start_path)
return NULL;
}
+/* Helper function to fill template from base+name if file exists */
+static void
+dirl_fill_templ(char** templ, char* base, char* name, char* def)
+{
+ char* path = calloc(sizeof(char), strlen(base) + strlen(name));
+ strcpy(path, base);
+ strcat(path, name);
+
+ char* file_buf = read_file(path);
+ free(path);
+
+ if (file_buf) {
+ *templ = file_buf;
+ } else {
+ *templ = def;
+ }
+}
+
struct dirl_templ
dirl_read_templ(const char* path)
{
- struct dirl_templ templates = { .header = DIRL_HEADER_DEFAULT,
- .entry = DIRL_ENTRY_DEFAULT,
- .footer = DIRL_FOOTER_DEFAULT };
+ struct dirl_templ templ;
char* templ_dir = dirl_find_templ_dir(path);
- char* header_file =
- calloc(sizeof(char), strlen(templ_dir) + strlen(DIRL_HEADER));
- strcpy(header_file, templ_dir);
- strcat(header_file, DIRL_HEADER);
- char* header = dirl_read(header_file);
- if (header)
- templates.header = header;
- free(header_file);
-
- char* entry_file =
- calloc(sizeof(char), strlen(templ_dir) + strlen(DIRL_ENTRY));
- strcpy(entry_file, templ_dir);
- strcat(entry_file, DIRL_ENTRY);
- char* entry = dirl_read(entry_file);
- if (entry)
- templates.entry = entry;
- free(entry_file);
-
- char* footer_file =
- calloc(sizeof(char), strlen(templ_dir) + strlen(DIRL_FOOTER));
- strcpy(footer_file, templ_dir);
- strcat(footer_file, DIRL_FOOTER);
- char* footer = dirl_read(footer_file);
- if (footer)
- templates.footer = footer;
- free(footer_file);
+ dirl_fill_templ(&templ.header, templ_dir, DIRL_HEADER, DIRL_HEADER_DEFAULT);
+ dirl_fill_templ(&templ.entry, templ_dir, DIRL_ENTRY, DIRL_ENTRY_DEFAULT);
+ dirl_fill_templ(&templ.footer, templ_dir, DIRL_FOOTER, DIRL_FOOTER_DEFAULT);
free(templ_dir);
- return templates;
+ return templ;
}
enum status
dirl_header(int fd, const struct response* res, const struct dirl_templ* templ)
{
/* Replace placeholder */
- char* nhead = replace(templ->header, "{curdir}", "something");
+ char* nhead = calloc(sizeof(char), strlen(templ->header));
+ memcpy(nhead, templ->header, strlen(templ->header));
+ replace(&nhead, "{curdir}", res->path);
/* Write header */
write(fd, nhead, strlen(nhead));
@@ -175,7 +121,10 @@ enum status
dirl_entry(int fd, const struct dirent* entry, const struct dirl_templ* templ)
{
/* Replace placeholder */
- char* nentry = replace(templ->entry, "{entry}", entry->d_name);
+ char* nentry = calloc(sizeof(char), strlen(templ->entry));
+ memcpy(nentry, templ->entry, strlen(templ->entry));
+ replace(&nentry, "{entry}", entry->d_name);
+ replace(&nentry, "{suffix}", suffix(entry->d_type));
/* Write entry */
write(fd, nentry, strlen(nentry));
@@ -189,7 +138,9 @@ enum status
dirl_footer(int fd, const struct dirl_templ* templ)
{
/* Replace placeholder */
- char* nfoot = replace(templ->footer, "{idx}", "something");
+ char* nfoot = calloc(sizeof(char), strlen(templ->footer));
+ memcpy(nfoot, templ->footer, strlen(templ->footer));
+ replace(&nfoot, "{idx}", "something");
/* Write footer */
write(fd, nfoot, strlen(nfoot));
diff --git a/dirl.h b/dirl.h
index 5b6478c..b374c01 100644
--- a/dirl.h
+++ b/dirl.h
@@ -30,7 +30,7 @@
#define DIRL_ENTRY_DEFAULT \
"
\n" \
- " {entry}\n"
+ " {entry}{suffix}\n"
#define DIRL_FOOTER_DEFAULT \
"