aboutsummaryrefslogtreecommitdiff
path: root/toys/posix/wc.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2012-09-06 03:10:18 -0500
committerRob Landley <rob@landley.net>2012-09-06 03:10:18 -0500
commit7c8103e5d2e472a2f11f6d6c6660c2903167fb7b (patch)
treed92d410e664f6e5b79d437bc7827c19e2cdfde87 /toys/posix/wc.c
parent1f4b0667fcead912501d1ab22ed8e4b11899bbad (diff)
downloadtoybox-7c8103e5d2e472a2f11f6d6c6660c2903167fb7b.tar.gz
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
amfs script), when to print filenames was wrong, and it should have a "total" line when counting multiple arguments.
Diffstat (limited to 'toys/posix/wc.c')
-rw-r--r--toys/posix/wc.c27
1 files changed, 23 insertions, 4 deletions
diff --git a/toys/posix/wc.c b/toys/posix/wc.c
index 17801188..9d11577c 100644
--- a/toys/posix/wc.c
+++ b/toys/posix/wc.c
@@ -26,6 +26,26 @@ config WC
#include "toys.h"
+DEFINE_GLOBALS(
+ unsigned long totals[3];
+)
+
+#define TT this.wc
+
+static void show_lengths(unsigned long *lengths, char *name)
+{
+ int i, nospace = 1;
+ for (i=0; i<3; i++) {
+ if (!toys.optflags || (toys.optflags&(1<<i))) {
+ xprintf(" %ld"+nospace, lengths[i]);
+ nospace = 0;
+ }
+ TT.totals[i] += lengths[i];
+ }
+ if (*toys.optargs) xprintf(" %s", name);
+ xputc('\n');
+}
+
static void do_wc(int fd, char *name)
{
int i, len;
@@ -48,13 +68,12 @@ static void do_wc(int fd, char *name)
lengths[2]++;
}
}
- for (i=0; i<3; i++)
- if (!toys.optflags || (toys.optflags&(1<<i)))
- printf("%ld ", lengths[i]);
- printf("%s\n", (!toys.optflags && strcmp(name,"-")) ? name : "");
+
+ show_lengths(lengths, name);
}
void wc_main(void)
{
loopfiles(toys.optargs, do_wc);
+ if (toys.optc>1) show_lengths(TT.totals, "total");
}