From: Jim Meyering Date: Sat, 4 Dec 1999 17:03:43 +0000 (+0000) Subject: Give the right diagnostic when failing to create a file in an X-Git-Tag: FILEUTILS-4_0k~18 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=091109470c385bf0f1c93f637985b07708ade234;p=thirdparty%2Fcoreutils.git 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. --- diff --git a/src/touch.c b/src/touch.c index 03366564d6..2db00d3049 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; }