bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] stat: interpret "-" as standard input


From: Jim Meyering
Subject: [PATCH] stat: interpret "-" as standard input
Date: Tue, 29 Sep 2009 16:05:05 +0200

Per recent discussion, here's all but the documentation update.

>From a033e28737c1f6320bfc56b484253b61051bad85 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Tue, 15 Sep 2009 23:07:18 +0200
Subject: [PATCH] stat: interpret "-" as standard input

* src/stat.c (do_stat): Interpret a command line argument of "-"
to mean "standard input", like many other tools do.
(do_statfs): Fail upon any attempt to use "-".
* NEWS (Changes in behavior): Mention it.
* tests/misc/stat-hyphen: New test, to exercise the above.
* tests/Makefile.am (TESTS): Add misc/stat-hyphen.
---
 NEWS                   |    5 +++++
 src/stat.c             |   17 ++++++++++++++++-
 tests/Makefile.am      |    1 +
 tests/misc/stat-hyphen |   35 +++++++++++++++++++++++++++++++++++
 4 files changed, 57 insertions(+), 1 deletions(-)
 create mode 100755 tests/misc/stat-hyphen

diff --git a/NEWS b/NEWS
index 075c0fa..f1f7347 100644
--- a/NEWS
+++ b/NEWS
@@ -48,6 +48,11 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   GNU/Linux where link(2) creates hard links to symlinks, and -L on
   BSD systems where link(2) follows symlinks.

+  stat: without -f, a command-line argument of "-" now means standard input.
+  With --file-system (-f), an argument of "-" is now rejected.
+  If you really must operate on a file named "-", specify it as
+  "./-" or use "--" to separate options from arguments.
+
 ** Improvements

   rm: rewrite to use gnulib's fts
diff --git a/src/stat.c b/src/stat.c
index 7d42598..14654b1 100644
--- a/src/stat.c
+++ b/src/stat.c
@@ -829,6 +829,13 @@ do_statfs (char const *filename, bool terse, char const 
*format)
 {
   STRUCT_STATVFS statfsbuf;

+  if (STREQ (filename, "-"))
+    {
+      error (0, 0, _("using %s to denote standard input does not work"
+                     " in file system mode"), quote (filename));
+      return false;
+    }
+
   if (STATFS (filename, &statfsbuf) != 0)
     {
       error (0, errno, _("cannot read file system information for %s"),
@@ -857,7 +864,15 @@ do_stat (char const *filename, bool terse, char const 
*format)
 {
   struct stat statbuf;

-  if ((follow_links ? stat : lstat) (filename, &statbuf) != 0)
+  if (STREQ (filename, "-"))
+    {
+      if (fstat (STDIN_FILENO, &statbuf) != 0)
+        {
+          error (0, errno, _("cannot stat standard input"));
+          return false;
+        }
+    }
+  else if ((follow_links ? stat : lstat) (filename, &statbuf) != 0)
     {
       error (0, errno, _("cannot stat %s"), quote (filename));
       return false;
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 2acad6b..5fd541a 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -216,6 +216,7 @@ TESTS =                                             \
   misc/split-fail                              \
   misc/split-l                                 \
   misc/stat-fmt                                        \
+  misc/stat-hyphen                             \
   misc/stat-printf                             \
   misc/stdbuf                                  \
   misc/stty                                    \
diff --git a/tests/misc/stat-hyphen b/tests/misc/stat-hyphen
new file mode 100755
index 0000000..f0757fe
--- /dev/null
+++ b/tests/misc/stat-hyphen
@@ -0,0 +1,35 @@
+#!/bin/sh
+# demonstrate that stat - works and stat -f - does not.
+
+# Copyright (C) 2009 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
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+  set -x
+  stat --version
+fi
+
+. $srcdir/test-lib.sh
+
+printf -- '-\n' > exp || framework_failure
+touch f || framework_failure
+
+fail=0
+stat --format=%n - < f > out || fail=1
+stat -f - < f && fail=1
+
+compare out exp || fail=1
+
+Exit $fail
--
1.6.5.rc2.177.ga9dd6




reply via email to

[Prev in Thread] Current Thread [Next in Thread]