From 255e09c18d5562c85bb98df9bba96719d6824376 Mon Sep 17 00:00:00 2001 From: night0721 Date: Sun, 17 Mar 2024 23:21:58 +0000 Subject: [PATCH] make mark all work correctly as expected --- ccc.c | 19 ++++++++++--------- file.c | 25 ++++++++++++++++++------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/ccc.c b/ccc.c index bbb789b..5a9a493 100644 --- a/ccc.c +++ b/ccc.c @@ -236,16 +236,15 @@ int main(int argc, char** argv) break; /* mark one file */ - case SPACE:; + case SPACE: add_file_stat(get_filepath(current_selection), 1); highlight_current_line(); break; /* mark all files in directory */ - case 'a':; - int save_current_sel = current_selection; - populate_files(cwd, 1); - change_dir(cwd, save_current_sel); /* reload current dir */ + case 'a': + populate_files(cwd, 2); + change_dir(cwd, current_selection); /* reload current dir */ break; /* mark actions: */ @@ -368,7 +367,7 @@ int mkdir_p(const char *destdir) /* * Read the provided directory and add all files in directory to linked list - * ftype: normal files = 0, marked = 1 + * ftype: normal files = 0, marked = 1, marking ALL = 2 * ep->d_name -> filename */ void populate_files(const char *path, int ftype) @@ -417,7 +416,7 @@ int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, s /* * Get file's last modified time, size, type * Add that file into list - * ftype: normal file = 0, marked = 1 + * ftype: normal file = 0, normal marked = 1, marking ALL = 2 */ long add_file_stat(char *filepath, int ftype) { @@ -457,8 +456,10 @@ long add_file_stat(char *filepath, int ftype) } /* if file is to be marked */ - if (ftype == 1) { - long index = add_marked(filepath, type); + if (ftype == 1 || ftype == 2) { + /* force if user is marking all files */ + bool force = ftype == 2 ? true : false; + long index = add_marked(filepath, type, force); /* free type and return without allocating more stuff */ free(type); if (index != -1) { diff --git a/file.c b/file.c index cfb4976..92179b2 100644 --- a/file.c +++ b/file.c @@ -131,7 +131,10 @@ void remove_marked(file *marked_file) } } -long add_marked(char *filepath, char *type) +/* + * force will not remove duplicate marked files, instead it just skip adding + */ +long add_marked(char *filepath, char *type, bool force) { file *current = marked; file *new_file = memalloc(sizeof(file)); @@ -153,17 +156,25 @@ long add_marked(char *filepath, char *type) long index = 1; while (current->next != NULL) { if (strcmp(current->path, new_file->path) == 0) { - remove_marked(current); - free_file(new_file); - return -1; + if (force) { + return index; + } else { + remove_marked(current); + free_file(new_file); + return -1; + } } current = current->next; index++; } if (strcmp(current->path, new_file->path) == 0){ - remove_marked(current); - free_file(new_file); - return -1; + if (force) { + return 0; + } else { + remove_marked(current); + free_file(new_file); + return -1; + } } current->next = new_file; return index;