add a new hidden flag --stdin-input for straight stdin input

This commit is contained in:
Klas Lindfors
2017-04-06 15:57:09 +02:00
parent d17aff4e0d
commit e6a7517050
4 changed files with 54 additions and 33 deletions
+13 -1
View File
@@ -457,7 +457,7 @@ bool prepare_rsa_signature(const unsigned char *in, unsigned int in_len, unsigne
return true;
}
bool read_pw(const char *name, char *pwbuf, size_t pwbuflen, int verify) {
bool read_pw(const char *name, char *pwbuf, size_t pwbuflen, int verify, int stdin_input) {
#define READ_PW_PROMPT_BASE "Enter %s: "
char prompt[sizeof(READ_PW_PROMPT_BASE) + 32] = {0};
int ret;
@@ -467,6 +467,18 @@ bool read_pw(const char *name, char *pwbuf, size_t pwbuflen, int verify) {
return false;
}
if(stdin_input) {
fprintf(stdout, "%s\n", name);
if(fgets(pwbuf, pwbuflen, stdin)) {
if(pwbuf[strlen(pwbuf) - 1] == '\n') {
pwbuf[strlen(pwbuf) - 1] = '\0';
}
return true;
} else {
return false;
}
}
ret = snprintf(prompt, sizeof(prompt), READ_PW_PROMPT_BASE, name);
if (ret < 0 || ((unsigned int) ret) > (sizeof(prompt)-1)) {
fprintf(stderr, "Failed to read %s: snprintf failed.\n", name);