aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHolden Rohrer <hr@hrhr.dev>2020-06-15 15:20:30 -0400
committerHolden Rohrer <hr@hrhr.dev>2020-06-15 15:20:30 -0400
commitba33d2184e4c975fd794ab85ccc66df65971fb77 (patch)
tree2533162255037e92d042e7ba5d6fff6139379d4f
parent0673f87892e799437f25b5ba304073a15f5143f7 (diff)
robust wsadd
-rw-r--r--badroff.c23
1 files changed, 12 insertions, 11 deletions
diff --git a/badroff.c b/badroff.c
index 2f66b29..25d89d2 100644
--- a/badroff.c
+++ b/badroff.c
@@ -338,17 +338,18 @@ static void nbspclean(char* txt){
}
}
-static char* wsadd(char* txt, int lnct) {
- int len = strlen(txt) + lnct*indlevel;
- txt = realloc(txt, sizeof(char)*(len+1));
- char* end = txt + len;
- char *nl, *last; nl = last = txt;
- while ( (nl = memchr(nl, '\n', end-nl))[1] ) {
- memmove(last+indlevel, last, nl-last);
- memset(last, ' ', indlevel);
- last = nl+1;
+static void wsadd(char** txt, int lnct) {
+ int len = strlen(*txt);
+ *txt = realloc(*txt, sizeof(char)*(len + lnct*indlevel + 1));
+ char* nl = *txt;
+ while (*nl) {
+ nl += indlevel;
+ memmove(nl, nl-indlevel, len+1);
+ memset(nl-indlevel, ' ', indlevel);
+ char* last = nl;
+ nl = (char*)memchr(nl, '\n', len)+1;
+ len -= nl-last;
}
- return txt;
}
char* wordset(char* txt){
@@ -367,7 +368,7 @@ char* wordset(char* txt){
}
}
- orig = wsadd(orig, ln);
+ wsadd(&orig, ln);
wsclean(orig); // remove trailing whitespace
nbspclean(orig);