summaryrefslogtreecommitdiff
path: root/src/cat.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2014-09-11 08:45:32 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2014-09-11 08:47:38 -0700
commit3b1ac4b1edd6f56155196ce79e6a58c652f03f2f (patch)
tree65cda39434853ebc16fad227b21e83b49b477f16 /src/cat.c
parente8715100cb2824fbd8ec724728a21fffdbcdb9f5 (diff)
downloadcoreutils-3b1ac4b1edd6f56155196ce79e6a58c652f03f2f.tar.xz
cat: allow copying empty files to themselves
Problem reported by Vincent Lefevre in: http://bugs.gnu.org/18449 * src/cat.c (main): Allow copying an empty file to itself. * tests/misc/cat-self.sh: New test. * tests/local.mk (all_tests): Add it.
Diffstat (limited to 'src/cat.c')
-rw-r--r--src/cat.c37
1 files changed, 10 insertions, 27 deletions
diff --git a/src/cat.c b/src/cat.c
index 026348c04..267864f20 100644
--- a/src/cat.c
+++ b/src/cat.c
@@ -527,8 +527,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 +637,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 +688,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;