cld-ssg

The static site generator (to be) used for hellocld.com
git clone git://git.hellocld.com/cld-ssg
Log | Files | Refs

commit 2669e12ff3fb871250824b0c9e35d816fd1bb6ec
parent 0bdcae19f6b76c5bc4779b4f67bb0988adb36061
Author: Christopher Ray Langford <chris@hellocld.com>
Date:   Mon, 30 Sep 2019 16:45:26 -0400

File copying is working!

Diffstat:
Mconfig.h | 2++
Afiles/images/doge.png | 0
Afiles/test.txt | 1+
Mmain.c | 57++++++++++++++++++++++++++++++++++++++++++++++++++++-----
Mutil.c | 31++++++++++++++++++++++++++++++-
Mutil.h | 3+++
6 files changed, 88 insertions(+), 6 deletions(-)

diff --git a/config.h b/config.h @@ -9,6 +9,8 @@ #define HTMLDIR "testhtml/" /* Directory where .md posts are saved */ #define POSTDIR "postdir/" +/* Directory containing anything you just need copied to HTMLDIR */ +#define RESOURCEDIR "files/" /* Header and footer files */ #define HEADER_HTML "assets/header.html" #define FOOTER_HTML "assets/footer.html" diff --git a/files/images/doge.png b/files/images/doge.png Binary files differ. diff --git a/files/test.txt b/files/test.txt @@ -0,0 +1 @@ +test file diff --git a/main.c b/main.c @@ -6,6 +6,8 @@ #include <stdio.h> #include <string.h> #include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> #include <errno.h> #include "util.h" @@ -35,6 +37,10 @@ int write_archive(struct post *posts[], int totalPosts); int write_rss(struct post *posts, int totalPosts); int write_post(struct post *post); +void copy_resources(char *); + +void errprintf(const char *, int); + char buf[MAX_POST_CHARS]; char *header; char *footer; @@ -69,7 +75,7 @@ int main() write_archive(posts, t_postcount); /* Copy images/videos/audio/static pages */ - + copy_resources(RESOURCEDIR); /* free up all the posts on the heap */ i = t_postcount; @@ -206,17 +212,25 @@ int write_index(struct post *posts[], int totalPosts) sprintf(buf, "%s%s", HTMLDIR, "index.html"); FILE *f = fopen(buf, "w"); if(f == NULL) { - printf("write_index ERROR: %d\n", errno); + errprintf("write_index", errno); return -1; } if(fprintf(f, header) < 0) { - printf("write_index ERROR: %d\n", errno); + errprintf("write_index", errno); return -1; } int c = 0; while(totalPosts-- > 0 && c++ < INDEX_POSTS) - fprintf(f, posts[totalPosts]->content); - fprintf(f, footer); + if(fprintf(f, posts[totalPosts]->content) < 0) { + errprintf("write_index", errno); + fclose(f); + return -1; + } + if(fprintf(f, footer) < 0) { + errprintf("write_index", errno); + fclose(f); + return -1; + } fclose(f); return 0; } @@ -238,3 +252,36 @@ int write_archive(struct post *posts[], int totalPosts) return 0; } +void errprintf(const char *function, int error) +{ + printf("%s ERROR: %d\n", function, error); +} + +void copy_resources(char *dir) +{ + char t_dest[MAX_URL_CHARS]; + struct stat t_stat; + /* find all files in resource directory */ + struct dirent **t_files; + int t_count = scandir(dir, &t_files, NULL, NULL); + while(t_count-- > 0) { + if(t_files[t_count]->d_name[0] == '.') + continue; + printf("Found %s%s\n", dir, t_files[t_count]->d_name); + sprintf(buf, "%s%s", dir, t_files[t_count]->d_name); + stat(buf, &t_stat); + if(S_ISDIR(t_stat.st_mode)) { + sprintf(buf, "%s%s%s", HTMLDIR, dir + strlen(RESOURCEDIR), t_files[t_count]->d_name); + printf("DEBUG: creating directory %s\n", buf); + create_directory(buf); + sprintf(buf, "%s%s/", dir, t_files[t_count]->d_name); + copy_resources(buf); + } + if(S_ISREG(t_stat.st_mode)) { + sprintf(t_dest, "%s%s", HTMLDIR, buf + strlen(RESOURCEDIR)); + printf("DEBUG: copying %s to %s\n", buf, t_dest); + copy_file(buf, t_dest); + } + } + +} diff --git a/util.c b/util.c @@ -66,4 +66,33 @@ char *read_text(const char *path, int maxLength) return o; } - +/* Copies a file from source to destination */ +int copy_file(const char *source, const char *dest) +{ + FILE *s = fopen(source, "r"); + if(s == NULL) { + printf("copy_file ERROR: Failed to open %s: %d\n", + source, + errno); + return -1; + } + FILE *d = fopen(dest, "w"); + if(s == NULL) { + printf("copy_file ERROR: Failed to open %s: %d\n", + dest, + errno); + return -1; + } + int c = fgetc(s); + while(c != EOF) { + if(fputc(c, d) == EOF) { + printf("copy_file ERROR: fputc error: %d", + errno); + return -1; + } + c = fgetc(s); + } + fclose(s); + fclose(d); + return 0; +} diff --git a/util.h b/util.h @@ -14,4 +14,7 @@ int dir_error(int, const char *); /* Reads a text file into a char* */ char *read_text(const char *, int); +/* Copies a file from one location to another */ +int copy_file(const char *, const char *); + #endif