diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2005-07-11 18:23:23 +0000 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2005-07-11 18:23:23 +0000 |
commit | 6560de0b044d64c000775b197515c6d7d991c938 (patch) | |
tree | 9128ebe1e710ed214d80316a7fd1d06940db6355 | |
parent | 9824112cfa187648b200b16dda1884ff54f64e52 (diff) | |
download | coreutils-6560de0b044d64c000775b197515c6d7d991c938.tar.xz |
(head_lines, head_file): Avoid setmode; use POSIX-specified routines instead.
(elide_tail_bytes_file, elide_tail_lines_file, head_bytes):
(head_lines, head_file): Always use binary mode except for std tty.
-rw-r--r-- | src/head.c | 22 |
1 files changed, 6 insertions, 16 deletions
diff --git a/src/head.c b/src/head.c index 0c9ead03b..df6493080 100644 --- a/src/head.c +++ b/src/head.c @@ -411,10 +411,6 @@ elide_tail_bytes_file (const char *filename, int fd, uintmax_t n_elide) { struct stat stats; - /* We need binary input, since `head' relies on `lseek' and byte counts, - while binary output will preserve the style (Unix/DOS) of text file. */ - SET_BINARY2 (fd, STDOUT_FILENO); - if (presume_input_pipe || fstat (fd, &stats) || ! S_ISREG (stats.st_mode)) { return elide_tail_bytes_pipe (filename, fd, n_elide); @@ -713,10 +709,6 @@ elide_tail_lines_seekable (const char *pretty_filename, int fd, static bool elide_tail_lines_file (const char *filename, int fd, uintmax_t n_elide) { - /* We need binary input, since `head' relies on `lseek' and byte counts, - while binary output will preserve the style (Unix/DOS) of text file. */ - SET_BINARY2 (fd, STDOUT_FILENO); - if (!presume_input_pipe) { /* Find the offset, OFF, of the Nth newline from the end, @@ -749,9 +741,6 @@ head_bytes (const char *filename, int fd, uintmax_t bytes_to_write) char buffer[BUFSIZ]; size_t bytes_to_read = BUFSIZ; - /* Need BINARY I/O for the byte counts to be accurate. */ - SET_BINARY2 (fd, fileno (stdout)); - while (bytes_to_write) { size_t bytes_read; @@ -777,10 +766,6 @@ head_lines (const char *filename, int fd, uintmax_t lines_to_write) { char buffer[BUFSIZ]; - /* Need BINARY I/O for the byte counts to be accurate. */ - /* FIXME: do we really need this when counting *lines*? */ - SET_BINARY2 (fd, fileno (stdout)); - while (lines_to_write) { size_t bytes_read = safe_read (fd, buffer, BUFSIZ); @@ -853,10 +838,12 @@ head_file (const char *filename, uintmax_t n_units, bool count_lines, have_read_stdin = true; fd = STDIN_FILENO; filename = _("standard input"); + if (O_BINARY && ! isatty (STDIN_FILENO)) + freopen (NULL, "rb", stdin); } else { - fd = open (filename, O_RDONLY); + fd = open (filename, O_RDONLY | O_BINARY); if (fd < 0) { error (0, errno, _("cannot open %s for reading"), quote (filename)); @@ -1061,6 +1048,9 @@ main (int argc, char **argv) ? (char const *const *) &argv[optind] : default_file_list); + if (O_BINARY && ! isatty (STDOUT_FILENO)) + freopen (NULL, "wb", stdout); + for (i = 0; file_list[i]; ++i) ok &= head_file (file_list[i], n_units, count_lines, elide_from_end); |