From c16171349cff9798b113a4e17e65600496240626 Mon Sep 17 00:00:00 2001 From: Matheus Afonso Martins Moreira Date: Thu, 2 Nov 2023 06:07:44 -0300 Subject: [PATCH] vcscanf: define BUFFER macro This is similar to the READ macro. READs a character and places it in the buffer. Automatically maintains the NUL terminator. If the end is reached, reallocate the buffer. --- libc/stdio/vcscanf.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/libc/stdio/vcscanf.c b/libc/stdio/vcscanf.c index e0b24bfd1..49f2bed59 100644 --- a/libc/stdio/vcscanf.c +++ b/libc/stdio/vcscanf.c @@ -34,6 +34,21 @@ c; \ }) +#define BUFFER_GROW 48 +#define BUFFER \ + ({ \ + int c = READ; \ + if (bufcur >= bufsize - 1) { \ + bufsize = bufsize + BUFFER_GROW; \ + buf = realloc(buf, bufsize); \ + } \ + if (c != -1) { \ + buf[bufcur++] = c; \ + buf[bufcur] = '\0'; \ + } \ + c; \ + }) + /** * String / file / stream decoder. *