1 /* wc - print the number of bytes, words, and lines in files
2 Copyright (C) 1985, 1991, 1995 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by Paul Rubin, phr@ocf.berkeley.edu
19 and David MacKenzie, djm@gnu.ai.mit.edu. */
25 #include <sys/types.h>
30 /* Size of atomic reads. */
31 #define BUFFER_SIZE (16 * 1024)
36 static void wc_file ();
37 static void write_counts ();
39 /* The name this program was run with. */
42 /* Cumulative number of lines, words, and chars in all files so far. */
43 static unsigned long total_lines, total_words, total_chars;
45 /* Which counts to print. */
46 static int print_lines, print_words, print_chars;
48 /* Nonzero if we have ever read the standard input. */
49 static int have_read_stdin;
51 /* The error code to return to the system. */
52 static int exit_status;
54 /* If non-zero, display usage information and exit. */
57 /* If non-zero, print the version on standard output then exits. */
58 static int show_version;
60 static struct option const longopts[] =
62 {"bytes", no_argument, NULL, 'c'},
63 {"chars", no_argument, NULL, 'c'},
64 {"lines", no_argument, NULL, 'l'},
65 {"words", no_argument, NULL, 'w'},
66 {"help", no_argument, &show_help, 1},
67 {"version", no_argument, &show_version, 1},
76 fprintf (stderr, "Try `%s --help' for more information.\n",
81 Usage: %s [OPTION]... [FILE]...\n\
86 -c, --bytes, --chars print the byte counts\n\
87 -l, --lines print the newline counts\n\
88 -w, --words print the word counts\n\
89 --help display this help and exit\n\
90 --version output version information and exit\n\
92 Print lines, words and bytes in that order. If none of -clw, select\n\
93 them all. With no FILE, or when FILE is -, read standard input.\n\
107 program_name = argv[0];
109 print_lines = print_words = print_chars = 0;
110 total_lines = total_words = total_chars = 0;
112 while ((optc = getopt_long (argc, argv, "clw", longopts, (int *) 0)) != EOF)
136 printf ("wc - %s\n", version_string);
143 if (print_lines + print_words + print_chars == 0)
144 print_lines = print_words = print_chars = 1;
146 nfiles = argc - optind;
155 for (; optind < argc; ++optind)
156 wc_file (argv[optind]);
159 write_counts (total_lines, total_words, total_chars, "total");
162 if (have_read_stdin && close (0))
163 error (1, errno, "-");
172 if (!strcmp (file, "-"))
179 int fd = open (file, O_RDONLY);
182 error (0, errno, "%s", file);
189 error (0, errno, "%s", file);
200 char buf[BUFFER_SIZE + 1];
201 register int bytes_read;
202 register int in_word = 0;
203 register unsigned long lines, words, chars;
205 lines = words = chars = 0;
207 /* When counting only bytes, save some line- and word-counting
208 overhead. If FD is a `regular' Unix file, using lseek is enough
209 to get its `size' in bytes. Otherwise, read blocks of BUFFER_SIZE
210 bytes at a time until EOF. Note that the `size' (number of bytes)
211 that wc reports is smaller than stats.st_size when the file is not
212 positioned at its beginning. That's why the lseek calls below are
213 necessary. For example the command
214 `(dd ibs=99k skip=1 count=0; ./wc -c) < /etc/group'
215 should make wc report `0' bytes. */
217 if (print_chars && !print_words && !print_lines)
219 off_t current_pos, end_pos;
222 if (fstat (fd, &stats) == 0 && S_ISREG (stats.st_mode)
223 && (current_pos = lseek (fd, (off_t) 0, SEEK_CUR)) != -1
224 && (end_pos = lseek (fd, (off_t) 0, SEEK_END)) != -1)
227 /* Be careful here. The current position may actually be
228 beyond the end of the file. As in the example above. */
229 chars = (diff = end_pos - current_pos) < 0 ? 0 : diff;
233 while ((bytes_read = safe_read (fd, buf, BUFFER_SIZE)) > 0)
239 error (0, errno, "%s", file);
244 else if (!print_words)
246 /* Use a separate loop when counting only lines or lines and bytes --
248 while ((bytes_read = safe_read (fd, buf, BUFFER_SIZE)) > 0)
250 register char *p = buf;
252 while ((p = memchr (p, '\n', (buf + bytes_read) - p)))
261 error (0, errno, "%s", file);
267 while ((bytes_read = safe_read (fd, buf, BUFFER_SIZE)) > 0)
269 register char *p = buf;
295 while (--bytes_read);
299 error (0, errno, "%s", file);
306 write_counts (lines, words, chars, file);
307 total_lines += lines;
308 total_words += words;
309 total_chars += chars;
313 write_counts (lines, words, chars, file)
314 unsigned long lines, words, chars;
318 printf ("%7lu", lines);
323 printf ("%7lu", words);
327 if (print_lines || print_words)
329 printf ("%7lu", chars);
332 printf (" %s", file);