mirror of
https://github.com/troglobit/libite.git
synced 2026-10-09 16:50:43 +07:00
Add support for yorn(), safe yes-or-no prompt
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
+3
-2
@@ -14,7 +14,7 @@ libite_la_SOURCES = chomp.c copyfile.c \
|
||||
reallocarray.c rsync.c \
|
||||
strlcpy.c strlcat.c strtonum.c \
|
||||
strdupa.h strndupa.h strnlen.h \
|
||||
strmatch.c \
|
||||
strmatch.c yorn.c \
|
||||
tempfile.c which.c \
|
||||
lite.h strlite.h \
|
||||
queue.h tree.h
|
||||
@@ -22,4 +22,5 @@ libite_la_SOURCES = chomp.c copyfile.c \
|
||||
|
||||
## Distribute these header files in the <lite/...> name space
|
||||
pkgincludedir = $(includedir)/lite
|
||||
pkginclude_HEADERS = lite.h queue.h conio.h tree.h strlite.h strdupa.h strndupa.h strnlen.h
|
||||
pkginclude_HEADERS = lite.h queue.h conio.h tree.h strlite.h strdupa.h \
|
||||
strndupa.h strnlen.h
|
||||
|
||||
@@ -95,6 +95,8 @@ void *reallocarray(void *optr, size_t nmemb, size_t size);
|
||||
void progress (int percent, int max_width);
|
||||
void progress_simple(int percent);
|
||||
|
||||
int yorn (const char *fmt, ...);
|
||||
|
||||
char *which (const char *cmd);
|
||||
int whichp (const char *cmd);
|
||||
|
||||
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
/* Safe yes-or-no with prompt
|
||||
*
|
||||
* Copyright (c) 2009 Joachim Nilsson <troglobit@gmail.com>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdio_ext.h> /* __fpurge() */
|
||||
#include <stdarg.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static char rawgetch(void)
|
||||
{
|
||||
struct termios savemodes, modmodes;
|
||||
char val;
|
||||
|
||||
if (!isatty(STDIN_FILENO))
|
||||
return getchar();
|
||||
|
||||
/* Backup terminal settings. */
|
||||
if (tcgetattr(STDIN_FILENO, &savemodes) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* "stty cbreak -echo" */
|
||||
modmodes = savemodes;
|
||||
modmodes.c_lflag &= ~ICANON;
|
||||
modmodes.c_lflag &= ~ECHO;
|
||||
modmodes.c_cc[VMIN] = 1;
|
||||
modmodes.c_cc[VTIME] = 0;
|
||||
|
||||
/* Set terminal in raw mode. */
|
||||
if (tcsetattr(STDIN_FILENO, TCSANOW, &modmodes) < 0) {
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &savemodes);
|
||||
return -1;
|
||||
}
|
||||
|
||||
val = getchar();
|
||||
|
||||
/* Restore terminal to previous state. */
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &savemodes);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* yorn - Pose a a Yes or No question and return answer
|
||||
* @fmt: Standard printf() style argument(s).
|
||||
*
|
||||
* This function prints the given question on screen, waits for user
|
||||
* input in the form of yes or no.
|
||||
*
|
||||
* Returns:
|
||||
* True(1) or False(0). True if the answer is yes.
|
||||
*/
|
||||
int yorn(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
char yorn;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
__fpurge(stdin);
|
||||
yorn = rawgetch();
|
||||
printf("%c\n", yorn);
|
||||
fflush(stdout);
|
||||
if (yorn != 'y' && yorn != 'Y')
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Local Variables:
|
||||
* indent-tabs-mode: t
|
||||
* c-file-style: "linux"
|
||||
* End:
|
||||
*/
|
||||
Reference in New Issue
Block a user