diff options
author | Jim Meyering <jim@meyering.net> | 1999-11-13 14:47:21 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 1999-11-13 14:47:21 +0000 |
commit | 0e09235c9f11251268b6edb2333287fb5d3f24a4 (patch) | |
tree | 34b57cd34bd6f296948beaa55da349dde3a98f84 /src/touch.c | |
parent | 6b85baa5025c5895f99cdac88c1dd5b530b7ccc7 (diff) | |
download | coreutils-0e09235c9f11251268b6edb2333287fb5d3f24a4.tar.xz |
(touch): Don't fail just because we couldn't open
an existing file. This makes it so that touching a read-only
file now works. Also clean up and simplify.
Based on a patch from Chip Salzenberg.
Diffstat (limited to 'src/touch.c')
-rw-r--r-- | src/touch.c | 35 |
1 files changed, 12 insertions, 23 deletions
diff --git a/src/touch.c b/src/touch.c index 87986bc22..e8851ad89 100644 --- a/src/touch.c +++ b/src/touch.c @@ -64,7 +64,9 @@ static int posix_date; /* If nonzero, the only thing we have to do is change both the modification and access time to the current time, so we don't - have to own the file, just be able to read and write it. */ + have to own the file, just be able to read and write it. + On some systems, we can do this if we own the file, even though + we have neither read nor write access to it. */ static int amtime_now; /* New time to use when setting time. */ @@ -108,41 +110,28 @@ touch (const char *file) { int status; struct stat sbuf; - int fd; + int fd IF_LINT (= 99); + int valid_fd; if (no_create) { - /* Try to open an existing FILE. */ - fd = open (file, O_WRONLY); - if (fd == -1 && errno == ENOENT) - { - /* FILE doesn't exist. So we're done. */ - return 0; - } + valid_fd = 0; } else { + valid_fd = 1; /* 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); } - /* Don't fail if the open failed because FILE is a directory. */ - if (fd == -1 - /* As usual, using E* macros like this is risky... - So heads up. */ - && errno != EISDIR) - { - error (0, errno, "%s", file); - return 1; - } - if (! amtime_now) { /* We're setting only one of the time values. stat the target to get - the other one. If we have the file descriptor already, use fstat, - otherwise, FILE is a directory, so we have to use stat. */ - if (fd == -1 ? stat (file, &sbuf) : fstat (fd, &sbuf)) + the other one. If we have the file descriptor already, use fstat. + Otherwise, either we're in no-create mode (and hence didn't call open) + or FILE is inaccessible or a directory, so we have to use stat. */ + if ((valid_fd && fd != -1) ? fstat (fd, &sbuf) : stat (file, &sbuf)) { error (0, errno, "%s", file); close (fd); @@ -150,7 +139,7 @@ touch (const char *file) } } - if (fd != -1 && close (fd) < 0) + if (valid_fd && fd != -1 && close (fd) < 0) { error (0, errno, "%s", file); return 1; |