mblaze

Unix utilities to deal with Maildir - my mirror
git clone https://pi.duncano.de/git/mblaze.git
Log | Files | Refs | README | COPYING

commit 8cd560fd4225f539174cb8dcf7bd482312d530d8
parent d3f14eaa55df4e33f564b9c8b09fc1d887999964
Author: Duncaen <mail@duncano.de>
Date:   Thu, 23 Feb 2017 20:47:38 +0100

magrep: use printf string precision to print matches instead of strndup

Diffstat:
magrep.c | 13+++++--------
1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/magrep.c b/magrep.c @@ -31,22 +31,19 @@ match(char *file, char *hdr, char *s) { if (oflag && !cflag && !qflag && !vflag) { regmatch_t pmatch; - size_t sublen, matched; - char *substr; + int len, matched; matched = 0; while (*s && regexec(&pattern, s, 1, &pmatch, 0) == 0) { s += pmatch.rm_so; - if (!(sublen = pmatch.rm_eo-pmatch.rm_so)) { + if (!(len = pmatch.rm_eo-pmatch.rm_so)) { s += 1; continue; } - matched++; - substr = strndup(s, sublen); - s += sublen; if (pflag) printf("%s: %s: ", file, hdr); - printf("%s\n", substr); - free(substr); + printf("%.*s\n", len, s); + s += len; + matched++; } return (matched && matches++); } else if (vflag ^ (regexec(&pattern, s, 0, 0, 0) == 0)) {