cex

C/Curses file EXplorer
git clone git://git.wimdupont.com/cex.git
Log | Files | Refs | README | LICENSE

commit cd2a4eedac87b81e12927f6c32e1ff426b705302
parent 77d4880f24ae7d2a919c8a54a3ad4fac9f91751b
Author: Wim Dupont <wim@wimdupont.com>
Date:   Sat,  5 Jul 2025 08:49:02 +0200

do not open when not a file

Diffstat:
Mcex.c | 42+++++++++++++++++++++++++++---------------
1 file changed, 27 insertions(+), 15 deletions(-)

diff --git a/cex.c b/cex.c @@ -107,6 +107,7 @@ static char *get_file_info(char *buf, const char *filepath); static char *get_fullpath(char *buf, DirWin *dirwin, size_t index); static char *get_dirname(char *buf, const char *path); static char *replace_home(char *buf, const char *path); +static mode_t get_st_mode(DirWin *dirwin, size_t index); static DirWin curwin, parwin, childwin; static char *userhome, *username, *editor, **selected, searchq[SEARCHLEN]; @@ -294,7 +295,7 @@ start(void) if (is_dir(&curwin, curwin.highlight)) change_dir(get_fullpath(chdname, &curwin, curwin.highlight), RIGHT, c == KEY_VRIGHT_ABS); - else + else if (S_ISREG(get_st_mode(&curwin, curwin.highlight))) open_child(FALSE); break; case KEY_LEFT: @@ -606,8 +607,6 @@ print_win(DirWin *dirwin) void update_child_win(void) { - char pathbuf[PATH_MAX]; - if (curwin.filecount <= 0) { free_dirwin(&childwin); return; @@ -617,14 +616,17 @@ update_child_win(void) switch (curwin.winfiles[curwin.highlight].d_type) { case DT_LNK: - if (access(get_fullpath(pathbuf, &curwin, curwin.highlight), F_OK) != 0) { + if (access(childwin.path, F_OK) != 0) { set_win_message(&childwin, "Link no longer exists."); break; } if (is_dir(&curwin, curwin.highlight)) goto dir; - else + else if (S_ISREG(get_st_mode(&curwin, curwin.highlight))) goto reg; + else + goto def; + break; case DT_DIR: dir: set_win_files(&childwin); @@ -634,7 +636,11 @@ update_child_win(void) print_content(); break; default: + def: + werase(childwin.window); free_dirwin(&childwin); + set_win_message(&childwin, "Not a file."); + wrefresh(childwin.window); break; } } @@ -855,13 +861,13 @@ combo_open(int key) case KEY_COMBO_OPEN_NOHUP_XDG: if (is_dir(&curwin, curwin.highlight)) change_dir(get_fullpath(chdname, &curwin, curwin.highlight), RIGHT, FALSE); - else + else if (S_ISREG(get_st_mode(&curwin, curwin.highlight))) open_nohup_xdg(); break; case KEY_COMBO_OPEN_EXEC: if (is_dir(&curwin, curwin.highlight)) change_dir(get_fullpath(chdname, &curwin, curwin.highlight), RIGHT, FALSE); - else + else if (S_ISREG(get_st_mode(&curwin, curwin.highlight))) open_child(TRUE); break; default: @@ -1127,7 +1133,8 @@ prev_search(void) void free_dirwin(DirWin *dirwin) { - free(dirwin->winfiles); + if (dirwin->winfiles != NULL) + free(dirwin->winfiles); dirwin->filecount = 0; dirwin->winfiles = NULL; dirwin->message = NULL; @@ -1176,21 +1183,26 @@ fatal(const char *fmt, ...) bool is_dir(DirWin *dirwin, size_t index) { - struct stat statbuf; - char linkpath[PATH_MAX], abspath[PATH_MAX]; - if (dirwin->filecount <= 0) return FALSE; if (dirwin->winfiles[index].d_type == DT_DIR) return TRUE; - get_fullpath(linkpath, dirwin, index); + return S_ISDIR(get_st_mode(dirwin, index)); +} - if (dirwin->winfiles[index].d_type == DT_LNK) - return (lstat(realpath(linkpath, abspath), &statbuf) >= 0) && S_ISDIR(statbuf.st_mode); +mode_t +get_st_mode(DirWin *dirwin, size_t index) +{ + struct stat statbuf; + char linkpath[PATH_MAX], abspath[PATH_MAX]; - return FALSE; + get_fullpath(linkpath, dirwin, index); + if (lstat(realpath(linkpath, abspath), &statbuf) >= 0) + return statbuf.st_mode; + + return -1; } char *