libutil: add confirm() prompt - sbase - suckless unix tools
 (HTM) git clone git://git.suckless.org/sbase
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 8d07e5e8f639f3fabb45379786c9225f7312e6e5
 (DIR) parent 8e18687849ad59c958becd5d9d4dbce462c34e57
 (HTM) Author: Thibaut Aubin <t.aubin20@ejm.org>
       Date:   Thu, 10 Apr 2025 00:00:00 +0000
       
       libutil: add confirm() prompt
       
       Diffstat:
         M Makefile                            |       1 +
         A libutil/confirm.c                   |      22 ++++++++++++++++++++++
         M libutil/eprintf.c                   |       2 --
         M util.h                              |       4 ++++
       
       4 files changed, 27 insertions(+), 2 deletions(-)
       ---
 (DIR) 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\
 (DIR) 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;
       +}
 (DIR) 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, ...)
        {
 (DIR) 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 *);