sbase

suckless unix tools
git clone git://git.2f30.org/sbase.git
Log | Files | Refs | README | LICENSE

commit 1db000a1932741ef0e304453da4ded5c02b70d42
parent 44133fb56058cfd73d5c49636802cdc9966030fc
Author: Michael Forney <mforney@mforney.org>
Date:   Fri Jul  8 10:24:14 -0700

touch: Avoid unnecessary call to stat

Now, we first try a utimensat. If it succeeds, we are done, if not (and no -c
flag), create the file, then set the times with futimens.

Diffstat:
touch.c | 23+++++++++--------------
1 file changed, 9 insertions(+), 14 deletions(-)
diff --git a/touch.c b/touch.c @@ -18,25 +18,20 @@ static struct timespec times[2] = {{.tv_nsec = UTIME_NOW}}; static void touch(const char *file) { - int fd; - struct stat st; + int fd, ret; - if (stat(file, &st) < 0) { - if (errno != ENOENT) - eprintf("stat %s:", file); - if (cflag) - return; - } else { - if (utimensat(AT_FDCWD, file, times, 0) < 0) - eprintf("utimensat %s:", file); + if (utimensat(AT_FDCWD, file, times, 0) == 0) + return; + if (errno != ENOENT) + eprintf("utimensat %s:", file); + if (cflag) return; - } - if ((fd = open(file, O_WRONLY | O_CREAT | O_EXCL, 0666)) < 0) eprintf("open %s:", file); + ret = futimens(fd, times); close(fd); - - touch(file); + if (ret < 0) + eprintf("futimens %s:", file); } static time_t