[hackers] [sbase] Fix wc(1) output for large files || FRIGN

From: <git_AT_suckless.org>
Date: Wed, 24 Feb 2016 15:45:27 +0100 (CET)

commit 79e8e330cbfbce4cbabae7c2a31846a89afdc095
Author: FRIGN <dev_AT_frign.de>
AuthorDate: Wed Feb 24 15:40:50 2016 +0100
Commit: sin <sin_AT_2f30.org>
CommitDate: Wed Feb 24 14:45:20 2016 +0000

    Fix wc(1) output for large files
    
    Previously, we used the System V output format:
            "%7d%7d%7d %s\n"
    The problem here is, that if any number has more than six digits, the
    result looks like one big number, as we don't mandate spaces.
    
    POSIX says the output format should rather be
            "%d %d %d %s\n"
    but in this case we wouldn't get consistent results.
    
    To serve both camps, I changed it to the following:
            "%6d %6d %6d %s\n"
    This won't change the output for normal values, but also
    prevent the output of large files to be ambiguous.

diff --git a/wc.c b/wc.c
index d6d43de..56f2fda 100644
--- a/wc.c
+++ b/wc.c
_AT_@ -15,12 +15,21 @@ output(const char *str, size_t nc, size_t nl, size_t nw)
         int noflags = !cmode && !lflag && !wflag;
         int first = 1;
 
- if (lflag || noflags)
- printf("%*.1zu", first ? (first = 0) : 7, nl);
- if (wflag || noflags)
- printf("%*.1zu", first ? (first = 0) : 7, nw);
- if (cmode || noflags)
- printf("%*.1zu", first ? (first = 0) : 7, nc);
+ if (lflag || noflags) {
+ if (!first)
+ putchar(' ');
+ printf("%*.1zu", first ? (first = 0) : 6, nl);
+ }
+ if (wflag || noflags) {
+ if (!first)
+ putchar(' ');
+ printf("%*.1zu", first ? (first = 0) : 6, nw);
+ }
+ if (cmode || noflags) {
+ if (!first)
+ putchar(' ');
+ printf("%*.1zu", first ? (first = 0) : 6, nc);
+ }
         if (str)
                 printf(" %s", str);
         putchar('\n');
Received on Wed Feb 24 2016 - 15:45:27 CET

This archive was generated by hypermail 2.3.0 : Wed Feb 24 2016 - 15:48:12 CET