commit 904ce83c3b73e531989d753a2fb3473266441dc3
parent 9c1071896127f15aea5df251ae35907b37404600
Author: Jacob R. Edwards <n/a>
Date: Mon, 7 Feb 2022 18:20:53 -0800
Add interactive confirmation to module program
Diffstat:
1 file changed, 25 insertions(+), 3 deletions(-)
diff --git a/lib/module.c b/lib/module.c
@@ -21,6 +21,7 @@
static int argtoo; /* Install given directories too, not just their contents. */
static int force; /* Install even if the target exists. */
static int (*action)(char *, char *); /* Install function (link, copy, or none). */
+static int check; /* Ask for confirmation instead of exiting. */
void
die(char *s)
@@ -98,6 +99,24 @@ none(char *a, char *b)
}
int
+confirm(char *a, char *b)
+{
+ char c, answer;
+
+ if (force)
+ return 1;
+ if (!check)
+ return 0;
+
+ fprintf(stderr, "%s -> %s? ", a, b);
+ answer = fgetc(stdin);
+ do
+ c = fgetc(stdin);
+ while (c != '\n' && c != EOF);
+ return answer == 'y' || answer == 'Y';
+}
+
+int
main(int argc, char *argv[])
{
FTS *fts;
@@ -112,7 +131,7 @@ main(int argc, char *argv[])
progname = *argv;
if (argc < 4) {
usage:
- fprintf(stderr, "usage: %s [force] action source ... target\n", progname);
+ fprintf(stderr, "usage: %s [force | check] action source ... target\n", progname);
return 1;
}
@@ -120,6 +139,9 @@ usage:
if (strcmp(*argv, "force") == 0) {
force = 1;
SHIFT(1);
+ } else if (strcmp(*argv, "check") == 0) {
+ check = 1;
+ SHIFT(1);
}
if (strcmp(argv[0], "copy") == 0)
@@ -181,9 +203,9 @@ usage:
else {
if (errno == ENOENT)
status = action(ent->fts_accpath, new);
- else if (force)
+ else if (confirm(ent->fts_accpath, new))
status = (unlink(new) || action(ent->fts_accpath, new));
- else if (action != none) {
+ else if (!check && action != none) {
errno = EEXIST;
status = 1;
}