From 14495e064beff7801ec837dbfc2d5a5d3161cf0c Mon Sep 17 00:00:00 2001 From: Ikey Doherty Date: Tue, 17 May 2016 17:03:34 +0100 Subject: [PATCH] Make use of autofree to simplify exit conditions Signed-off-by: Ikey Doherty --- src/prepare.c | 23 ++++++++--------------- src/server.c | 3 +-- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/prepare.c b/src/prepare.c index 004cd49..9fdd172 100644 --- a/src/prepare.c +++ b/src/prepare.c @@ -86,7 +86,8 @@ int tarcount; static void do_one_file(char *base1, char *base2, char *path, int isdir) { - char *fullpath1 = NULL, *fullpath2 = NULL, *dir, *dir2; + char *dir2 = NULL; + autofree(char) * fullpath1, *fullpath2, *dir = NULL; struct stat buf1, buf2; int ret; @@ -94,13 +95,12 @@ static void do_one_file(char *base1, char *base2, char *path, int isdir) return; } if (asprintf(&fullpath2, "%s%s.tar", base2, path) < 0) { - free(fullpath1); return; } ret = lstat(fullpath1, &buf1); if (ret) { - goto out; + return; } if (S_ISLNK(buf1.st_mode)) { @@ -125,7 +125,6 @@ static void do_one_file(char *base1, char *base2, char *path, int isdir) (void)nc_mkdir_p(dir2, 00755); } - free(dir); chdir(base1); if (!isdir && asprintf(&command, @@ -163,21 +162,14 @@ static void do_one_file(char *base1, char *base2, char *path, int isdir) free(command); } } - -out: - free(fullpath1); - free(fullpath2); } static void recurse_dir(char *base1, char *base2, char *path) { DIR *dir; - char *fullpath1, *fullpath2, *newpath; + char *fullpath1 = NULL; struct dirent *entry; - fullpath1 = NULL; - fullpath2 = NULL; - if (asprintf(&fullpath1, "%s%s", base1, path) < 0) { return; } @@ -185,10 +177,13 @@ static void recurse_dir(char *base1, char *base2, char *path) dir = opendir(fullpath1); if (!dir) { - free(fullpath1); return; } + while (1) { + autofree(char) *fullpath2 = NULL; + autofree(char) *newpath = NULL; + entry = readdir(dir); if (!entry) { break; @@ -215,11 +210,9 @@ static void recurse_dir(char *base1, char *base2, char *path) } else { do_one_file(base1, base2, newpath, 0); } - free(newpath); } } closedir(dir); - free(fullpath1); } int main(int argc, char **argv) diff --git a/src/server.c b/src/server.c index b2e335c..1942bcc 100644 --- a/src/server.c +++ b/src/server.c @@ -167,7 +167,7 @@ static int curl_get_file(const char *url, const char *prefix, time_t timestamp) } if (ret == 200) { - char *command = NULL; + autofree(char) *command = NULL; // printf("Filename is %s\n", filename); memset(&statbuf, 0, sizeof(statbuf)); @@ -178,7 +178,6 @@ static int curl_get_file(const char *url, const char *prefix, time_t timestamp) prefix, filename) >= 0) { system(command); - free(command); } } unlink(filename);