diff options
author | Anton L. Šijanec <anton@sijanec.eu> | 2020-04-29 22:38:41 +0200 |
---|---|---|
committer | Anton L. Šijanec <anton@sijanec.eu> | 2020-04-29 22:38:41 +0200 |
commit | 52a5a9d9bd8d767bd76c02f20668e1c7d92e33f6 (patch) | |
tree | bda89def263033aa7efc2f87e55b9c7add3013c0 /lib | |
parent | delam. (diff) | |
download | bverbose-52a5a9d9bd8d767bd76c02f20668e1c7d92e33f6.tar bverbose-52a5a9d9bd8d767bd76c02f20668e1c7d92e33f6.tar.gz bverbose-52a5a9d9bd8d767bd76c02f20668e1c7d92e33f6.tar.bz2 bverbose-52a5a9d9bd8d767bd76c02f20668e1c7d92e33f6.tar.lz bverbose-52a5a9d9bd8d767bd76c02f20668e1c7d92e33f6.tar.xz bverbose-52a5a9d9bd8d767bd76c02f20668e1c7d92e33f6.tar.zst bverbose-52a5a9d9bd8d767bd76c02f20668e1c7d92e33f6.zip |
Diffstat (limited to 'lib')
-rw-r--r-- | lib/jsmin.c | 6 | ||||
-rw-r--r-- | lib/mkdirp.c | 46 |
2 files changed, 50 insertions, 2 deletions
diff --git a/lib/jsmin.c b/lib/jsmin.c index f63e68f..402c6c9 100644 --- a/lib/jsmin.c +++ b/lib/jsmin.c @@ -32,7 +32,7 @@ is the option to read from a source file and output to the minified file. #include <stdlib.h> #include <stdio.h> - +#include <fopenmkdir.c> static int the_a; static int the_b; static int look_ahead = EOF; @@ -316,9 +316,11 @@ static void jsmin() { */ int minify_js(const char* source_js_filename, const char* minified_js_filename) { - minified_js_file = fopen(minified_js_filename, "w"); + minified_js_file = fopen_mkdir(minified_js_filename, "w"); source_js_file = fopen(source_js_filename, "r"); jsmin(); + fclose(minified_js_file); + fclose(source_js_file); return 0; } diff --git a/lib/mkdirp.c b/lib/mkdirp.c new file mode 100644 index 0000000..8c79cf4 --- /dev/null +++ b/lib/mkdirp.c @@ -0,0 +1,46 @@ +// borrowed from https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950 + +#include <string.h> +#include <limits.h> /* PATH_MAX */ +#include <sys/stat.h> /* mkdir(2) */ +#include <errno.h> +#ifndef PATH_MAX +#define PATH_MAX 255 +#endif +int mkdir_p(const char *path) { + /* Adapted from http://stackoverflow.com/a/2336245/119527 */ + const size_t len = strlen(path); + char _path[PATH_MAX]; + char *p; + + errno = 0; + + /* Copy string so its mutable */ + if (len > sizeof(_path)-1) { + errno = ENAMETOOLONG; + return -1; + } + strcpy(_path, path); + + /* Iterate the string */ + for (p = _path + 1; *p; p++) { + if (*p == '/') { + /* Temporarily truncate */ + *p = '\0'; + + if (mkdir(_path, S_IRWXU) != 0) { + if (errno != EEXIST) + return -1; + } + + *p = '/'; + } + } + + if (mkdir(_path, S_IRWXU) != 0) { + if (errno != EEXIST) + return -1; + } + + return 0; +} |