From 5e2cb884c58732863246948f4f1cebc220e048a0 Mon Sep 17 00:00:00 2001 From: night0721 Date: Sun, 17 Nov 2024 19:27:42 +0000 Subject: [PATCH] Reduce lines for p_cwd logic --- README.md | 3 +-- ccc.1 | 2 +- ccc.c | 44 ++++++++++++++++++++++---------------------- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 3b0693e..447c659 100644 --- a/README.md +++ b/README.md @@ -60,10 +60,10 @@ z: refresh current dir A: show directory disk usage/block size i: toggle file details u: sort files +x: view file/dir attributes y: copy filename to clipboard !: open shell in current dir - f: new file n: new dir r: rename @@ -83,7 +83,6 @@ O: open file with a GUI program detached from file manager /: search -x: view file/dir attributes e: show history c: copy diff --git a/ccc.1 b/ccc.1 index 7c8c8d3..a477a12 100644 --- a/ccc.1 +++ b/ccc.1 @@ -40,10 +40,10 @@ z: refresh current dir A: show directory disk usage/block size i: toggle file details u: sort files +x: view file/dir attributes y: copy filename to clipboard !: open shell in current dir - f: new file n: new dir r: rename diff --git a/ccc.c b/ccc.c index 2cf3424..dca3918 100644 --- a/ccc.c +++ b/ccc.c @@ -83,8 +83,8 @@ long sel_file = 0; int file_picker = 1; int to_open_file = 0; char *argv_cp; -char *cwd; -char *p_cwd; /* previous cwd */ +char cwd[4096]; +char p_cwd[4096]; /* previous cwd */ int half_width; ArrayList *files; ArrayList *marked; @@ -161,10 +161,7 @@ int main(int argc, char **argv) marked = arraylist_init(100); hashtable_init(); - cwd = memalloc(PATH_MAX); getcwd(cwd, PATH_MAX); - p_cwd = memalloc(PATH_MAX); - p_cwd[0] = '\0'; populate_files(cwd, 0, &files); handle_sigwinch(-1); @@ -193,27 +190,27 @@ int main(int argc, char **argv) break; /* go back */ - case BACKSPACE: - case ARROW_LEFT: - case 'h': + case BACKSPACE:; + case ARROW_LEFT:; + case 'h':; + char dir[PATH_MAX]; + strcpy(dir, cwd); /* get parent directory */ - strcpy(p_cwd, cwd); - char *last_slash = strrchr(cwd, '/'); + char *last_slash = strrchr(dir, '/'); if (last_slash) { - if (last_slash == cwd) { + if (!strcmp(last_slash, dir)) { change_dir("/", 0, 0); break; } *last_slash = '\0'; - change_dir(cwd, 0, 0); + change_dir(dir, 0, 0); } break; /* enter directory/open a file */ - case ENTER: - case ARROW_RIGHT: - case 'l': - strcpy(p_cwd, cwd); + case ENTER:; + case ARROW_RIGHT:; + case 'l':; file c_file = files->items[sel_file]; /* Check if it is directory or a regular file */ @@ -284,7 +281,6 @@ int main(int argc, char **argv) wpprintw("$HOME not defined (Press any key to continue)"); readch(); } else { - strcpy(p_cwd, cwd); change_dir(home, 0, 0); } break; @@ -293,7 +289,6 @@ int main(int argc, char **argv) case 't':; char *trash_dir = check_trash_dir(); if (trash_dir) { - strcpy(p_cwd, cwd); change_dir(trash_dir, 0, 0); } break; @@ -504,8 +499,12 @@ char *check_trash_dir(void) */ void change_dir(const char *buf, int selection, int ftype) { - if (cwd != buf) - strcpy(cwd, buf); + if (strcmp(cwd, buf) != 0) { + char tmp[PATH_MAX]; + strcpy(tmp, buf); + strcpy(p_cwd, cwd); + strcpy(cwd, tmp); + } if (ftype == 0) arraylist_free(files); chdir(cwd); @@ -1171,8 +1170,9 @@ void goto_dir(void) wpprintw("chdir failed: %s (Press any key to continue)", strerror(errno)); readch(); } - getcwd(cwd, PATH_MAX); - change_dir(cwd, 0, 0); + char new_cwd[PATH_MAX]; + getcwd(new_cwd, PATH_MAX); + change_dir(new_cwd, 0, 0); free(input); }