commit 187065c549a5915e527065e32ced7c04be0ae985
parent 5a505f7af6a835b2df4bfaa3bfe8cbd66fcf7e2d
Author: Wim Dupont <wim@wimdupont.com>
Date: Thu, 25 Jul 2024 22:22:30 +0200
fix prompt args
Diffstat:
M | cex.c | | | 43 | +++++++++++++++++++++---------------------- |
M | cex.h | | | 4 | ++-- |
2 files changed, 23 insertions(+), 24 deletions(-)
diff --git a/cex.c b/cex.c
@@ -1079,17 +1079,13 @@ is_dir(DirWin *dirwin, size_t index)
}
static char *
-prompt_answer(char *buf, size_t size, const char *fmt, ...)
+prompt_answer(char *buf, size_t size, const char *question)
{
- va_list ap;
-
move(maxy-1, 0);
clrtoeol();
attron(COLOR_PAIR(PROMPT_COLOR));
- va_start(ap, fmt);
- vw_printw(stdscr, fmt, ap);
- va_end(ap);
+ printw(question);
attroff(COLOR_PAIR(PROMPT_COLOR));
echo();
@@ -1102,15 +1098,17 @@ prompt_answer(char *buf, size_t size, const char *fmt, ...)
}
static bool
-prompt_confirm(const char *fmt, ...)
+prompt_confirm(size_t size, const char *fmt, ...)
{
- char response[1];
+ char response[1], question[size];
va_list ap;
va_start(ap, fmt);
- prompt_answer(response, 1, fmt, ap);
+ vsnprintf(question, size, fmt, ap);
va_end(ap);
+ prompt_answer(response, 1, question);
+
return strcasecmp(response, "y") == 0;
}
@@ -1214,13 +1212,13 @@ make_file(const char *path)
static int
rm_file(const char *fname)
{
- char *msg, *rmdirfmt = "rm -rf \"%s\"";
+ char *msg, *rmdirfmt = "rm -rf \"%s\"", *pfmt = "Remove %s? (y/N) ";
int res = 0;
- size_t size = PATH_MAX + strlen(rmdirfmt);
+ size_t cmdsize = PATH_MAX + strlen(rmdirfmt), psize = strlen(pfmt) + strlen(fname);
- if (prompt_confirm("Remove %s? (y/N) ", fname)) {
+ if (prompt_confirm(psize, pfmt, fname)) {
if (is_dir(&curwin, curwin.highlight))
- run_command(size, rmdirfmt, fname);
+ run_command(cmdsize, rmdirfmt, fname);
else
res = remove(fname);
curwin.highlight = MAX(curwin.highlight-1, 0);
@@ -1287,36 +1285,37 @@ make_mod()
static int
make_access()
{
- char pathbuf[PATH_MAX], *cmdfmt = "ln -s \"%s\" \"%s\"";
- size_t size = PATH_MAX + strlen(cmdfmt);
+ 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;
- if (prompt_confirm("Make access \"%s\"? (y/N) ", curwin.winfiles[curwin.highlight].d_name))
- run_command(size, cmdfmt, get_fullpath(pathbuf, &curwin, curwin.highlight), LN_ACCESS_DIR);
+ if (prompt_confirm(psize, pfmt, curwin.winfiles[curwin.highlight].d_name))
+ run_command(cmdsize, cmdfmt, get_fullpath(pathbuf, &curwin, curwin.highlight), LN_ACCESS_DIR);
}
static void
exe_selection(SelAction action, const char *askn)
{
- size_t size = PATH_MAX*2 + 20;
+ char *pfmt = "%s selection (%d files) ? (y/N) ";
+ size_t cmdsize = PATH_MAX*2 + 20, psize = strlen(pfmt) + strlen(askn) + 5;
if (selected == NULL || selc == 0)
return;
- if (askn != NULL && !prompt_confirm("%s selection (%d files) ? (y/N) ", askn, selc))
+ if (askn != NULL && !prompt_confirm(psize, pfmt, askn, selc))
return;
switch (action) {
case COPY:
for (size_t i = 0; i <selc; i++)
- run_command(size, "cp -rf \"%s\" \"%s\"", selected[i], curwin.path);
+ run_command(cmdsize, "cp -rf \"%s\" \"%s\"", selected[i], curwin.path);
break;
case REMOVE:
for (size_t i = 0; i <selc; i++)
- run_command(size, "rm -rf \"%s\"", selected[i]);
+ run_command(cmdsize, "rm -rf \"%s\"", selected[i]);
break;
case MOVE:
for (size_t i = 0; i <selc; i++)
- run_command(size, "mv \"%s\" \"%s\"", selected[i], curwin.path);
+ run_command(cmdsize, "mv \"%s\" \"%s\"", selected[i], curwin.path);
break;
default:
break;
diff --git a/cex.h b/cex.h
@@ -130,7 +130,7 @@ 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(const char *sel);
-static bool prompt_confirm(const char *fmt, ...);
+static bool prompt_confirm(size_t size, const char *fmt, ...);
static int compare_file(const void *a, const void *b);
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);
@@ -141,7 +141,7 @@ static int make_mod();
static int make_access();
static int rm_file(const char *fname);
static int rename_file();
-static char *prompt_answer(char *buf, size_t size, const char *fmt, ...);
+static char *prompt_answer(char *buf, size_t size, const char *question);
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);