diff options
-rw-r--r-- | touch.c | 18 |
1 files changed, 12 insertions, 6 deletions
@@ -39,20 +39,26 @@ enum times { static int touch(const char *path, struct timespec times[2], int create) { - int fd = open(path, (create ? O_CREAT : 0), 0644); + if (access(path, F_OK) == 0) { + return utimensat(AT_FDCWD, path, times, 0); + } + + if (!create) { + return 0; + } + + int fd = creat(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if (fd == -1) { - if (!create && errno == ENOENT) { - return 0; - } fprintf(stderr, "touch: %s: %s\n", path, strerror(errno)); return 1; } + if (futimens(fd, times) != 0) { fprintf(stderr, "touch: %s: %s\n", path, strerror(errno)); close(fd); return 1; } - close(fd); + return 0; } @@ -117,7 +123,7 @@ int main(int argc, char *argv[]) switch (how) { case CURRENT: times[0].tv_nsec = UTIME_NOW; - times[0].tv_nsec = UTIME_NOW; + times[1].tv_nsec = UTIME_NOW; break; case DATE: /* TODO */ |