annotate toys/wc.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents 67b0ded3c56c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
386
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/wc.html
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 USE_WC(NEWTOY(wc, "cwl", TOYFLAG_USR|TOYFLAG_BIN))
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
10
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 config WC
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 bool "wc"
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 default y
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 help
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 usage: wc -lwc [FILE...]
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
16
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 Count lines, words, and characters in input.
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
18
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 -l show lines
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 -w show words
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 -c show characters
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
22
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 By default outputs lines, words, characters, and filename for each
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 argument (or from stdin if none).
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 */
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
26
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 #include "toys.h"
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
28
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 static void do_wc(int fd, char *name)
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 {
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 int i, len;
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 unsigned long word=0, lengths[]={0,0,0};
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
33
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 for (;;) {
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 len = read(fd, toybuf, sizeof(toybuf));
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 if (len<0) {
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 perror_msg("%s",name);
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 toys.exitval = EXIT_FAILURE;
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 }
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 if (len<1) break;
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 for (i=0; i<len; i++) {
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 if (toybuf[i]==10) lengths[0]++;
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 if (isspace(toybuf[i])) word=0;
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 else {
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 if (!word) lengths[1]++;
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 word=1;
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 }
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 lengths[2]++;
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 }
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 }
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 for (i=0; i<3; i++)
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 if (!toys.optflags || (toys.optflags&(1<<i)))
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 printf("%ld ", lengths[i]);
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 printf("%s\n", (!toys.optflags && strcmp(name,"-")) ? name : "");
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 }
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
56
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 void wc_main(void)
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 {
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 loopfiles(toys.optargs, do_wc);
03bf7c545b6f Add wc.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 }