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 "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) {