summaryrefslogtreecommitdiff
path: root/src/cat.c
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2015-07-04 17:13:50 +0300
committerIgor Pashev <pashev.igor@gmail.com>2015-07-04 17:13:50 +0300
commit71cd8e3a743046573744123777061b64881bf372 (patch)
tree82522befe647f4fff186a5630cad0cad33f8ef53 /src/cat.c
parentc18578632fd3c9e513e613a86ba2b7c4ebee6c45 (diff)
downloadcoreutils-upstream.tar.gz
Imported Upstream version 8.24upstream/8.24upstream
Diffstat (limited to 'src/cat.c')
-rw-r--r--src/cat.c60
1 files changed, 22 insertions, 38 deletions
diff --git a/src/cat.c b/src/cat.c
index 8aef79fa..948f1341 100644
--- a/src/cat.c
+++ b/src/cat.c
@@ -1,5 +1,5 @@
/* cat -- concatenate files and print on the standard output.
- Copyright (C) 1988-2014 Free Software Foundation, Inc.
+ Copyright (C) 1988-2015 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -90,7 +90,12 @@ Usage: %s [OPTION]... [FILE]...\n\
"),
program_name);
fputs (_("\
-Concatenate FILE(s), or standard input, to standard output.\n\
+Concatenate FILE(s) to standard output.\n\
+"), stdout);
+
+ emit_stdin_note ();
+
+ fputs (_("\
\n\
-A, --show-all equivalent to -vET\n\
-b, --number-nonblank number nonempty output lines, overrides -n\n\
@@ -107,10 +112,6 @@ Concatenate FILE(s), or standard input, to standard output.\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
- fputs (_("\
-\n\
-With no FILE, or when FILE is -, read standard input.\n\
-"), stdout);
printf (_("\
\n\
Examples:\n\
@@ -118,7 +119,7 @@ Examples:\n\
%s Copy standard input to standard output.\n\
"),
program_name, program_name);
- emit_ancillary_info ();
+ emit_ancillary_info (PROGRAM_NAME);
}
exit (status);
}
@@ -239,7 +240,7 @@ cat (
/* Pointer to the next character in the input buffer. */
char *bpin;
- /* Pointer to the first non-valid byte in the input buffer, i.e. the
+ /* Pointer to the first non-valid byte in the input buffer, i.e., the
current end of the buffer. */
char *eob;
@@ -365,7 +366,7 @@ cat (
/* It was a real (not a sentinel) newline. */
/* Was the last line empty?
- (i.e. have two or more consecutive newlines been read?) */
+ (i.e., have two or more consecutive newlines been read?) */
if (++newlines > 0)
{
@@ -422,7 +423,7 @@ cat (
which means that the buffer is empty or that a proper newline
has been found. */
- /* If quoting, i.e. at least one of -v, -e, or -t specified,
+ /* If quoting, i.e., at least one of -v, -e, or -t specified,
scan for chars that need conversion. */
if (show_nonprinting)
{
@@ -527,8 +528,8 @@ main (int argc, char **argv)
/* I-node number of the output. */
ino_t out_ino;
- /* True if the output file should not be the same as any input file. */
- bool check_redirection = true;
+ /* True if the output is a regular file. */
+ bool out_isreg;
/* Nonzero if we have ever read standard input. */
bool have_read_stdin = false;
@@ -637,25 +638,9 @@ main (int argc, char **argv)
error (EXIT_FAILURE, errno, _("standard output"));
outsize = io_blksize (stat_buf);
- /* Input file can be output file for non-regular files.
- fstat on pipes returns S_IFSOCK on some systems, S_IFIFO
- on others, so the checking should not be done for those types,
- and to allow things like cat < /dev/tty > /dev/tty, checking
- is not done for device files either. */
-
- if (S_ISREG (stat_buf.st_mode))
- {
- out_dev = stat_buf.st_dev;
- out_ino = stat_buf.st_ino;
- }
- else
- {
- check_redirection = false;
-#ifdef lint /* Suppress 'used before initialized' warning. */
- out_dev = 0;
- out_ino = 0;
-#endif
- }
+ out_dev = stat_buf.st_dev;
+ out_ino = stat_buf.st_ino;
+ out_isreg = S_ISREG (stat_buf.st_mode) != 0;
if (! (number || show_ends || squeeze_blank))
{
@@ -704,14 +689,13 @@ main (int argc, char **argv)
fdadvise (input_desc, 0, 0, FADVISE_SEQUENTIAL);
- /* Compare the device and i-node numbers of this input file with
- the corresponding values of the (output file associated with)
- stdout, and skip this input file if they coincide. Input
- files cannot be redirected to themselves. */
+ /* Don't copy a nonempty regular file to itself, as that would
+ merely exhaust the output device. It's better to catch this
+ error earlier rather than later. */
- if (check_redirection
+ if (out_isreg
&& stat_buf.st_dev == out_dev && stat_buf.st_ino == out_ino
- && (input_desc != STDIN_FILENO))
+ && lseek (input_desc, 0, SEEK_CUR) < stat_buf.st_size)
{
error (0, 0, _("%s: input file is output file"), infile);
ok = false;
@@ -780,5 +764,5 @@ main (int argc, char **argv)
if (have_read_stdin && close (STDIN_FILENO) < 0)
error (EXIT_FAILURE, errno, _("closing standard input"));
- exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
+ return ok ? EXIT_SUCCESS : EXIT_FAILURE;
}