From: Paul Eggert Date: Mon, 11 Jul 2005 18:27:49 +0000 (+0000) Subject: (wc): Avoid setmode; use POSIX-specified routines instead. X-Git-Tag: CPPI-1_12~263 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f3946fbee9a26999028e67e5f4ecef59b74eed38;p=platform%2Fupstream%2Fcoreutils.git (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. --- diff --git a/src/wc.c b/src/wc.c index 3e451eb..98af229 100644 --- a/src/wc.c +++ b/src/wc.c @@ -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);