Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated ykfde.c to silently skip terminal updates when tcgetattr... #14

Merged
merged 1 commit into from
Apr 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions bin/ykfde.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down