diff --git a/cli.c b/cli.c index 4139bc3..c758859 100644 --- a/cli.c +++ b/cli.c @@ -26,8 +26,6 @@ #include "strtonum.h" #include "otp.h" -#define LINE_SIZE 16384 - extern const char *__progname; static void @@ -44,8 +42,9 @@ int main(int argc, char *argv[]) { const char *errstr; - char *key; - size_t key_len; + char *key, *line; + size_t key_len, linesz; + ssize_t linelen; uint64_t counter; unsigned int step; int32_t r; @@ -110,16 +109,17 @@ main(int argc, char *argv[]) key = argv[0]; key_len = strlen(key); } else { - key = malloc(sizeof(uint8_t) * (LINE_SIZE + 1)); - if (key == NULL) - err(1, "malloc"); - if (fgets(key, LINE_SIZE + 1, stdin) == NULL) - err(1, "fgets"); - key_len = strlen(key); - if (key[key_len - 1] == '\n') { - key[key_len - 1] = '\0'; - key_len--; + line = NULL; + linesz = 0; + linelen = getline(&line, &linesz, stdin); + if (linelen == -1 && ferror(stdin)) + err(1, "getline"); + if (linelen > 0 && line[linelen - 1] == '\n') { + line[linelen - 1] = '\0'; + linelen--; } + key = line; + key_len = linelen; } if (do_hotp) {