This makes possible to use the function to initialize the string from
an existing char array.
---
ed.c | 30 +++++++++++++++++++++---------
1 file changed, 21 insertions(+), 9 deletions(-)
diff --git a/ed.c b/ed.c
index b430e74..13e956a 100644
--- a/ed.c
+++ b/ed.c
@@ -122,12 +122,24 @@ prevln(int line)
}
static String *
-string(String *s)
+string(String *s, char *from)
{
+ size_t len;
+ char *t;
+
+ if (!from) {
+ len = 0;
+ t = NULL;
+ } else {
+ if ((t = strdup(from)) == NULL)
+ error("out of memory");
+ len = strlen(t);
+ }
+
free(s->str);
- s->str = NULL;
- s->siz = 0;
- s->cap = 0;
+ s->str = t;
+ s->siz = len;
+ s->cap = len;
return s;
}
@@ -949,7 +961,7 @@ join(void)
char *t, c;
static String s;
- string(&s);
+ string(&s, NULL);
for (i = line1;; i = nextln(i)) {
chksignals();
for (t = gettxt(i); (c = *t) != '\n'; ++t)
@@ -1014,7 +1026,7 @@ execsh(void)
skipblank();
if ((c = input()) != '!') {
back(c);
- string(&cmd);
+ string(&cmd, NULL);
} else if (cmd.siz) {
--cmd.siz;
repl = 1;
@@ -1048,7 +1060,7 @@ getrhs(int delim)
int c;
static String s;
- string(&s);
+ string(&s, NULL);
while ((c = input()) != '\n' && c != EOF && c != delim)
addchar(c, &s);
addchar('\0', &s);
@@ -1152,7 +1164,7 @@ subline(int num, int nth)
int i, m, changed;
static String s;
- string(&s);
+ string(&s, NULL);
i = changed = 0;
for (m = match(num); m; m = rematch(num)) {
chksignals();
@@ -1456,7 +1468,7 @@ doglobal(void)
int cnt, ln, k;
skipblank();
- string(&cmdline);
+ string(&cmdline, NULL);
gflag = 1;
if (uflag)
chkprint(0);
--
2.37.3