diff options
author | Dmitrij D. Czarkoff <czarkoff@gmail.com> | 2016-05-01 14:40:54 +0200 |
---|---|---|
committer | Dmitrij D. Czarkoff <czarkoff@gmail.com> | 2016-05-01 14:40:54 +0200 |
commit | 0e1d18a2d0f48243fc4e0d40515bb0f224f618f0 (patch) | |
tree | cb984b95afc544bb5a457c6d5799c6aacf7125af | |
parent | aaab6d54a4cee653290d9cca59811e3f14217b7c (diff) | |
download | imv-0e1d18a2d0f48243fc4e0d40515bb0f224f618f0.tar.gz |
Make imv_navigator_add() return an integer value
In case of error return non-zero value instead of cleaning up navigator. This
way main loop may do some more cleanup, or maybe even act differently.
-rw-r--r-- | src/main.c | 14 | ||||
-rw-r--r-- | src/navigator.c | 30 | ||||
-rw-r--r-- | src/navigator.h | 5 | ||||
-rw-r--r-- | test/navigator.c | 16 |
4 files changed, 40 insertions, 25 deletions
@@ -198,7 +198,10 @@ int main(int argc, char** argv) buf[--len] = 0; } if(len > 0) { - imv_navigator_add(&nav, buf, g_options.recursive); + if(imv_navigator_add(&nav, buf, g_options.recursive) != 0) { + imv_navigator_destroy(&nav); + exit(1); + } break; } } @@ -230,7 +233,10 @@ int main(int argc, char** argv) errno = 0; /* clear errno */ } /* add the given path to the list to load */ - imv_navigator_add(&nav, argv[i], g_options.recursive); + if(imv_navigator_add(&nav, argv[i], g_options.recursive) != 0) { + imv_navigator_destroy(&nav); + exit(1); + } } /* if we weren't given any paths we have nothing to view. exit */ @@ -639,7 +645,9 @@ int main(int argc, char** argv) buf[--len] = 0; } if(len > 0) { - imv_navigator_add(&nav, buf, g_options.recursive); + if(imv_navigator_add(&nav, buf, g_options.recursive)) { + break; + } need_redraw = 1; } } diff --git a/src/navigator.c b/src/navigator.c index ebf69b2..97772cd 100644 --- a/src/navigator.c +++ b/src/navigator.c @@ -48,7 +48,7 @@ void imv_navigator_destroy(struct imv_navigator *nav) memset(nav, 0, sizeof(struct imv_navigator)); } -static void add_item(struct imv_navigator *nav, const char *path, +static int add_item(struct imv_navigator *nav, const char *path, time_t mtime) { if(nav->num_paths % BUFFER_SIZE == 0) { @@ -58,23 +58,24 @@ static void add_item(struct imv_navigator *nav, const char *path, new_paths = realloc(nav->paths, sizeof(char*) * new_size); new_mtimes = realloc(nav->mtimes, sizeof(time_t) * new_size); if (new_paths == NULL || new_mtimes == NULL) { - perror("add_item"); - free(nav->paths); - free(nav->mtimes); - exit(1); + return 1; } nav->paths = new_paths; nav->mtimes = new_mtimes; } - nav->paths[nav->num_paths] = strdup(path); + if((nav->paths[nav->num_paths] = strndup(path, PATH_MAX)) == NULL) { + return 1; + } nav->mtimes[nav->num_paths] = mtime; nav->num_paths += 1; if(nav->num_paths == 1) { nav->changed = 1; } + + return 0; } -void imv_navigator_add(struct imv_navigator *nav, const char *path, +int imv_navigator_add(struct imv_navigator *nav, const char *path, int recursive) { char path_buf[PATH_MAX]; @@ -85,23 +86,28 @@ void imv_navigator_add(struct imv_navigator *nav, const char *path, if(d) { struct dirent *dir; while((dir = readdir(d)) != NULL) { - if(strcmp(dir->d_name, "..") == 0 || - strcmp(dir->d_name, ".") == 0) { + if(strcmp(dir->d_name, "..") == 0 || strcmp(dir->d_name, ".") == 0) { continue; } snprintf(path_buf, sizeof(path_buf), "%s/%s", path, dir->d_name); if(recursive) { - imv_navigator_add(nav, path_buf, recursive); + if(imv_navigator_add(nav, path_buf, recursive) != 0) { + return 1; + } } else { stat(path_buf, &path_info); - add_item(nav, path_buf, path_info.st_mtim.tv_sec); + if(add_item(nav, path_buf, path_info.st_mtim.tv_sec) != 0) { + return 1; + } } } closedir(d); } } else { - add_item(nav, path, path_info.st_mtim.tv_sec); + return add_item(nav, path, path_info.st_mtim.tv_sec); } + + return 0; } const char *imv_navigator_selection(struct imv_navigator *nav) diff --git a/src/navigator.h b/src/navigator.h index 5933aa4..ac3cc59 100644 --- a/src/navigator.h +++ b/src/navigator.h @@ -41,8 +41,9 @@ void imv_navigator_destroy(struct imv_navigator *nav); /* Adds the given path to the navigator's internal list. * If a directory is given, all files within that directory are added. * An internal copy of path is made. - * If recursive is non-zero then subdirectories are recursed into. */ -void imv_navigator_add(struct imv_navigator *nav, const char *path, + * If recursive is non-zero then subdirectories are recursed into. + * Non-zero return code denotes failure. */ +int imv_navigator_add(struct imv_navigator *nav, const char *path, int recursive); /* Returns a read-only reference to the current path. The pointer is only diff --git a/test/navigator.c b/test/navigator.c index 2a8a7bb..55ab392 100644 --- a/test/navigator.c +++ b/test/navigator.c @@ -25,13 +25,13 @@ static void test_navigator_add_remove(void **state) /* Check poll_changed */ assert_false(imv_navigator_poll_changed(&nav, 0)); - /* Add 3 paths */ - imv_navigator_add(&nav, FILENAME1, 0); - imv_navigator_add(&nav, FILENAME2, 0); - imv_navigator_add(&nav, FILENAME3, 0); - imv_navigator_add(&nav, FILENAME4, 0); - imv_navigator_add(&nav, FILENAME5, 0); - imv_navigator_add(&nav, FILENAME6, 0); + /* Add 6 paths, one non-existant should fail */ + assert_false(imv_navigator_add(&nav, FILENAME1, 0)); + assert_false(imv_navigator_add(&nav, FILENAME2, 0)); + assert_false(imv_navigator_add(&nav, FILENAME3, 0)); + assert_false(imv_navigator_add(&nav, FILENAME4, 0)); + assert_false(imv_navigator_add(&nav, FILENAME5, 0)); + assert_false(imv_navigator_add(&nav, FILENAME6, 0)); assert_int_equal(nav.num_paths, 6); /* Check poll_changed */ @@ -88,7 +88,7 @@ static void test_navigator_file_changed(void **state) } assert_false(futimens(fd, times) == -1); - imv_navigator_add(&nav, FILENAME1, 0); + assert_false(imv_navigator_add(&nav, FILENAME1, 0)); assert_true(imv_navigator_poll_changed(&nav, 0)); assert_false(imv_navigator_poll_changed(&nav, 0)); |