1
0
Fork 0
forked from soccera/rdo

Use the correct file descriptor for printing

Before we used printf("\n"); to simulate the enter key the user pressed,
which prints to normal stdout, which could have been redirected.
Now we directly write to the found TTY.

We also close the TTY file descriptor on every code path now.
This commit is contained in:
sw1tchbl4d3 2022-05-06 08:13:01 +02:00
parent 66d15caad4
commit fd4ee6a712

View file

@ -39,23 +39,25 @@ char* readpassphrase(const char* prompt, char* buf, size_t bufsz) {
if (write(fd, prompt, strlen(prompt)) < 0) {
tcsetattr(ttyfd, 0, &term);
close(fd);
return NULL;
}
n = read(fd, buf, bufsz);
if (n < 0) {
tcsetattr(ttyfd, 0, &term);
printf("\n");
n = write(fd, "\n", 1);
close(fd);
return NULL;
}
buf[n-1] = '\0';
// NOTE: As we disabled echo, the enter sent by the user isn't displayed, so we resend it.
n = write(fd, "\n", 1);
close(fd);
tcsetattr(ttyfd, 0, &term);
// NOTE: As we disabled echo, the enter sent by the user isn't displayed.
printf("\n");
return buf;
}