summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1999-12-04 17:03:43 +0000
committerJim Meyering <jim@meyering.net>1999-12-04 17:03:43 +0000
commit091109470c385bf0f1c93f637985b07708ade234 (patch)
treea5f7c9972c43872e26803bafcbc2c75432b0fe86 /src
parent9481a7bd36575258c805df04af3d6f2a8265e02e (diff)
downloadcoreutils-091109470c385bf0f1c93f637985b07708ade234.tar.xz
Give the right diagnostic when failing to create a file in an
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.
Diffstat (limited to 'src')
-rw-r--r--src/touch.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/src/touch.c b/src/touch.c
index 03366564d..2db00d304 100644
--- a/src/touch.c
+++ b/src/touch.c
@@ -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;
}