aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2016-03-19 10:36:16 +0100
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2016-03-19 10:36:16 +0100
commitc7b4bc4d64a3ce669e54b7c7dae87527d9ee3123 (patch)
treee5f5ccf4d0d177cb8dd55b6f8ad026c18a2310a9
parent37592b57b31987519e59117d49bccd0207e67f15 (diff)
downloadblogc-c7b4bc4d64a3ce669e54b7c7dae87527d9ee3123.tar.gz
blogc-c7b4bc4d64a3ce669e54b7c7dae87527d9ee3123.tar.bz2
blogc-c7b4bc4d64a3ce669e54b7c7dae87527d9ee3123.zip
main: loader: use file utils from squareball
-rw-r--r--configure.ac2
m---------squareball0
-rw-r--r--src/file.c26
-rw-r--r--src/main.c52
4 files changed, 18 insertions, 62 deletions
diff --git a/configure.ac b/configure.ac
index ad759c6..d217c90 100644
--- a/configure.ac
+++ b/configure.ac
@@ -119,7 +119,7 @@ AS_IF([test "x$have_cmocka" = "xyes"], , [
])
AM_CONDITIONAL([USE_CMOCKA], [test "x$have_cmocka" = "xyes"])
-AC_CHECK_HEADERS([sys/types.h sys/stat.h time.h])
+AC_CHECK_HEADERS([sys/types.h sys/stat.h time.h libgen.h])
LT_LIB_M
diff --git a/squareball b/squareball
-Subproject ebc02478a78c83e5e62afc485a294e1c78442db
+Subproject 10c2a010e3a7058c1519d5fb8c377ca0dd0c4ce
diff --git a/src/file.c b/src/file.c
index 972f433..a12fbc2 100644
--- a/src/file.c
+++ b/src/file.c
@@ -10,10 +10,8 @@
#include <config.h>
#endif /* HAVE_CONFIG_H */
-#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
-#include <string.h>
#include <squareball.h>
#include "file.h"
#include "error.h"
@@ -25,29 +23,7 @@
char*
blogc_file_get_contents(const char *path, size_t *len, sb_error_t **err)
{
- if (path == NULL || err == NULL || *err != NULL)
- return NULL;
-
- *len = 0;
- FILE *fp = fopen(path, "r");
-
- if (fp == NULL) {
- int tmp_errno = errno;
- *err = sb_error_new_printf(BLOGC_ERROR_LOADER,
- "Failed to open file (%s): %s", path, strerror(tmp_errno));
- return NULL;
- }
-
- sb_string_t *str = sb_string_new();
- char buffer[BLOGC_FILE_CHUNK_SIZE];
-
- while (!feof(fp)) {
- size_t read_len = fread(buffer, sizeof(char), BLOGC_FILE_CHUNK_SIZE, fp);
- *len += read_len;
- sb_string_append_len(str, buffer, read_len);
- }
- fclose(fp);
- return sb_string_free(str, false);
+ return sb_file_get_contents(path, len, err);
}
diff --git a/src/main.c b/src/main.c
index 709b37e..1f5c76c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -18,6 +18,10 @@
#include <sys/types.h>
#endif /* HAVE_SYS_TYPES_H */
+#ifdef HAVE_LIBGEN_H
+#include <libgen.h>
+#endif /* HAVE_LIBGEN_H */
+
#include <errno.h>
#include <locale.h>
#include <stdbool.h>
@@ -68,41 +72,6 @@ blogc_print_usage(void)
}
-static void
-blogc_mkdir_recursive(const char *filename)
-{
- char *fname = sb_strdup(filename);
- for (char *tmp = fname; *tmp != '\0'; tmp++) {
- if (*tmp != '/' && *tmp != '\\')
- continue;
-#if defined(HAVE_SYS_STAT_H) && defined(HAVE_SYS_TYPES_H)
- char bkp = *tmp;
- *tmp = '\0';
- if ((strlen(fname) > 0) &&
-#if defined(WIN32) || defined(_WIN32)
- (-1 == mkdir(fname)) &&
-#else
- (-1 == mkdir(fname, 0777)) &&
-#endif
- (errno != EEXIST))
- {
- fprintf(stderr, "blogc: error: failed to create output "
- "directory (%s): %s\n", fname, strerror(errno));
- free(fname);
- exit(2);
- }
- *tmp = bkp;
-#else
- // FIXME: show this warning only if actually trying to create a directory.
- fprintf(stderr, "blogc: warning: can't create output directories "
- "for your platform. please create the directories yourself.\n");
- break;
-#endif
- }
- free(fname);
-}
-
-
int
main(int argc, char **argv)
{
@@ -252,7 +221,18 @@ main(int argc, char **argv)
FILE *fp = stdout;
if (!write_to_stdout) {
- blogc_mkdir_recursive(output);
+
+#ifdef HAVE_LIBGEN_H
+ char *_output = sb_strdup(output);
+ sb_mkdir_recursive(dirname(_output), &err);
+ free(_output);
+ if (err != NULL) {
+ blogc_error_print(err);
+ rv = 2;
+ goto cleanup4;
+ }
+#endif
+
fp = fopen(output, "w");
if (fp == NULL) {
fprintf(stderr, "blogc: error: failed to open output file (%s): %s\n",