commit a8d6afe1c62cb8da1a8574ae52490ab966d411e2
parent 8abcd06c301adae3d81cd9094c3b93d175174184
Author: Wim Dupont <wim@wimdupont.com>
Date: Tue, 27 Oct 2026 21:16:39 +0100
cleanup
Diffstat:
| M | cex.c | | | 150 | ++++++++++++++++++++++++++++++++++++++++++++++++------------------------------- |
1 file changed, 91 insertions(+), 59 deletions(-)
diff --git a/cex.c b/cex.c
@@ -114,8 +114,7 @@ static int rename_file(const char *fname);
static char *truncate_buf_prefix(char *buf, size_t bufsize, size_t max_len);
static char *human_readable_bytes(char *buf, intmax_t bytes);
static char *prompt_answer(char *buf, size_t size, const char *question);
-static char *cpstr(char *dest, const char *src);
-static char *substr(char *buf, const char *haystack, const char *needle, const bool casesens);
+static char *substr(const char *haystack, const char *needle, bool casesens);
static char *get_file_info(char *buf, size_t bufsize, const char *filepath);
static char *get_fullpath(char *buf, DirWin *dirwin, size_t index);
static char *get_dirname(char *buf, const char *path);
@@ -140,16 +139,16 @@ int
main(int argc, char **argv)
{
struct passwd userinf;
- char filename[PATH_MAX];
+ const char *filename = NULL;
int opt;
bool writepath = FALSE;
while ((opt = getopt(argc, argv, "f:")) != -1) {
switch (opt) {
case 'f':
- if (strlen(optarg) >= sizeof(filename))
+ if (strlen(optarg) >= PATH_MAX)
fatal("Output path is too long.\n");
- cpstr(filename, optarg);
+ filename = optarg;
writepath = TRUE;
break;
default:
@@ -199,7 +198,7 @@ init_dirwins(void)
set_win_files(&parwin);
if (curwin.winfiles != NULL && curwin.winfiles[0].d_type == DT_DIR) {
- cpstr(childwin.path, curwin.path);
+ strcpy(childwin.path, curwin.path);
update_child_win();
}
}
@@ -385,11 +384,15 @@ start(void)
exe_selection(MOVE, "Move");
break;
case KEY_RM_FILE:
+ if (curwin.filecount == 0)
+ break;
rm_file(curwin.winfiles[curwin.highlight].d_name);
set_win_files(&curwin);
update_child_win();
break;
case KEY_RENAME_FILE:
+ if (curwin.filecount == 0)
+ break;
rename_file(curwin.winfiles[curwin.highlight].d_name);
set_win_files(&curwin);
update_child_win();
@@ -502,7 +505,7 @@ set_win_files(DirWin *dirwin)
if ((dirwin->winfiles = (WinFile*) realloc(dirwin->winfiles, cap * sizeof(WinFile))) == NULL)
fatal("Fatal: failed to realloc.\n");
}
- cpstr(dirwin->winfiles[count].d_name, ent->d_name);
+ strcpy(dirwin->winfiles[count].d_name, ent->d_name);
dirwin->winfiles[count].d_type = ent->d_type;
dirwin->filecount = (int) count + 1;
dirwin->winfiles[count].selected = is_selected(dirwin, count);
@@ -575,8 +578,8 @@ print_win(DirWin *dirwin)
if (dirwin->holdupdate)
return;
- size_t cplen, len, width = dirwin->maxx > 1 ? (size_t) dirwin->maxx-1 : 0;
- char *subs, name[sizeof(dirwin->winfiles[0].d_name)+1], sbuf[SEARCHLEN+1];
+ size_t len, width = dirwin->maxx > 1 ? (size_t) dirwin->maxx-1 : 0;
+ char *subs, name[sizeof(dirwin->winfiles[0].d_name)+1];
int sindex, y = 0, x = 1;
werase(dirwin->window);
@@ -644,15 +647,12 @@ print_win(DirWin *dirwin)
} else
mvwaddstr(dirwin->window, y, x, name);
if (dirwin->wintype == CURRENT && searchc > 0
- && ((subs = substr(subs, name, searchq, searchcs)) != NULL)) {
+ && ((subs = substr(name, searchq, searchcs)) != NULL)) {
sindex = (int) (subs - name);
wattron(dirwin->window, COLOR_PAIR(SEARCH_MATCH_COLOR));
if (dirwin->usehighlight && dirwin->highlight == i)
wattron(dirwin->window, A_REVERSE);
- cplen = strlen(searchq);
- memcpy(sbuf, name + sindex, cplen);
- sbuf[cplen] = '\0';
- mvwaddstr(dirwin->window, y, x+sindex, sbuf);
+ mvwaddnstr(dirwin->window, y, x+sindex, subs, (int) strlen(searchq));
wattroff(dirwin->window, COLOR_PAIR(SEARCH_MATCH_COLOR));
if (dirwin->usehighlight && dirwin->highlight == i)
wattroff(dirwin->window, A_REVERSE);
@@ -767,21 +767,21 @@ get_file_info(char *buf, size_t bufsize, const char *filepath)
if ((stat(filepath, &statbuf)) != 0) {
switch (errno) {
case EACCES:
- buf = "No permission.";
+ snprintf(buf, bufsize, "%s", "No permission.");
break;
default:
- buf = "Retrieving filestats failed.";
+ snprintf(buf, bufsize, "%s", "Retrieving filestats failed.");
break;
}
return buf;
}
if ((usr = getpwuid(statbuf.st_uid)) == NULL) {
- buf = "Retrieving username failed.";
+ snprintf(buf, bufsize, "%s", "Retrieving username failed.");
return buf;
}
if ((grp = getgrgid(statbuf.st_gid)) == NULL) {
- buf = "Retrieving groupname failed.";
+ snprintf(buf, bufsize, "%s", "Retrieving groupname failed.");
return buf;
}
@@ -815,19 +815,19 @@ print_content(void)
{
FILE *fp = NULL;
size_t size = childwin.maxx;
- char str[size+1], buf[PATH_MAX];
+ char str[size+1];
int y = 0, x = 0;
werase(childwin.window);
free_dirwin(&childwin);
- if ((fp = fopen(get_fullpath(buf, &curwin, curwin.highlight), "r")) == NULL) {
+ if ((fp = fopen(childwin.path, "r")) == NULL) {
switch (errno) {
case EACCES:
set_win_message(&childwin, "No permission.");
return;
default:
- fatal("Error opening file \"%s\"\n", buf);
+ fatal("Error opening file \"%s\"\n", childwin.path);
}
}
@@ -862,19 +862,20 @@ void
show_file_mime(void)
{
int appsize = 256;
- char mimebuf[MIME_MAX], appbuf[appsize], buf[MIME_MAX+appsize];
+ char mimebuf[MIME_MAX], appbuf[appsize];
+
+ if (curwin.filecount == 0)
+ return;
if (get_mime(mimebuf, MIME_MAX, curwin.winfiles[curwin.highlight].d_name) != 0)
- cpstr(mimebuf, "Can't retrieve mime.");
+ strcpy(mimebuf, "Can't retrieve mime.");
if (get_mime_default(appbuf, appsize, mimebuf) != 0)
- cpstr(appbuf, "Can't retrieve default for mime.");
-
- snprintf(buf, MIME_MAX+appsize, "%s: %s", mimebuf, appbuf);
+ strcpy(appbuf, "Can't retrieve default for mime.");
move(1, 1);
clrtoeol();
- addstr(buf);
+ printw("%s: %s", mimebuf, appbuf);
}
void
@@ -1007,7 +1008,7 @@ change_dir(const char *chdname, Direction direction, bool abspath)
return;
if (!abspath)
- cpstr(curwin.path, chdname);
+ strcpy(curwin.path, chdname);
else if (getcwd(curwin.path, PATH_MAX) == NULL)
fatal("getcwd() error");
@@ -1206,14 +1207,14 @@ move_down(DirWin *dirwin)
void
first_search(void)
{
- if (searchc == 0)
+ if (searchc == 0 || curwin.filecount == 0)
return;
- if (substr(NULL, curwin.winfiles[curwin.highlight].d_name, searchq, searchcs) != NULL)
+ if (substr(curwin.winfiles[curwin.highlight].d_name, searchq, searchcs) != NULL)
return;
for (int i = 0; i < MAX(curwin.filecount-1, 0); i++) {
- if (substr(NULL, curwin.winfiles[i].d_name, searchq, searchcs) != NULL) {
+ if (substr(curwin.winfiles[i].d_name, searchq, searchcs) != NULL) {
curwin.highlight = i;
return;
}
@@ -1227,7 +1228,7 @@ next_search(void)
return;
for (int i = curwin.highlight; i < MAX(curwin.filecount-1, 0); i++) {
- if (substr(NULL, curwin.winfiles[i+1].d_name, searchq, searchcs) != NULL) {
+ if (substr(curwin.winfiles[i+1].d_name, searchq, searchcs) != NULL) {
curwin.highlight = i+1;
return;
}
@@ -1241,7 +1242,7 @@ prev_search(void)
return;
for (int i = curwin.highlight; i > 0; i--) {
- if (substr(NULL, curwin.winfiles[i-1].d_name, searchq, searchcs) != NULL) {
+ if (substr(curwin.winfiles[i-1].d_name, searchq, searchcs) != NULL) {
curwin.highlight = i-1;
return;
}
@@ -1540,6 +1541,7 @@ make_dir(void)
char name[PATH_MAX];
prompt_answer(name, PATH_MAX, "Name of directory? ");
+
return mkdir(name, 0755);
}
@@ -1556,6 +1558,7 @@ make_file(void)
fatal("Error opening file \"%s\"\n", name);
fclose(fptr);
}
+
return 0;
}
@@ -1564,7 +1567,12 @@ rm_file(const char *fname)
{
char *msg, *rmdirfmt = "rm -rf \"%s\"", *pfmt = "Remove %s? (y/N) ";
int res = 0;
- size_t cmdsize = PATH_MAX + strlen(rmdirfmt), psize = strlen(pfmt) + strlen(fname);
+ size_t cmdsize = PATH_MAX + strlen(rmdirfmt), psize;
+
+ if (fname == NULL)
+ return 0;
+
+ psize = strlen(pfmt) + strlen(fname);
if (prompt_confirm(psize, pfmt, fname)) {
if (is_dir(&curwin, curwin.highlight))
@@ -1598,6 +1606,9 @@ rename_file(const char *fname)
{
char name[PATH_MAX];
+ if (fname == NULL)
+ return 0;
+
prompt_answer(name, PATH_MAX, "New name? ");
if (strlen(name) > 0)
@@ -1612,6 +1623,9 @@ make_mod(void)
char name[4];
bool isvalid;
+ if (curwin.filecount == 0)
+ return 0;
+
prompt_answer(name, sizeof(name), "File mods (numeric)? ");
if (strlen(name) == 3) {
@@ -1638,6 +1652,9 @@ make_chown(void)
size_t len = maxlogin > 0 ? (size_t) maxlogin + 1 : 256;
char owner[len], group[len], *err;
+ if (curwin.filecount == 0)
+ return 0;
+
prompt_answer(owner, len, "Owner name? ");
if ((usr = getpwnam(owner)) == NULL) {
@@ -1666,6 +1683,7 @@ make_chown(void)
return errno;
}
}
+
return 0;
}
@@ -1684,11 +1702,22 @@ sudo_chown(char *owner, char *group, long namelen)
void
make_access(void)
{
- char pathbuf[PATH_MAX], *cmdfmt = "ln -s \"%s\" \"%s\"", *pfmt = "Make access \"%s\"? (y/N) ";
- size_t cmdsize = PATH_MAX + strlen(cmdfmt), psize = strlen(pfmt) + PATH_MAX;
+ char pathbuf[PATH_MAX], accessdir[PATH_MAX];
+ char *cmdfmt = "ln -s \"%s\" \"%s/\"", *pfmt = "Make access \"%s\"? (y/N) ";
+ size_t cmdsize = PATH_MAX*2 + strlen(cmdfmt), psize = strlen(pfmt) + PATH_MAX;
+
+ if (curwin.filecount == 0)
+ return;
+
+ if (!prompt_confirm(psize, pfmt, curwin.winfiles[curwin.highlight].d_name))
+ return;
+
+ if (mkdir(replace_home(accessdir, LN_ACCESS_DIR), 0755) != 0 && errno != EEXIST) {
+ print_error(256, "Could not create access directory: %s", strerror(errno));
+ return;
+ }
- if (prompt_confirm(psize, pfmt, curwin.winfiles[curwin.highlight].d_name))
- run_command(cmdsize, cmdfmt, get_fullpath(pathbuf, &curwin, curwin.highlight), LN_ACCESS_DIR);
+ run_command(cmdsize, cmdfmt, get_fullpath(pathbuf, &curwin, curwin.highlight), LN_ACCESS_DIR);
}
void
@@ -1697,8 +1726,11 @@ make_open_default(void)
int appsize = 256, qsize = MIME_MAX+25;
char mime[MIME_MAX], question[qsize], app[appsize];
+ if (curwin.filecount == 0)
+ return;
+
if (get_mime(mime, MIME_MAX, curwin.winfiles[curwin.highlight].d_name) != 0)
- cpstr(mime, "Can't retrieve mime.");
+ strcpy(mime, "Can't retrieve mime.");
snprintf(question, qsize, "Application for mime %s?", mime);
@@ -1756,6 +1788,9 @@ exe_selection(SelAction action, const char *askn)
void
select_file(const char *path)
{
+ if (path[0] == '\0')
+ return;
+
if (remove_selected(path))
return;
@@ -1784,7 +1819,6 @@ select_files(void)
for (int i = MIN(start_sel, curwin.highlight); i <= MAX(start_sel, curwin.highlight); i++)
select_file(get_fullpath(pathbuf, &curwin, i));
-
start_sel = -1;
}
@@ -1809,20 +1843,9 @@ clear_search(void)
}
char *
-cpstr(char *dest, const char *src)
+substr(const char *haystack, const char *needle, bool casesens)
{
- size_t cplen = strlen(src);
- memcpy(dest, src, cplen);
- dest[cplen] = '\0';
- return dest;
-}
-
-char *
-substr(char *buf, const char *haystack, const char *needle, const bool casesens)
-{
- buf = casesens ? strstr(haystack, needle) : strcasestr(haystack, needle);
-
- return buf;
+ return casesens ? strstr(haystack, needle) : strcasestr(haystack, needle);
}
char *
@@ -1849,20 +1872,29 @@ get_fullpath(char *buf, DirWin *dirwin, size_t index)
char *
replace_home(char *buf, const char *path)
{
- char *envv = "$HOME";
+ const char *envv = "$HOME";
+ int len;
- if (strncmp(path, envv, strlen(envv)) == 0
- && snprintf(buf, PATH_MAX, "%s%s", userhome,
- path + strlen(envv)) >= PATH_MAX)
+ if (strncmp(path, envv, strlen(envv)) == 0)
+ len = snprintf(buf, PATH_MAX, "%s%s", userhome,
+ path + strlen(envv));
+ else
+ len = snprintf(buf, PATH_MAX, "%s", path);
+
+ if (len < 0 || len >= PATH_MAX)
buf[0] = '\0';
+
return buf;
}
char *
get_dirname(char *buf, const char *path)
{
- cpstr(buf, path);
- dirname(buf);
+ char *parent = dirname(strcpy(buf, path));
+
+ if (parent != buf)
+ memmove(buf, parent, strlen(parent) + 1);
+
return buf;
}