cex

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

commit cf2649cd06e7fb5fd928588dcf566aee11f85b4a
parent 74656d0325d33f5ed257699185a15b645803fa6e
Author: Wim Dupont <wim@wimdupont.com>
Date:   Sat, 13 Jul 2024 21:35:12 +0200

some cleanup

Diffstat:
Mcex.c | 86+++++++++++++++++++++++++++++++------------------------------------------------
Mcex.h | 1+
2 files changed, 35 insertions(+), 52 deletions(-)

diff --git a/cex.c b/cex.c @@ -1,10 +1,7 @@ -#include <sys/types.h> #include <sys/stat.h> -#include <sys/wait.h> +#include <sys/param.h> #include <ctype.h> #include <unistd.h> -#include <limits.h> -#include <stdio.h> #include <stdlib.h> #include <libgen.h> #include <string.h> @@ -14,7 +11,6 @@ #include <grp.h> #include <dirent.h> #include <errno.h> -#include <sys/param.h> #include <curses.h> #include "cex.h" @@ -23,7 +19,7 @@ static DirWin curwin, parwin, childwin; static char *userhome, *username, *editor, **selected, searchq[SEARCHLEN]; static int maxy, maxx; static size_t selc, searchc; -static bool hide = DEFAULT_HIDE, print_bot = TRUE; +static bool print_bot, hide = DEFAULT_HIDE; int main(int argc, char **argv) @@ -36,7 +32,7 @@ main(int argc, char **argv) while ((opt = getopt(argc, argv, "f:")) != -1) { switch (opt) { case 'f': - strcpy(filename, optarg); + cpstr(filename, optarg); writepath = TRUE; break; default: @@ -83,7 +79,7 @@ init_dirwins() set_win_files(&parwin); if (curwin.winfiles != NULL && curwin.winfiles[0].d_type == DT_DIR) { - strcpy(childwin.path, curwin.path); + cpstr(childwin.path, curwin.path); update_child_win(); } } @@ -91,8 +87,6 @@ init_dirwins() static void init_screen() { - FILE *scrfile; - initscr(); noecho(); start_color(); @@ -359,7 +353,7 @@ set_win_files(DirWin *dirwin) continue; if ((dirwin->winfiles = (WinFile*) realloc(dirwin->winfiles, (count+1)*(sizeof(WinFile)))) == NULL) fatal("Fatal: failed to realloc.\n"); - strcpy(dirwin->winfiles[count].d_name, ent->d_name); + cpstr(dirwin->winfiles[count].d_name, ent->d_name); dirwin->winfiles[count].d_type = ent->d_type; dirwin->winfiles[count].selected = is_selected(dirwin, count); (count)++; @@ -435,13 +429,9 @@ print_win(DirWin *dirwin) if (dirwin->holdupdate) return; - size_t size = dirwin->maxx; - int y = 0, x = 1; - char name[size+1], sbuf[size+1]; - char *subs; - int sindex; - char abspath[PATH_MAX]; - char pathbuf[PATH_MAX]; + size_t cplen, size = dirwin->maxx; + char *subs, name[size+1], sbuf[size+1], pathbuf[PATH_MAX]; + int sindex, y = 0, x = 1; wclear(dirwin->window); @@ -507,8 +497,9 @@ print_win(DirWin *dirwin) wattron(dirwin->window, COLOR_PAIR(SEARCH_MATCH_COLOR)); if (dirwin->usehighlight && dirwin->highlight == i) wattron(dirwin->window, A_REVERSE); - memcpy(sbuf, name + sindex, strlen(searchq)); - sbuf[strlen(searchq)] = '\0'; + cplen = strlen(searchq); + memcpy(sbuf, name + sindex, cplen); + sbuf[cplen] = '\0'; mvwaddstr(dirwin->window, y, x+sindex, sbuf); wattroff(dirwin->window, COLOR_PAIR(SEARCH_MATCH_COLOR)); if (dirwin->usehighlight && dirwin->highlight == i) @@ -581,7 +572,7 @@ print_bot_title() get_fullpath(pathbuf, &curwin, curwin.highlight); memcpy(fileinf, get_file_info(fileinf, pathbuf), maxx); - fileinf[strlen(fileinf)] = '\0'; + fileinf[maxx] = '\0'; move(maxy-1, 0); clrtoeol(); @@ -656,10 +647,9 @@ static void print_content() { char buf[PATH_MAX]; - char cmd[300]; - FILE * fp = NULL; + FILE *fp = NULL; size_t size = childwin.maxx; - char str[size + 1]; + char str[size+1]; int y = 0, x = 0; werase(childwin.window); @@ -790,8 +780,6 @@ combo_open(int key) static void combo_make(int key) { - char chdname[PATH_MAX]; - switch (key) { case KEY_COMBO_MAKE_FILE: make_file(curwin.path); @@ -817,13 +805,11 @@ combo_make(int key) static void change_dir(char *chdname, Direction direction, bool abspath) { - char tmpdir[PATH_MAX]; - if (chdir(chdname) != 0) return; if (!abspath) - strcpy(curwin.path, chdname); + cpstr(curwin.path, chdname); else if (getcwd(curwin.path, PATH_MAX) == NULL) fatal("getcwd() error"); @@ -1016,8 +1002,6 @@ move_down(DirWin *dirwin) static void next_search() { - size_t size = curwin.maxx; - if (searchc == 0) return; @@ -1032,8 +1016,6 @@ next_search() static void prev_search() { - size_t size = curwin.maxx; - if (searchc == 0) return; @@ -1100,7 +1082,6 @@ is_dir(DirWin *dirwin, size_t index) struct stat statbuf; char linkpath[PATH_MAX]; char abspath[PATH_MAX]; - size_t len; if (dirwin->filecount <= 0) return FALSE; @@ -1192,7 +1173,6 @@ run_command(size_t size, const char *fmt, ...) static int make_dir(char *path) { - FILE *fptr; char name[PATH_MAX]; move(maxy-1, 0); @@ -1240,12 +1220,9 @@ make_file(char *path) static int rm_file(char *fname) { + char *msg, response[1], *rmdirfmt = "rm -rf %s"; int res = 0; - char response[1]; - char *msg; - char *rmdirfmt = "rm -rf %s"; size_t size = PATH_MAX + strlen(rmdirfmt); - char cmd[size]; move(maxy-1, 0); clrtoeol(); @@ -1293,7 +1270,6 @@ rm_file(char *fname) static int rename_file(char *fname) { - int res = 0; char name[PATH_MAX]; move(maxy-1, 0); @@ -1312,15 +1288,14 @@ rename_file(char *fname) if (strlen(name) > 0) return rename(fname, name); - return res; + return 0; } static int make_mod() { - int res = 0; char name[3]; - int mod; + bool isvalid; move(maxy-1, 0); clrtoeol(); @@ -1336,16 +1311,18 @@ make_mod() curs_set(0); if (strlen(name) == 3) { - bool isvalid = TRUE; + isvalid = TRUE; for (size_t i = 0; i < 3; i++) { if (!isdigit(name[i])) isvalid = FALSE; } if (isvalid) chmod(curwin.winfiles[curwin.highlight].d_name, strtol(name, NULL, 8)); + else + return -1; } - return res; + return 0; } static int @@ -1354,14 +1331,13 @@ make_access() char *cmdfmt = "ln -s %s %s"; char pathbuf[PATH_MAX]; 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) { - char *cmdfmt; - char dest[PATH_MAX]; char response[1]; size_t size = PATH_MAX*2 + 20; @@ -1446,6 +1422,15 @@ clear_search() } static char * +cpstr(char *dest, char *src) +{ + size_t cplen = strlen(src); + memcpy(dest, src, cplen); + dest[cplen] = '\0'; + return dest; +} + +static char * get_fullpath(char *buf, DirWin *dirwin, size_t index) { index = MAX(index, 0); @@ -1463,12 +1448,9 @@ static char * replace_home(char *buf, char *path) { char *envv = "$HOME"; - size_t hlen; if ((strstr(path, envv) != NULL)) { - hlen = strlen(userhome); - memcpy(buf, userhome, hlen); - buf[hlen] = '\0'; + cpstr(buf, userhome); strcat(buf, path + strlen(envv)); } return buf; @@ -1477,7 +1459,7 @@ replace_home(char *buf, char *path) static char * get_dirname(char *buf, char *path) { - strcpy(buf, path); + cpstr(buf, path); dirname(buf); return buf; } diff --git a/cex.h b/cex.h @@ -140,6 +140,7 @@ static int make_mod(); static int make_access(); static int rm_file(char *fname); static int rename_file(); +static char *cpstr(char *dest, char *src); static char *get_file_info(char *buf, char *filepath); static char *get_fullpath(char *buf, DirWin *dirwin, size_t index); static char *get_dirname(char *buf, char *path);