From: Ralph Corderoy Date: Mon, 31 Oct 2016 18:18:07 +0000 (+0000) Subject: mhbuild's fgetstr(): simplify source, logic untouched. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/f139ce94a95fd895d8b4b74826660ab076ed5129?hp=36c39624a208467bf2bd1d7155b85edcfc5b07ae mhbuild's fgetstr(): simplify source, logic untouched. The tests aren't getting 100% coverage and the nested assignments make it harder to see what's happening, and what needs to happen to gain coverage. Hoist invariants so it's clear they're such to the reader and not just the compiler. --- diff --git a/uip/mhbuildsbr.c b/uip/mhbuildsbr.c index fc90dc01..df346fec 100644 --- a/uip/mhbuildsbr.c +++ b/uip/mhbuildsbr.c @@ -612,23 +612,26 @@ static char * fgetstr (char *s, int n, FILE *stream) { char *cp, *ep; - int o_n = n; + ep = s + n; while(1) { - for (ep = (cp = s) + o_n; cp < ep; ) { - int i; + for (cp = s; cp < ep;) { + int len; if (!fgets (cp, n, stream)) - return (cp != s ? s : NULL); + return cp == s ? NULL : s; /* "\\\nEOF" ignored. */ if (cp == s && *cp != '#') - return s; + return s; /* Plaintext line. */ - cp += (i = strlen (cp)) - 1; - if (i <= 1 || *cp-- != '\n' || *cp != '\\') + len = strlen(cp); + if (len <= 1) + break; /* Can't contain "\\\n". */ + cp += len - 1; /* Just before NUL. */ + if (*cp-- != '\n' || *cp != '\\') break; - *cp = '\0'; - n -= (i - 2); + *cp = '\0'; /* Erase the trailing "\\\n". */ + n -= (len - 2); } if (strcmp(s, "#on\n") == 0) { @@ -638,11 +641,9 @@ fgetstr (char *s, int n, FILE *stream) } else if (strcmp(s, "#pop\n") == 0) { directive_pop(); } else { - break; + return s; } } - - return s; }