cld-ssg

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

commit 04c5688e9252806a8990b180593e7c940b9ec0eb
parent e21de81a0fe02447e26a3e641b6c4252cbefdb6e
Author: Christopher Ray Langford <chris@hellocld.com>
Date:   Sat, 28 Sep 2019 14:07:08 -0400

Fixed directory generation. Moved it out of main.c

To save on sanity, I moved some of the more generic, possibly reusable
functions to their own util source files.

Diffstat:
MMakefile | 7+++++--
Mmain.c | 31++++---------------------------
Autil.c | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Autil.h | 17+++++++++++++++++
Autil.o | 0
5 files changed, 95 insertions(+), 29 deletions(-)

diff --git a/Makefile b/Makefile @@ -2,12 +2,15 @@ CC = gcc CFLAGS = -g -Wall -pedantic CLIBS = -lcmark -default: dirs main.c config.h - $(CC) $(CFLAGS) main.c -o ./bin/cld-ssg $(CLIBS) +default: dirs main.c config.h util + $(CC) $(CFLAGS) main.c ./bin/util.o -o ./bin/cld-ssg $(CLIBS) dirs: if [ ! -d ./bin ] ; then mkdir bin ; fi +util: util.h util.c + $(CC) $(CFLAGS) util.c -c -o ./bin/util.o + clean: if [ -d ./bin ] ; then rm -rf ./bin/* ; fi diff --git a/main.c b/main.c @@ -8,10 +8,11 @@ #include <sys/stat.h> #include <errno.h> -#include "config.h" - +#include "util.h" #include "cmark.h" +#include "config.h" + int md_filter(const struct dirent *); struct post { @@ -35,10 +36,6 @@ int write_archive(struct post *posts, int totalPosts); int write_rss(struct post *posts, int totalPosts); int write_post(struct post *post); -int create_directory(const char *path); - -char *read_text(const char *path, int maxLength); - char buf[MAX_POST_CHARS]; int main() @@ -57,7 +54,7 @@ int main() /* Write archive.html */ /* Copy images/videos/audio/static pages */ - + printf("*** read_file testing ***\n\n"); struct post *tp = create_post("2019-09-24-20-24-real-test.md"); @@ -183,24 +180,4 @@ int write_post(struct post *post) } -/* Reads a text file into a char* */ -char *read_text(const char *path, int maxLength) -{ - char *o = malloc(maxLength); - FILE *f = fopen(path, "r"); - int c; - char *t = o; - while((c = fgetc(f)) != EOF && --maxLength > 0) - *(t++) = (char)c; - *t = '\0'; - fclose(f); - return o; -} - -/* Creates a directory structure based on the path provided */ -int create_directory(const char *path) -{ - mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - -} diff --git a/util.c b/util.c @@ -0,0 +1,69 @@ +#include <stdlib.h> +#include <sys/stat.h> +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include "util.h" + +#define MAX_CHARS 512 + +char buf[MAX_CHARS]; + +/* Creates a directory structure based on the path provided */ +int create_directory(const char *path) +{ + char c; + memcpy(buf, path, MAX_CHARS); + + char *p = buf; + + while((c = *(++p))) + if(c == '/') { + *p = '\0'; + if(mkdir(buf, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) + if(dir_error(errno, buf) < 0) return -1; + *p = '/'; + } + if(mkdir(buf, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) + if(dir_error(errno, buf) < 0) return -1; + return 0; +} + +/* Error checking for create_directory */ +int dir_error(int status, const char* dir) +{ + switch(status) { + case 0: + case EEXIST: + break; + case EACCES: + printf("create_directory error: %s permission denied\n", dir); + return -1; + break; + case ENAMETOOLONG: + printf("create_directory error: path %s too long\n", dir); + return -1; + break; + case ENOTDIR: + printf("create_directory error: non-directory %s exists\n", dir); + return -1; + break; + } + return 0; +} + +/* Reads a text file into a char* */ +char *read_text(const char *path, int maxLength) +{ + char *o = malloc(maxLength); + FILE *f = fopen(path, "r"); + int c; + char *t = o; + while((c = fgetc(f)) != EOF && --maxLength > 0) + *(t++) = (char)c; + *t = '\0'; + fclose(f); + return o; +} + + diff --git a/util.h b/util.h @@ -0,0 +1,17 @@ +/* + * Some utility functions I might use in future projects + */ + +#ifndef UTIL_H +#define UTIL_H + +/* Creates a directory structure based on the path provided */ +int create_directory(const char *); + +/* Error checking for create_directory */ +int dir_error(int, const char *); + +/* Reads a text file into a char* */ +char *read_text(const char *, int); + +#endif diff --git a/util.o b/util.o Binary files differ.