When not reading from a tty use getline().

This commit is contained in:
Nikos Mavrogiannopoulos
2014-01-30 19:22:02 +01:00
parent aeb0b40221
commit 313f1b67fd
+19 -7
View File
@@ -277,7 +277,8 @@ int main(int argc, char **argv)
{
int ret, optct;
const char *username, *groupname, *fpasswd;
const char* passwd;
char* passwd;
size_t l, i;
if ((ret = gnutls_global_init()) < 0) {
fprintf(stderr, "global_init: %s\n", gnutls_strerror(ret));
@@ -315,15 +316,16 @@ int main(int argc, char **argv)
unlock_user(fpasswd, username);
else { /* set password */
passwd = getpass("Enter password: ");
if (passwd == NULL) {
fprintf(stderr, "Please specify a password\n");
exit(1);
}
if (isatty(STDIN_FILENO)) {
char* p2;
passwd = getpass("Enter password: ");
if (passwd == NULL) {
fprintf(stderr, "Please specify a password\n");
exit(1);
}
p2 = strdup(passwd);
passwd = getpass("Re-enter password: ");
if (passwd == NULL) {
@@ -336,6 +338,16 @@ int main(int argc, char **argv)
exit(1);
}
free(p2);
} else {
passwd = NULL;
l = getline(&passwd, &i, stdin);
if (l <= 1) {
fprintf(stderr, "Please specify a password\n");
exit(1);
}
if (passwd[l-1] == '\n')
passwd[l-1] = 0;
}
crypt_int(fpasswd, username, groupname, passwd);