diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2005-07-11 18:27:49 +0000 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2005-07-11 18:27:49 +0000 |
commit | f3946fbee9a26999028e67e5f4ecef59b74eed38 (patch) | |
tree | e7d832d7bd2b634a816fb268697e81c2f7101224 /src | |
parent | 07fc234838d4bae0ddaa98ead2233ba2c98a1979 (diff) | |
download | coreutils-f3946fbee9a26999028e67e5f4ecef59b74eed38.tar.xz |
(wc): Avoid setmode; use POSIX-specified routines instead.
(wc_file): FILE might be null now.
(main): Simplify code a bit, so that fewer places need the setmode fixes.
Diffstat (limited to 'src')
-rw-r--r-- | src/wc.c | 32 |
1 files changed, 11 insertions, 21 deletions
@@ -226,9 +226,6 @@ wc (int fd, char const *file_x, struct fstatus *fstatus) } count_complicated = print_words | print_linelength; - /* We need binary input, since `wc' relies on `lseek' and byte counts. */ - SET_BINARY (fd); - /* When counting only bytes, save some line- and word-counting overhead. If FD is a `regular' Unix file, using lseek is enough to get its `size' in bytes. Otherwise, read blocks of BUFFER_SIZE @@ -504,14 +501,16 @@ wc (int fd, char const *file_x, struct fstatus *fstatus) static bool wc_file (char const *file, struct fstatus *fstatus) { - if (STREQ (file, "-")) + if (! file || STREQ (file, "-")) { have_read_stdin = true; + if (O_BINARY && ! isatty (STDIN_FILENO)) + freopen (NULL, "rb", stdin); return wc (STDIN_FILENO, file, fstatus); } else { - int fd = open (file, O_RDONLY); + int fd = open (file, O_RDONLY | O_BINARY); if (fd == -1) { error (0, errno, "%s", file); @@ -595,6 +594,7 @@ compute_number_width (int nfiles, struct fstatus const *fstatus) int main (int argc, char **argv) { + int i; bool ok; int optc; int nfiles; @@ -653,23 +653,13 @@ main (int argc, char **argv) fstatus = get_input_fstatus (nfiles, argv + optind); number_width = compute_number_width (nfiles, fstatus); - if (! argv[optind]) - { - have_read_stdin = true; - ok = wc (STDIN_FILENO, NULL, &fstatus[0]); - } - else - { - int i; - - ok = true; - for (i = 0; i < nfiles; i++) - ok &= wc_file (argv[optind + i], &fstatus[i]); + ok = true; + for (i = 0; i < nfiles; i++) + ok &= wc_file (argv[optind + i], &fstatus[i]); - if (nfiles > 1) - write_counts (total_lines, total_words, total_chars, total_bytes, - max_line_length, _("total")); - } + if (1 < nfiles) + write_counts (total_lines, total_words, total_chars, total_bytes, + max_line_length, _("total")); free (fstatus); |