diff options
author | Duncaen <mail@duncano.de> | 2017-02-23 20:47:38 +0100 |
---|---|---|
committer | Duncaen <mail@duncano.de> | 2017-02-23 20:47:38 +0100 |
commit | 8cd560fd4225f539174cb8dcf7bd482312d530d8 (patch) | |
tree | 6e26726875cc730810c1e72b41023996424dbed0 | |
parent | d3f14eaa55df4e33f564b9c8b09fc1d887999964 (diff) | |
download | mblaze-8cd560fd4225f539174cb8dcf7bd482312d530d8.tar.gz mblaze-8cd560fd4225f539174cb8dcf7bd482312d530d8.tar.xz mblaze-8cd560fd4225f539174cb8dcf7bd482312d530d8.zip |
magrep: use printf string precision to print matches instead of strndup
-rw-r--r-- | magrep.c | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/magrep.c b/magrep.c index 0804071..6acb0a8 100644 --- 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)) { |