If getline fails, errno might be overridden by isatty, leading a wrong
error message.
Also, getline returns -1 on EOF. In which case errno is not set. Use a
custom error message and still handle this as an error condition.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
const int fd = fileno(input);
size_t dummy = 0;
ssize_t len;
+ int save_errno;
fputs(prompt, stdout);
if (isatty(fd)) {
err(EXIT_FAILURE, _("could not set terminal attributes"));
}
len = getline(&pass, &dummy, input);
+ save_errno = errno;
if (isatty(fd))
/* restore terminal */
if (tcsetattr(fd, TCSANOW, &saved))
err(EXIT_FAILURE, _("could not set terminal attributes"));
- if (len < 0)
+ if (len < 0) {
+ if (feof(input))
+ errx(EXIT_FAILURE, _("failed to read password"));
+ errno = save_errno;
err(EXIT_FAILURE, _("getline() failed"));
+ }
if (0 < len && *(pass + len - 1) == '\n')
*(pass + len - 1) = '\0';
return pass;