diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2020-05-26 13:53:44 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2020-05-26 13:53:44 -0400 |
commit | 9bc167d23a3f738057185dc8a6412132c7c47c3e (patch) | |
tree | ea4ec66c8c5913c8bfda717dc5c7acb8053d0f10 /lib | |
parent | c34dc1f1dfe74ded5b71bb0bb10f61f3610bc987 (diff) | |
download | spmc-9bc167d23a3f738057185dc8a6412132c7c47c3e.tar.gz |
Errors set return value and jump to end of function
Diffstat (limited to 'lib')
-rw-r--r-- | lib/strlist.c | 66 |
1 files changed, 35 insertions, 31 deletions
diff --git a/lib/strlist.c b/lib/strlist.c index 0de0d2a..f90cce4 100644 --- a/lib/strlist.c +++ b/lib/strlist.c @@ -58,15 +58,15 @@ static int reader_strlist_append_file(size_t lineno, char **line) { * @param pStrList * @param path file path or HTTP/s address * @param readerFn pointer to a reader function (use NULL to retrieve all data) - * @return 0=success -1=error (spmerrno set) + * @return 0=success 1=no data, -1=error (spmerrno set) */ int strlist_append_file(StrList *pStrList, char *_path, ReaderFn *readerFn) { + int retval = 0; int is_remote = 0; char *path = NULL; char *filename = NULL; char *from_file_tmpdir = NULL; - char **from_file = NULL; - char *fetched = NULL; + char **data = NULL; if (readerFn == NULL) { readerFn = reader_strlist_append_file; @@ -75,26 +75,21 @@ int strlist_append_file(StrList *pStrList, char *_path, ReaderFn *readerFn) { path = strdup(_path); if (path == NULL) { spmerrno = errno; - return -1; - } - - filename = calloc(PATH_MAX, sizeof(char)); - if (filename == NULL) { - spmerrno = errno; - return -1; + retval = -1; + goto fatal; } - filename = expandpath(path); - is_remote = (startswith(path, "http") || startswith(path, "https")); if (is_remote) { long response = 0; + char *fetched = NULL; from_file_tmpdir = spm_mkdtemp(TMP_DIR, __FUNCTION__, NULL); if (from_file_tmpdir == NULL) { spmerrno = errno; spmerrno_cause("file_from temp directory"); - return -1; + retval = -1; + goto fatal; } fetched = join((char *[]){from_file_tmpdir, basename(path), NULL}, DIRSEPS); @@ -103,38 +98,47 @@ int strlist_append_file(StrList *pStrList, char *_path, ReaderFn *readerFn) { char cause[PATH_MAX]; sprintf(cause, "HTTP(%ld): %s: %s", response, http_response_str(response), path); spmerrno_cause(cause); - return -1; + retval = -1; + goto fatal; } - strncpy(filename, fetched, PATH_MAX - 1); + filename = strdup(fetched); + free(fetched); + } else { + filename = expandpath(path); } - if (exists(filename) != 0) { + if (filename == NULL) { spmerrno = errno; - spmerrno_cause(filename); - return -1; + retval = -1; + goto fatal; } - from_file = file_readlines(filename, 0, 0, readerFn); - if (from_file == NULL) { - spmerrno = errno; - spmerrno_cause(filename); - return -1; + data = file_readlines(filename, 0, 0, readerFn); + if (data == NULL) { + retval = 1; + goto fatal; } - for (size_t record = 0; from_file[record] != NULL; record++) { - strlist_append(pStrList, from_file[record]); - free(from_file[record]); + for (size_t record = 0; data[record] != NULL; record++) { + strlist_append(pStrList, data[record]); + free(data[record]); } - free(from_file); + free(data); - if (from_file_tmpdir) { +fatal: + if (from_file_tmpdir != NULL) { rmdirs(from_file_tmpdir); + free(from_file_tmpdir); + } + if (filename != NULL) { + free(filename); + } + if (path != NULL) { + free(path); } - free(from_file_tmpdir); - free(filename); - return 0; + return retval; } /** |