summaryrefslogtreecommitdiffstats
path: root/lib/yesno.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 16:18:36 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-26 16:18:36 +0000
commit6c3ea4f47ea280811a7fe53a22f7832e4533c9ec (patch)
tree3d7ed5da23b5dbf6f9e450dfb61642832249c31e /lib/yesno.c
parentAdding upstream version 1:4.13+dfsg1. (diff)
downloadshadow-6c3ea4f47ea280811a7fe53a22f7832e4533c9ec.tar.xz
shadow-6c3ea4f47ea280811a7fe53a22f7832e4533c9ec.zip
Adding upstream version 1:4.15.2.upstream/1%4.15.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/yesno.c')
-rw-r--r--lib/yesno.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/lib/yesno.c b/lib/yesno.c
new file mode 100644
index 0000000..029cd81
--- /dev/null
+++ b/lib/yesno.c
@@ -0,0 +1,87 @@
+/*
+ * SPDX-FileCopyrightText: 1992 - 1994, Julianne Frances Haugh
+ * SPDX-FileCopyrightText: 2007 - 2008, Nicolas François
+ * SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+
+#include <config.h>
+
+#ident "$Id$"
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "prototypes.h"
+
+
+/*
+ * Synopsis
+ * bool yes_or_no(bool read_only);
+ *
+ * Arguments
+ * read_only
+ * In read-only mode, all questions are answered "no". It
+ * will print "No" to stdout.
+ *
+ * Description
+ * After a yes/no question, this function gets the answer from the
+ * user.
+ *
+ * Calls to this function will normally be preceeded by a prompt on
+ * stdout, so we should fflush(3).
+ *
+ * Return value
+ * false "no"
+ * true "yes"
+ *
+ * See also
+ * rpmatch(3)
+ */
+
+
+#if !defined(HAVE_RPMATCH)
+static int rpmatch(const char *response);
+#endif
+
+
+bool
+yes_or_no(bool read_only)
+{
+ bool ret;
+ char *buf;
+ size_t size;
+
+ if (read_only) {
+ puts(_("No"));
+ return false;
+ }
+
+ fflush(stdout);
+
+ buf = NULL;
+ ret = false;
+ size = 0;
+ if (getline(&buf, &size, stdin) != -1)
+ ret = rpmatch(buf) == 1;
+
+ free(buf);
+ return ret;
+}
+
+
+#if !defined(HAVE_RPMATCH)
+static int
+rpmatch(const char *response)
+{
+ if (response[0] == 'y' || response[0] == 'Y')
+ return 1;
+
+ if (response[0] == 'n' || response[0] == 'n')
+ return 0;
+
+ return -1;
+}
+#endif