From b5ad306ac967db3f3dace48952437b7729d31087 Mon Sep 17 00:00:00 2001 From: Alec Lanter Date: Fri, 28 Apr 2017 08:17:40 -0500 Subject: Updated ykfde.c to silently skip terminal updates when tcgetattr fails so that keys can be piped through from other commands. --- bin/ykfde.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/bin/ykfde.c b/bin/ykfde.c index 37e82a3..cc69636 100644 --- a/bin/ykfde.c +++ b/bin/ykfde.c @@ -64,32 +64,38 @@ char * ask_secret(const char * text) { char * factor = NULL; size_t len; ssize_t readlen; + bool onTerminal = true; /* get terminal properties */ if (tcgetattr(STDIN_FILENO, &tp) < 0) { - fprintf(stderr, "Failed reading terminal attributes. Not on terminal?\n"); - return NULL; + onTerminal = false; + tp_save = tp; } - tp_save = tp; /* disable echo on terminal */ - tp.c_lflag &= ~ECHO; - if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tp) < 0) { - fprintf(stderr, "Failed setting terminal attributes.\n"); - return NULL; + if (onTerminal) { + tp.c_lflag &= ~ECHO; + if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tp) < 0) { + fprintf(stderr, "Failed setting terminal attributes.\n"); + return NULL; + } + + printf("Please give %s:", text); } - printf("Please give %s:", text); readlen = getline(&factor, &len, stdin); factor[readlen - 1] = '\0'; - putchar('\n'); - /* restore terminal */ - if (tcsetattr(STDIN_FILENO, TCSANOW, &tp_save) < 0) { - fprintf(stderr, "Failed setting terminal attributes.\n"); - free(factor); - return NULL; + if (onTerminal) { + putchar('\n'); + + /* restore terminal */ + if (tcsetattr(STDIN_FILENO, TCSANOW, &tp_save) < 0) { + fprintf(stderr, "Failed setting terminal attributes.\n"); + free(factor); + return NULL; + } } return factor; -- cgit v1.2.3-70-g09d2