From ba33d2184e4c975fd794ab85ccc66df65971fb77 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Mon, 15 Jun 2020 15:20:30 -0400 Subject: robust wsadd --- badroff.c | 23 ++++++++++++----------- 1 file 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); -- cgit