sfm

simple file manager
git clone https://git.afify.dev/sfm.git
Log | Files | Refs | README | LICENSE

commit 40e1fa73f1ae7b45c41f15e5e23d3b43921af1ba
parent 2022bab8177e59400bd7662f1f4f3aa78d4a7fc0
Author: afify <hassan@afify.dev>
Date:   Tue, 25 May 2021 16:52:32 +0300

[fix] rm yank_file, use **selected_files

- single yank or deletion is appended to the *selected_files
- making paste and move single file use spawn() with no execption
- ability of adding individual files to **selected_files

Diffstat:
Msfm.c | 54+++++++++++++++++++++++-------------------------------
1 file changed, 23 insertions(+), 31 deletions(-)

diff --git a/sfm.c b/sfm.c @@ -193,7 +193,6 @@ static char *editor[2]; static char fed[] = "vi"; static int theight, twidth, scrheight; static size_t selection_size = 0; -static char *yank_file[MAX_P]; static int *selection; static int cont_vmode = 0; static char **selected_files; @@ -1410,10 +1409,16 @@ selcalc(void) static void free_files(void) { - if (selected_files == NULL) - return; - free(selected_files); - selected_files = NULL; + size_t i; + + if (selected_files != NULL) { + for (i = 0; i < selection_size; i++) { + free(selected_files[i]); + selected_files[i] = NULL; + } + free(selected_files); + selected_files = NULL; + } } static void @@ -1426,7 +1431,9 @@ init_files(void) selected_files = ecalloc(selection_size, sizeof(char *)); for (i = 0; i < selection_size; i++) { - selected_files[i] = cpane->direntr[selection[i] - 1].name; + selected_files[i] = ecalloc(MAX_P, sizeof(char)); + strncpy(selected_files[i], + cpane->direntr[selection[i] - 1].name, MAX_P); } } @@ -1472,21 +1479,11 @@ seldel(void) static void paste(void) { - if (yank_file[0] != NULL) { - print_status(cprompt, "coping"); - if (spawn(cp_cmd, cp_cmd_len, &yank_file, 1, cpane->dirn) != 0) - print_error(strerror(errno)); - else - print_status(cprompt, "file copied"); - yank_file[0] = NULL; /* set yank_file len 0 */ + if (selected_files == NULL) { + print_error("nothing to paste"); return; } - print_error("nothing to paste"); - - if (selected_files == NULL) - return; - if (spawn(cp_cmd, cp_cmd_len, selected_files, selection_size, cpane->dirn) < 0) print_error(strerror(errno)); @@ -1499,21 +1496,11 @@ paste(void) static void selmv(void) { - if (yank_file[0] != NULL) { - print_status(cprompt, "moving"); - if (spawn(mv_cmd, mv_cmd_len, yank_file, 1, cpane->dirn) != 0) - print_error(strerror(errno)); - else - print_status(cprompt, "file moved"); - yank_file[0] = NULL; /* set yank_file len 0 */ + if (selected_files == NULL) { + print_error("nothing to move"); return; } - print_error("nothing to move"); - - if (selected_files == NULL) - return; - if (spawn(mv_cmd, mv_cmd_len, selected_files, selection_size, cpane->dirn) < 0) print_error(strerror(errno)); @@ -1557,7 +1544,12 @@ yank(void) { if (cpane->dirc < 1) return; - yank_file[0] = CURSOR(cpane).name; + + free_files(); + selection_size = 1; + selected_files = ecalloc(selection_size, sizeof(char *)); + selected_files[0] = ecalloc(MAX_P, sizeof(char)); + strncpy(selected_files[0], CURSOR(cpane).name, MAX_P); print_status(cprompt, "1 file is yanked", selection_size); }