Mercurial > hg > toybox
annotate toys/posix/wc.c @ 674:7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Oct 2012 00:02:30 -0500 |
parents | 5cb41d56f9d3 |
children | 9414be56b1db |
rev | line source |
---|---|
386 | 1 /* vi: set sw=4 ts=4: |
2 * | |
387
67b0ded3c56c
Add dirname, and typo fixes to wc header.
Rob Landley <rob@landley.net>
parents:
386
diff
changeset
|
3 * wc.c - Word count |
386 | 4 * |
387
67b0ded3c56c
Add dirname, and typo fixes to wc header.
Rob Landley <rob@landley.net>
parents:
386
diff
changeset
|
5 * Copyright 2011 Rob Landley <rob@landley.net> |
386 | 6 * |
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/wc.html | |
8 | |
9 USE_WC(NEWTOY(wc, "cwl", TOYFLAG_USR|TOYFLAG_BIN)) | |
10 | |
11 config WC | |
12 bool "wc" | |
13 default y | |
14 help | |
15 usage: wc -lwc [FILE...] | |
16 | |
17 Count lines, words, and characters in input. | |
18 | |
19 -l show lines | |
20 -w show words | |
21 -c show characters | |
22 | |
23 By default outputs lines, words, characters, and filename for each | |
24 argument (or from stdin if none). | |
25 */ | |
26 | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
662
diff
changeset
|
27 #define FOR_wc |
386 | 28 #include "toys.h" |
29 | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
662
diff
changeset
|
30 GLOBALS( |
662
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
31 unsigned long totals[3]; |
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
32 ) |
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
33 |
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
34 static void show_lengths(unsigned long *lengths, char *name) |
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
35 { |
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
36 int i, nospace = 1; |
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
37 for (i=0; i<3; i++) { |
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
38 if (!toys.optflags || (toys.optflags&(1<<i))) { |
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
39 xprintf(" %ld"+nospace, lengths[i]); |
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
40 nospace = 0; |
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
41 } |
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
42 TT.totals[i] += lengths[i]; |
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
43 } |
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
44 if (*toys.optargs) xprintf(" %s", name); |
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
45 xputc('\n'); |
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
46 } |
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
47 |
386 | 48 static void do_wc(int fd, char *name) |
49 { | |
50 int i, len; | |
51 unsigned long word=0, lengths[]={0,0,0}; | |
52 | |
53 for (;;) { | |
54 len = read(fd, toybuf, sizeof(toybuf)); | |
55 if (len<0) { | |
56 perror_msg("%s",name); | |
57 toys.exitval = EXIT_FAILURE; | |
58 } | |
59 if (len<1) break; | |
60 for (i=0; i<len; i++) { | |
61 if (toybuf[i]==10) lengths[0]++; | |
62 if (isspace(toybuf[i])) word=0; | |
63 else { | |
64 if (!word) lengths[1]++; | |
65 word=1; | |
66 } | |
67 lengths[2]++; | |
68 } | |
69 } | |
662
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
70 |
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
71 show_lengths(lengths, name); |
386 | 72 } |
73 | |
74 void wc_main(void) | |
75 { | |
76 loopfiles(toys.optargs, do_wc); | |
662
5cb41d56f9d3
Posix compliance: wc shouldn't have trailing spaces (breaks aboriginal's mkinitr
Rob Landley <rob@landley.net>
parents:
653
diff
changeset
|
77 if (toys.optc>1) show_lengths(TT.totals, "total"); |
386 | 78 } |