From a033e28737c1f6320bfc56b484253b61051bad85 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Tue, 15 Sep 2009 23:07:18 +0200 Subject: 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. --- src/stat.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/stat.c b/src/stat.c index 7d42598f1..14654b174 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; -- cgit v1.2.3-54-g00ecf