]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
Give the right diagnostic when failing to create a file in an
authorJim Meyering <jim@meyering.net>
Sat, 4 Dec 1999 17:03:43 +0000 (17:03 +0000)
committerJim Meyering <jim@meyering.net>
Sat, 4 Dec 1999 17:03:43 +0000 (17:03 +0000)
unwritable directory.

(touch): Record errno upon failed errno and use that
saved value if a subsequent fstat, stat or utime call fails.
Reported by Wichert Akkerman via Michael Stone.

src/touch.c

index 03366564d69f5062e0a20db4fd3d6baa7e78b84d..2db00d3049714d682bdfcca612868e12b6876fc6 100644 (file)
@@ -111,12 +111,15 @@ touch (const char *file)
   int status;
   struct stat sbuf;
   int fd = -1;
+  int open_errno = 0;
 
   if (! no_create)
     {
       /* Try to open FILE, creating it if necessary.  */
       fd = open (file, O_WRONLY | O_CREAT,
                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
+      if (fd == -1)
+       open_errno = errno;
     }
 
   if (! amtime_now)
@@ -127,7 +130,7 @@ touch (const char *file)
         or FILE is inaccessible or a directory, so we have to use stat.  */
       if (fd != -1 ? fstat (fd, &sbuf) : stat (file, &sbuf))
        {
-         error (0, errno, "%s", file);
+         error (0, open_errno ? open_errno : errno, "%s", file);
          close (fd);
          return 1;
        }
@@ -172,7 +175,7 @@ touch (const char *file)
 
   if (status)
     {
-      error (0, errno, "%s", file);
+      error (0, open_errno ? open_errno : errno, "%s", file);
       return 1;
     }