Reduce lines for p_cwd logic

This commit is contained in:
Night Kaly 2024-11-17 19:27:42 +00:00
parent ec859e8403
commit 5e2cb884c5
Signed by: night0721
SSH key fingerprint: SHA256:B/hgVwUoBpx5vdNsXl9w8XwZljA9766uk6T4ubZp5HM
3 changed files with 24 additions and 25 deletions

View file

@ -60,10 +60,10 @@ z: refresh current dir
A: show directory disk usage/block size A: show directory disk usage/block size
i: toggle file details i: toggle file details
u: sort files u: sort files
x: view file/dir attributes
y: copy filename to clipboard y: copy filename to clipboard
!: open shell in current dir !: open shell in current dir
f: new file f: new file
n: new dir n: new dir
r: rename r: rename
@ -83,7 +83,6 @@ O: open file with a GUI program detached from file manager
/: search /: search
x: view file/dir attributes
e: show history e: show history
c: copy c: copy

2
ccc.1
View file

@ -40,10 +40,10 @@ z: refresh current dir
A: show directory disk usage/block size A: show directory disk usage/block size
i: toggle file details i: toggle file details
u: sort files u: sort files
x: view file/dir attributes
y: copy filename to clipboard y: copy filename to clipboard
!: open shell in current dir !: open shell in current dir
f: new file f: new file
n: new dir n: new dir
r: rename r: rename

44
ccc.c
View file

@ -83,8 +83,8 @@ long sel_file = 0;
int file_picker = 1; int file_picker = 1;
int to_open_file = 0; int to_open_file = 0;
char *argv_cp; char *argv_cp;
char *cwd; char cwd[4096];
char *p_cwd; /* previous cwd */ char p_cwd[4096]; /* previous cwd */
int half_width; int half_width;
ArrayList *files; ArrayList *files;
ArrayList *marked; ArrayList *marked;
@ -161,10 +161,7 @@ int main(int argc, char **argv)
marked = arraylist_init(100); marked = arraylist_init(100);
hashtable_init(); hashtable_init();
cwd = memalloc(PATH_MAX);
getcwd(cwd, PATH_MAX); getcwd(cwd, PATH_MAX);
p_cwd = memalloc(PATH_MAX);
p_cwd[0] = '\0';
populate_files(cwd, 0, &files); populate_files(cwd, 0, &files);
handle_sigwinch(-1); handle_sigwinch(-1);
@ -193,27 +190,27 @@ int main(int argc, char **argv)
break; break;
/* go back */ /* go back */
case BACKSPACE: case BACKSPACE:;
case ARROW_LEFT: case ARROW_LEFT:;
case 'h': case 'h':;
char dir[PATH_MAX];
strcpy(dir, cwd);
/* get parent directory */ /* get parent directory */
strcpy(p_cwd, cwd); char *last_slash = strrchr(dir, '/');
char *last_slash = strrchr(cwd, '/');
if (last_slash) { if (last_slash) {
if (last_slash == cwd) { if (!strcmp(last_slash, dir)) {
change_dir("/", 0, 0); change_dir("/", 0, 0);
break; break;
} }
*last_slash = '\0'; *last_slash = '\0';
change_dir(cwd, 0, 0); change_dir(dir, 0, 0);
} }
break; break;
/* enter directory/open a file */ /* enter directory/open a file */
case ENTER: case ENTER:;
case ARROW_RIGHT: case ARROW_RIGHT:;
case 'l': case 'l':;
strcpy(p_cwd, cwd);
file c_file = files->items[sel_file]; file c_file = files->items[sel_file];
/* Check if it is directory or a regular 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)"); wpprintw("$HOME not defined (Press any key to continue)");
readch(); readch();
} else { } else {
strcpy(p_cwd, cwd);
change_dir(home, 0, 0); change_dir(home, 0, 0);
} }
break; break;
@ -293,7 +289,6 @@ int main(int argc, char **argv)
case 't':; case 't':;
char *trash_dir = check_trash_dir(); char *trash_dir = check_trash_dir();
if (trash_dir) { if (trash_dir) {
strcpy(p_cwd, cwd);
change_dir(trash_dir, 0, 0); change_dir(trash_dir, 0, 0);
} }
break; break;
@ -504,8 +499,12 @@ char *check_trash_dir(void)
*/ */
void change_dir(const char *buf, int selection, int ftype) void change_dir(const char *buf, int selection, int ftype)
{ {
if (cwd != buf) if (strcmp(cwd, buf) != 0) {
strcpy(cwd, buf); char tmp[PATH_MAX];
strcpy(tmp, buf);
strcpy(p_cwd, cwd);
strcpy(cwd, tmp);
}
if (ftype == 0) if (ftype == 0)
arraylist_free(files); arraylist_free(files);
chdir(cwd); chdir(cwd);
@ -1171,8 +1170,9 @@ void goto_dir(void)
wpprintw("chdir failed: %s (Press any key to continue)", strerror(errno)); wpprintw("chdir failed: %s (Press any key to continue)", strerror(errno));
readch(); readch();
} }
getcwd(cwd, PATH_MAX); char new_cwd[PATH_MAX];
change_dir(cwd, 0, 0); getcwd(new_cwd, PATH_MAX);
change_dir(new_cwd, 0, 0);
free(input); free(input);
} }