libutil: add confirm() prompt

This commit is contained in:
Thibaut Aubin 2025-04-10 00:00:00 +00:00 committed by Roberto E. Vargas Caballero
parent 8e18687849
commit 8d07e5e8f6
4 changed files with 27 additions and 2 deletions

View File

@ -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\

22
libutil/confirm.c Normal file
View File

@ -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;
}

View File

@ -8,8 +8,6 @@
char *argv0;
static void xvprintf(const char *, va_list);
void
eprintf(const char *fmt, ...)
{

4
util.h
View File

@ -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 *);