(EISDIR): Define to 0, if not already defined.
authorJim Meyering <jim@meyering.net>
Sat, 15 Sep 2001 17:07:24 +0000 (17:07 +0000)
committerJim Meyering <jim@meyering.net>
Sat, 15 Sep 2001 17:07:24 +0000 (17:07 +0000)
(touch): Give a better diagnostic for e.g., `touch /' by non-root.
Based on a patch from Michael Stone.
Reported by Jeff Sheinberg as Debian bug #101677.

src/touch.c

index 35fa58f..517ec84 100644 (file)
@@ -57,6 +57,10 @@ time_t time ();
 # define O_NOCTTY 0
 #endif
 
+#if !defined EISDIR
+# define EISDIR 0
+#endif
+
 /* The name by which this program was run. */
 char *program_name;
 
@@ -138,7 +142,11 @@ touch (const char *file)
       /* Try to open FILE, creating it if necessary.  */
       fd = open (file, O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY,
                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
-      if (fd == -1)
+
+      /* Don't save a copy of errno if it's EISDIR, since that would lead
+        touch to give a bogus diagnostic for e.g., `touch /' (assuming
+        we don't own / or have write access to it).  */
+      if (fd == -1 && errno != EISDIR)
        open_errno = errno;
     }