cex

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

commit 109c1e21635dd1c77cab80b1ae6f8739dadd8da9
parent d11c2cb224a63cbea6c5f957b2915393dbbc3b9d
Author: Wim Dupont <wim@wimdupont.com>
Date:   Sat, 13 Jul 2024 22:51:45 +0200

more cleanup and fixes

Diffstat:
Mcex.c | 46+++++++++++++++++++++-------------------------
Mcex.h | 30+++++++++++++++---------------
2 files changed, 36 insertions(+), 40 deletions(-)

diff --git a/cex.c b/cex.c @@ -285,7 +285,7 @@ start() } static void -wpath(char *filename) +wpath(const char *filename) { FILE *fptr; @@ -386,7 +386,7 @@ is_selected(DirWin *dirwin, size_t index) } static bool -remove_selected(char *sel) +remove_selected(const char *sel) { bool res = FALSE; @@ -577,15 +577,13 @@ print_bot_title() } static char * -get_file_info(char *buf, char *filepath) +get_file_info(char *buf, const char *filepath) { struct stat statbuf; struct passwd *usr; struct tm *tm; struct group *grp; - char mods[11]; - char datestr[256]; - char modfill = '-'; + char mods[11], datestr[256], modfill = '-'; if ((stat(filepath, &statbuf)) != 0) { switch (errno) { @@ -790,7 +788,7 @@ combo_make(int key) } static void -change_dir(char *chdname, Direction direction, bool abspath) +change_dir(const char *chdname, Direction direction, bool abspath) { if (chdir(chdname) != 0) return; @@ -989,7 +987,7 @@ next_search() if (searchc == 0) return; - for (size_t i = curwin.highlight; i < curwin.filecount-1; i++) { + for (size_t i = curwin.highlight; i < MAX(curwin.filecount-1, 0); i++) { if ((strstr(curwin.winfiles[i+1].d_name, searchq) != NULL)) { curwin.highlight = i+1; return; @@ -1064,8 +1062,7 @@ static bool is_dir(DirWin *dirwin, size_t index) { struct stat statbuf; - char linkpath[PATH_MAX]; - char abspath[PATH_MAX]; + char linkpath[PATH_MAX], abspath[PATH_MAX]; if (dirwin->filecount <= 0) return FALSE; @@ -1095,7 +1092,7 @@ compare_file(const void *a, const void *b) } static int -get_mime(char *buf, size_t bufsize, char *path) +get_mime(char *buf, size_t bufsize, const char *path) { char *cmdfmt = "xdg-mime query filetype '%s'"; size_t size = PATH_MAX + strlen(cmdfmt); @@ -1107,7 +1104,7 @@ get_mime(char *buf, size_t bufsize, char *path) } static int -get_mime_default(char *buf, size_t bufsize, char *mime) +get_mime_default(char *buf, size_t bufsize, const char *mime) { char *cmdfmt = "xdg-mime query default '%s'"; size_t cmdsize = MIME_MAX + strlen(cmdfmt); @@ -1119,7 +1116,7 @@ get_mime_default(char *buf, size_t bufsize, char *mime) } static int -read_command(char *buf, size_t bufsize, char *cmd) +read_command(char *buf, size_t bufsize, const char *cmd) { FILE *file; @@ -1155,7 +1152,7 @@ run_command(size_t size, const char *fmt, ...) } static int -make_dir(char *path) +make_dir(const char *path) { char name[PATH_MAX]; @@ -1176,7 +1173,7 @@ make_dir(char *path) } static int -make_file(char *path) +make_file(const char *path) { FILE *fptr; char name[PATH_MAX]; @@ -1202,7 +1199,7 @@ make_file(char *path) } static int -rm_file(char *fname) +rm_file(const char *fname) { char *msg, response[1], *rmdirfmt = "rm -rf %s"; int res = 0; @@ -1252,7 +1249,7 @@ rm_file(char *fname) } static int -rename_file(char *fname) +rename_file(const char *fname) { char name[PATH_MAX]; @@ -1312,15 +1309,14 @@ make_mod() static int make_access() { - char *cmdfmt = "ln -s %s %s"; - char pathbuf[PATH_MAX]; + char pathbuf[PATH_MAX], *cmdfmt = "ln -s %s %s"; size_t size = PATH_MAX + strlen(cmdfmt); run_command(size, cmdfmt, get_fullpath(pathbuf, &curwin, curwin.highlight), LN_ACCESS_DIR); } static void -exe_selection(SelAction action, char *askn) +exe_selection(SelAction action, const char *askn) { char response[1]; size_t size = PATH_MAX*2 + 20; @@ -1371,7 +1367,7 @@ exe_selection(SelAction action, char *askn) } static void -select_file(char *path) +select_file(const char *path) { if (selc == 0 && ((selected = (char**) malloc(sizeof(char *)))) == NULL) fatal("Fatal: failed to malloc selected.\n"); @@ -1406,7 +1402,7 @@ clear_search() } static char * -cpstr(char *dest, char *src) +cpstr(char *dest, const char *src) { size_t cplen = strlen(src); memcpy(dest, src, cplen); @@ -1417,8 +1413,8 @@ cpstr(char *dest, char *src) static char * get_fullpath(char *buf, DirWin *dirwin, size_t index) { - index = MAX(index, 0); index = MIN(index, dirwin->filecount-1); + index = MAX(index, 0); if (strcmp(dirwin->path, "/") == 0) snprintf(buf, PATH_MAX, "/%s", dirwin->winfiles[index].d_name); @@ -1429,7 +1425,7 @@ get_fullpath(char *buf, DirWin *dirwin, size_t index) } static char * -replace_home(char *buf, char *path) +replace_home(char *buf, const char *path) { char *envv = "$HOME"; @@ -1441,7 +1437,7 @@ replace_home(char *buf, char *path) } static char * -get_dirname(char *buf, char *path) +get_dirname(char *buf, const char *path) { cpstr(buf, path); dirname(buf); diff --git a/cex.h b/cex.h @@ -90,7 +90,7 @@ static void init_dirwins(); static void init_screen(); static void resize(); static void start(); -static void wpath(char *filename); +static void wpath(const char *filename); static void reset_flags(); static void set_startpr(DirWin *dirwin); static void set_win_files(DirWin *dirwin); @@ -101,8 +101,8 @@ static void print_top_title(); static void print_bot_title(); static void print_content(); static void show_file_mime(); -static void select_file(char *path); -static void exe_selection(SelAction action, char *askn); +static void select_file(const char *path); +static void exe_selection(SelAction action, const char *askn); static void clear_selected(); static void clear_search(); static void combo_key(int keypress); @@ -110,7 +110,7 @@ static void combo_go(int keypress); static void combo_inf(int keypress); static void combo_open(int keypress); static void combo_make(int keypress); -static void change_dir(char *chdname, Direction direction, bool abspath); +static void change_dir(const char *chdname, Direction direction, bool abspath); static void change_parent_dir(Direction direction); static void open_child(); static void open_nohup_xdg(); @@ -129,21 +129,21 @@ static void clean(); static void fatal(const char *fmt, ...); static bool is_dir(DirWin *dirwin, size_t index); static bool is_selected(DirWin *dirwin, size_t count); -static bool remove_selected(char *sel); +static bool remove_selected(const char *sel); static int compare_file(const void *a, const void *b); -static int get_mime(char *buf, size_t bufsize, char *path); -static int get_mime_default(char *buf, size_t bufsize, char *mime); -static int read_command(char *buf, size_t bufsize, char *cmd); -static int make_file(char *path); -static int make_dir(char *path); +static int get_mime(char *buf, size_t bufsize, const char *path); +static int get_mime_default(char *buf, size_t bufsize, const char *mime); +static int read_command(char *buf, size_t bufsize, const char *cmd); +static int make_file(const char *path); +static int make_dir(const char *path); static int make_mod(); static int make_access(); -static int rm_file(char *fname); +static int rm_file(const char *fname); static int rename_file(); -static char *cpstr(char *dest, char *src); -static char *get_file_info(char *buf, char *filepath); +static char *cpstr(char *dest, const char *src); +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, char *path); -static char *replace_home(char *buf, char *path); +static char *get_dirname(char *buf, const char *path); +static char *replace_home(char *buf, const char *path); #endif