* src/ls.c (gobble_file): Make it so failure to stat a
authorJim Meyering <jim@meyering.net>
Tue, 25 Jul 2006 15:23:59 +0000 (15:23 +0000)
committerJim Meyering <jim@meyering.net>
Tue, 25 Jul 2006 15:23:59 +0000 (15:23 +0000)
non-command-line file provokes an exit status of 1, not 0.
Say "cannot access" rather than "cannot stat".
* tests/ls/stat-failed: New file/test, for the above.
* tests/ls/Makefile.am (TESTS): Add stat-failed.

ChangeLog
src/ls.c
tests/ls/Makefile.am
tests/ls/stat-failed [new file with mode: 0755]

index f5fda0a..f95ec10 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2006-07-25  Jim Meyering  <jim@meyering.net>
 
+       * src/ls.c (gobble_file): Make it so failure to stat a
+       non-command-line file provokes an exit status of 1, not 0.
+       Say "cannot access" rather than "cannot stat".
+       * tests/ls/stat-failed: New file/test, for the above.
+       * tests/ls/Makefile.am (TESTS): Add stat-failed.
+
        * src/ls.c: Declare stat_failed to be "bool", not "int" everywhere.
 
        * src/ls.c [enum filetype] (command_line): Remove member.  Not needed.
index 457256d..64411d1 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
@@ -2611,14 +2611,13 @@ gobble_file (char const *name, enum filetype type, ino_t inode,
       f->stat_failed = (err < 0);
       if (f->stat_failed)
        {
-         /* We treat stat failures for files the user named special.
-            There is no guarantee that these files really exist so
-            we do not print any information.  */
+         /* Failure to stat a command line argument leads to
+            an exit status of 2.  For other files, stat failure
+            provokes an exit status of 1.  */
+         file_failure (command_line_arg,
+                       _("cannot access %s"), absolute_name);
          if (command_line_arg)
-           {
-             file_failure (1, "%s", absolute_name);
-             return 0;
-           }
+           return 0;
 
          f->filetype = type;
          memset (&f->stat, '\0', sizeof (f->stat));
index 7c6ae5a..fc8af7c 100644 (file)
@@ -3,6 +3,7 @@
 AUTOMAKE_OPTIONS = 1.2 gnits
 
 TESTS = \
+  stat-failed \
   stat-dtype \
   inode dangle file-type recursive dired infloop \
   rt-1 time-1 symlink-slash follow-slink no-arg m-option \
diff --git a/tests/ls/stat-failed b/tests/ls/stat-failed
new file mode 100755 (executable)
index 0000000..a7c0066
--- /dev/null
@@ -0,0 +1,36 @@
+#!/bin/sh
+# Verify that ls works properly when it fails to stat a file that is
+# not mentioned on the command line.
+
+if test "$VERBOSE" = yes; then
+  set -x
+  ls --version
+fi
+
+. $srcdir/../envvar-check
+. $srcdir/../lang-default
+PRIV_CHECK_ARG=require-non-root . $srcdir/../priv-check
+
+pwd=`pwd`
+t0=`echo "$0"|sed 's,.*/,,'`.tmp; tmp=$t0/$$
+trap 'status=$?; cd $pwd; chmod -R u+rwx $t0; rm -rf $t0 && exit $status' 0
+trap '(exit $?); exit $?' 1 2 13 15
+
+framework_failure=0
+mkdir -p $tmp || framework_failure=1
+cd $tmp || framework_failure=1
+mkdir d || framework_failure=1
+ln -s / d/s || framework_failure=1
+chmod 600 d || framework_failure=1
+
+if test $framework_failure = 1; then
+  echo "$0: failure in testing framework" 1>&2
+  (exit 1); exit 1
+fi
+
+fail=0
+
+ls -Log d > out 2> err
+test $? = 1 || fail=1
+
+(exit $fail); exit $fail