Skip to content

Commit 3379324

Browse files
committed
Buffer and print also what follows the last newline
_print() saves in cli->buffer whatever follows the last newline, but then just throws it away on the next call. Use cli->buffer by copying it in front of the new input.
1 parent 0acad90 commit 3379324

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

libcli.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,11 +1856,23 @@ static void _print(struct cli_def *cli, int print_mode, const char *format, va_l
18561856

18571857
n = vasprintf(&p, format, ap);
18581858
if (n < 0) return;
1859-
if (cli->buffer) free(cli->buffer);
1860-
cli->buffer = p;
1861-
cli->buf_size = n;
1859+
if (cli->buffer) {
1860+
int len = strlen(cli->buffer);
1861+
unsigned size = len + n + 1;
1862+
1863+
if (size > cli->buf_size) {
1864+
char *buf = realloc(cli->buffer, size);
1865+
if (!buf) return;
1866+
cli->buffer = buf;
1867+
cli->buf_size = size;
1868+
}
18621869

1863-
p = cli->buffer;
1870+
memcpy(cli->buffer + len, p, n);
1871+
free(p);
1872+
*(cli->buffer + len + n) = 0;
1873+
p = cli->buffer;
1874+
}
1875+
cli->buffer = p;
18641876
do {
18651877
char *next = strchr(p, '\n');
18661878
struct cli_filter *f = (print_mode & PRINT_FILTERED) ? cli->filters : 0;
@@ -1886,7 +1898,7 @@ static void _print(struct cli_def *cli, int print_mode, const char *format, va_l
18861898
} while (p);
18871899

18881900
if (p && *p) {
1889-
if (p != cli->buffer) memmove(cli->buffer, p, strlen(p));
1901+
if (p != cli->buffer) memmove(cli->buffer, p, strlen(p) + 1);
18901902
} else
18911903
*cli->buffer = 0;
18921904
}

0 commit comments

Comments
 (0)