commit a666c0f736069129c42e9235a2758406aea969f6
parent 6431c14bb25ce63732f326bd8791c8cc58cbb234
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Thu, 3 Dec 2015 11:28:47 +0100
Use addchar() in readcmd()
Don't do twice the same thing!
Diffstat:
M | ed.c | | | 27 | ++++++++++----------------- |
1 file changed, 10 insertions(+), 17 deletions(-)
diff --git a/ed.c b/ed.c
@@ -13,7 +13,6 @@
#include <stdlib.h>
#include <string.h>
-#define CMDSIZE 100
#define REGEXSIZE 100
#define LINESIZE 80
#define NUMLINES 32
@@ -919,11 +918,13 @@ static void
readcmd(int isglobal)
{
static char *buf;
- static size_t size;
- char *s, *p;
+ static size_t size, cap;
int c;
- if (isglobal && (c = getchar()) == '&') {
+ if (!isglobal) {
+ if (optprompt)
+ fputs(prompt, stdout);
+ } else if ((c = getchar()) == '&') {
/* FIXME: this code can let garbage in the input stream */
if (getchar() != '\n')
error("Unknown command");
@@ -931,24 +932,16 @@ readcmd(int isglobal)
return;
}
- for (s = buf;; *s++ = c) {
- if (optprompt)
- fputs(prompt, stdout);
- if (!buf || s == buf + size - 2) {
- if (size > SIZE_MAX - CMDSIZE ||
- !(p = realloc(buf, size + CMDSIZE)))
- error("out of memory");
- buf = p;
- s = !size ? p : p + size - 2;
- size = size+CMDSIZE;
- }
+ size = 0;
+ for (;;) {
if ((c = getchar()) == EOF || c == '\n')
break;
if (c == '\\' && (c = getchar()) == EOF)
break;
+ buf = addchar(c, buf, &cap, &size);
}
- *s++ = '\n';
- *s = '\0';
+ buf = addchar('\n', buf, &cap, &size);
+ buf = addchar('\0', buf, &cap, &size);
cmdp = buf;
}