Use getline

This commit is contained in:
Lucas 2021-02-11 23:13:08 +00:00
parent dde97e45d0
commit a51a5b5b0d
1 changed files with 13 additions and 13 deletions

26
cli.c
View File

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