1 /* wc - print the number of bytes, words, and lines in files
2 Copyright (C) 85, 91, 95, 96, 1997 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Paul Rubin, phr@ocf.berkeley.edu
19 and David MacKenzie, djm@gnu.ai.mit.edu. */
25 #include <sys/types.h>
29 /* Size of atomic reads. */
30 #define BUFFER_SIZE (16 * 1024)
34 /* The name this program was run with. */
37 /* Cumulative number of lines, words, and chars in all files so far.
38 max_line_length is the maximum over all files processed so far. */
39 static unsigned long total_lines
, total_words
, total_chars
, max_line_length
;
41 /* Which counts to print. */
42 static int print_lines
, print_words
, print_chars
, print_linelength
;
44 /* Nonzero if we have ever read the standard input. */
45 static int have_read_stdin
;
47 /* The error code to return to the system. */
48 static int exit_status
;
50 /* If nonzero, display usage information and exit. */
53 /* If nonzero, print the version on standard output then exits. */
54 static int show_version
;
56 static struct option
const longopts
[] =
58 {"bytes", no_argument
, NULL
, 'c'},
59 {"chars", no_argument
, NULL
, 'c'},
60 {"lines", no_argument
, NULL
, 'l'},
61 {"words", no_argument
, NULL
, 'w'},
62 {"max-line-length", no_argument
, NULL
, 'L'},
63 {"help", no_argument
, &show_help
, 1},
64 {"version", no_argument
, &show_version
, 1},
72 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
77 Usage: %s [OPTION]... [FILE]...\n\
81 Print line, word, and byte counts for each FILE, and a total line if\n\
82 more than one FILE is specified. With no FILE, or when FILE is -,\n\
83 read standard input.\n\
84 -c, --bytes, --chars print the byte counts\n\
85 -l, --lines print the newline counts\n\
86 -L, --max-line-length print the length of the longest line\n\
87 -w, --words print the word counts\n\
88 --help display this help and exit\n\
89 --version output version information and exit\n\
91 puts (_("\nReport bugs to <textutils-bugs@gnu.org>."));
93 exit (status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
97 write_counts (long unsigned int lines
, long unsigned int words
,
98 long unsigned int chars
, long unsigned int linelength
,
102 printf ("%7lu", lines
);
107 printf ("%7lu", words
);
111 if (print_lines
|| print_words
)
113 printf ("%7lu", chars
);
115 if (print_linelength
)
117 if (print_lines
|| print_words
|| print_chars
)
119 printf ("%7lu", linelength
);
122 printf (" %s", file
);
127 wc (int fd
, const char *file
)
129 char buf
[BUFFER_SIZE
+ 1];
130 register int bytes_read
;
131 register int in_word
= 0;
132 register unsigned long lines
, words
, chars
, linelength
;
134 lines
= words
= chars
= linelength
= 0;
136 /* When counting only bytes, save some line- and word-counting
137 overhead. If FD is a `regular' Unix file, using lseek is enough
138 to get its `size' in bytes. Otherwise, read blocks of BUFFER_SIZE
139 bytes at a time until EOF. Note that the `size' (number of bytes)
140 that wc reports is smaller than stats.st_size when the file is not
141 positioned at its beginning. That's why the lseek calls below are
142 necessary. For example the command
143 `(dd ibs=99k skip=1 count=0; ./wc -c) < /etc/group'
144 should make wc report `0' bytes. */
146 if (print_chars
&& !print_words
&& !print_lines
&& !print_linelength
)
148 off_t current_pos
, end_pos
;
151 if (fstat (fd
, &stats
) == 0 && S_ISREG (stats
.st_mode
)
152 && (current_pos
= lseek (fd
, (off_t
) 0, SEEK_CUR
)) != -1
153 && (end_pos
= lseek (fd
, (off_t
) 0, SEEK_END
)) != -1)
156 /* Be careful here. The current position may actually be
157 beyond the end of the file. As in the example above. */
158 chars
= (diff
= end_pos
- current_pos
) < 0 ? 0 : diff
;
162 while ((bytes_read
= safe_read (fd
, buf
, BUFFER_SIZE
)) > 0)
168 error (0, errno
, "%s", file
);
173 else if (!print_words
&& !print_linelength
)
175 /* Use a separate loop when counting only lines or lines and bytes --
177 while ((bytes_read
= safe_read (fd
, buf
, BUFFER_SIZE
)) > 0)
179 register char *p
= buf
;
181 while ((p
= memchr (p
, '\n', (buf
+ bytes_read
) - p
)))
190 error (0, errno
, "%s", file
);
196 register unsigned long linepos
= 0;
198 while ((bytes_read
= safe_read (fd
, buf
, BUFFER_SIZE
)) > 0)
200 register char *p
= buf
;
212 if (linepos
> linelength
)
213 linelength
= linepos
;
217 linepos
+= 8 - (linepos
% 8);
236 while (--bytes_read
);
240 error (0, errno
, "%s", file
);
243 if (linepos
> linelength
)
244 linelength
= linepos
;
249 write_counts (lines
, words
, chars
, linelength
, file
);
250 total_lines
+= lines
;
251 total_words
+= words
;
252 total_chars
+= chars
;
253 if (linelength
> max_line_length
)
254 max_line_length
= linelength
;
258 wc_file (const char *file
)
260 if (!strcmp (file
, "-"))
267 int fd
= open (file
, O_RDONLY
);
270 error (0, errno
, "%s", file
);
277 error (0, errno
, "%s", file
);
284 main (int argc
, char **argv
)
289 program_name
= argv
[0];
290 setlocale (LC_ALL
, "");
291 bindtextdomain (PACKAGE
, LOCALEDIR
);
292 textdomain (PACKAGE
);
295 print_lines
= print_words
= print_chars
= print_linelength
= 0;
296 total_lines
= total_words
= total_chars
= max_line_length
= 0;
298 while ((optc
= getopt_long (argc
, argv
, "clLw", longopts
, NULL
)) != -1)
317 print_linelength
= 1;
326 printf ("wc (%s) %s\n", GNU_PACKAGE
, VERSION
);
333 if (print_lines
+ print_words
+ print_chars
+ print_linelength
== 0)
334 print_lines
= print_words
= print_chars
= 1;
336 nfiles
= argc
- optind
;
345 for (; optind
< argc
; ++optind
)
346 wc_file (argv
[optind
]);
349 write_counts (total_lines
, total_words
, total_chars
, max_line_length
,
353 if (have_read_stdin
&& close (0))
354 error (EXIT_FAILURE
, errno
, "-");
356 exit (exit_status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);