Replace last use of fgetln with getline, GitHub issue 5389 from Yayo Razo.

This commit is contained in:
nicm
2026-07-13 19:35:22 +00:00
committed by tmux update bot
parent fba5083b58
commit b1c347ee0c

View File

@@ -1,4 +1,4 @@
/* $OpenBSD: prompt-history.c,v 1.1 2026/06/25 11:39:11 nicm Exp $ */ /* $OpenBSD: prompt-history.c,v 1.2 2026/07/13 19:35:22 nicm Exp $ */
/* /*
* Copyright (c) 2026 Nicholas Marriott <nicholas.marriott@gmail.com> * Copyright (c) 2026 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -79,8 +79,9 @@ void
prompt_load_history(void) prompt_load_history(void)
{ {
FILE *f; FILE *f;
char *history_file, *line, *tmp; char *history_file, *line = NULL;
size_t length; size_t length = 0;
ssize_t got;
if ((history_file = prompt_find_history_file()) == NULL) if ((history_file = prompt_find_history_file()) == NULL)
return; return;
@@ -94,23 +95,13 @@ prompt_load_history(void)
} }
free(history_file); free(history_file);
for (;;) { while ((got = getline(&line, &length, f)) != -1) {
if ((line = fgetln(f, &length)) == NULL) if (got > 0 && line[got - 1] == '\n')
break; line[got - 1] = '\0';
if (got > 0)
if (length > 0) { prompt_add_typed_history(line);
if (line[length - 1] == '\n') {
line[length - 1] = '\0';
prompt_add_typed_history(line);
} else {
tmp = xmalloc(length + 1);
memcpy(tmp, line, length);
tmp[length] = '\0';
prompt_add_typed_history(tmp);
free(tmp);
}
}
} }
free(line);
fclose(f); fclose(f);
} }