logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: c49f4e6caf8610b51b4b475023bc5a28e732d569
parent 4c21c817b83edddf81a3b34f42bba156062de792
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue,  5 Nov 2024 21:28:22 +0100

cmd/wc: move wc_opts matching outside of loop

Diffstat:

Mcmd/wc.c15+++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/cmd/wc.c b/cmd/wc.c @@ -65,15 +65,13 @@ wc_file_bytes(int fd, char *filename) { off_t bytes = 0, lines = 0, words = 0, wordlen = 0; - int lw = FIELD_MATCH(wc_opts, WC_OPT_L) || FIELD_MATCH(wc_opts, WC_OPT_W); - ssize_t nread = -1; - while((nread = read(fd, buf, WC_BUFSIZ)) > 0) + if(FIELD_MATCH(wc_opts, WC_OPT_L) || FIELD_MATCH(wc_opts, WC_OPT_W)) { - bytes += nread; - - if(lw) + while((nread = read(fd, buf, WC_BUFSIZ)) > 0) { + bytes += nread; + for(ssize_t i = 0; i < nread; i++) { int c = buf[i]; @@ -95,6 +93,11 @@ wc_file_bytes(int fd, char *filename) } } } + else + { + while((nread = read(fd, buf, WC_BUFSIZ)) > 0) + bytes += nread; + } if(nread < 0 && errno != 0) {