commit 8d07e5e8f639f3fabb45379786c9225f7312e6e5
parent 8e18687849ad59c958becd5d9d4dbce462c34e57
Author: Thibaut Aubin <t.aubin20@ejm.org>
Date:   Thu, 10 Apr 2025 00:00:00 +0000
libutil: add confirm() prompt
Diffstat:
4 files changed, 27 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
@@ -54,6 +54,7 @@ LIBUTILOBJ =\
 	libutil/concat.o\
 	libutil/cp.o\
 	libutil/crypt.o\
+	libutil/confirm.o\
 	libutil/ealloc.o\
 	libutil/enmasse.o\
 	libutil/eprintf.o\
diff --git a/libutil/confirm.c b/libutil/confirm.c
@@ -0,0 +1,22 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdarg.h>
+#include <ctype.h>
+
+#include "../util.h"
+
+int
+confirm(const char *fmt, ...)
+{
+	int c, ans;
+	va_list ap;
+
+	va_start(ap, fmt);
+	xvprintf(fmt, ap);
+	va_end(ap);
+
+	c = getchar();
+	ans = (c == 'y' || c == 'Y');
+	while (c != '\n' && c != EOF)
+		c = getchar();
+	return ans;
+}
diff --git a/libutil/eprintf.c b/libutil/eprintf.c
@@ -8,8 +8,6 @@
 
 char *argv0;
 
-static void xvprintf(const char *, va_list);
-
 void
 eprintf(const char *fmt, ...)
 {
diff --git a/util.h b/util.h
@@ -4,6 +4,7 @@
 #include <regex.h>
 #include <stddef.h>
 #include <stdio.h>
+#include <stdarg.h>
 
 #include "arg.h"
 #include "compat.h"
@@ -43,6 +44,9 @@ int  fshut(FILE *, const char *);
 void enprintf(int, const char *, ...);
 void eprintf(const char *, ...);
 void weprintf(const char *, ...);
+void xvprintf(const char *, va_list);
+
+int confirm(const char*, ...);
 
 double estrtod(const char *);