[hackers] [sbase] touch: Use both atime and mtime of reference file || Michael Forney

From: <git_AT_suckless.org>
Date: Sat, 16 May 2015 14:34:05 +0200 (CEST)

commit b4adb4bb870c32b91bd5470024aaa95c7e90724e
Author: Michael Forney <mforney_AT_mforney.org>
Date: Sat May 16 04:57:27 2015 +0000

    touch: Use both atime and mtime of reference file

diff --git a/touch.c b/touch.c
index 2789716..fb04142 100644
--- a/touch.c
+++ b/touch.c
_AT_@ -14,14 +14,13 @@
 static int aflag;
 static int cflag;
 static int mflag;
-static struct timespec t;
+static struct timespec times[2];
 
 static void
 touch(const char *file)
 {
         int fd;
         struct stat st;
- struct timespec times[2];
         int r;
 
         if ((r = stat(file, &st)) < 0) {
_AT_@ -30,8 +29,10 @@ touch(const char *file)
                 if (cflag)
                         return;
         } else if (!r) {
- times[0] = aflag ? t : st.st_atim;
- times[1] = mflag ? t : st.st_mtim;
+ if (!aflag)
+ times[0] = st.st_atim;
+ if (!mflag)
+ times[1] = st.st_mtim;
                 if (utimensat(AT_FDCWD, file, times, 0) < 0)
                         eprintf("utimensat %s:", file);
                 return;
_AT_@ -117,8 +118,8 @@ int
 main(int argc, char *argv[])
 {
         struct stat st;
- char *ref;
- clock_gettime(CLOCK_REALTIME, &t);
+ char *ref = NULL;
+ clock_gettime(CLOCK_REALTIME, &times[0]);
 
         ARGBEGIN {
         case 'a':
_AT_@ -129,7 +130,7 @@ main(int argc, char *argv[])
                 break;
         case 'd':
         case 't':
- t.tv_sec = parsetime(EARGF(usage()), t.tv_sec);
+ times[0].tv_sec = parsetime(EARGF(usage()), times[0].tv_sec);
                 break;
         case 'm':
                 mflag = 1;
_AT_@ -138,10 +139,11 @@ main(int argc, char *argv[])
                 ref = EARGF(usage());
                 if (stat(ref, &st) < 0)
                         eprintf("stat '%s':", ref);
- t = st.st_mtim;
+ times[0] = st.st_atim;
+ times[1] = st.st_mtim;
                 break;
         case 'T':
- t.tv_sec = estrtonum(EARGF(usage()), 0, LLONG_MAX);
+ times[0].tv_sec = estrtonum(EARGF(usage()), 0, LLONG_MAX);
                 break;
         default:
                 usage();
_AT_@ -151,6 +153,8 @@ main(int argc, char *argv[])
                 usage();
         if (!aflag && !mflag)
                 aflag = mflag = 1;
+ if (!ref)
+ times[1] = times[0];
 
         for (; *argv; argc--, argv++)
                 touch(*argv);
Received on Sat May 16 2015 - 14:34:05 CEST

This archive was generated by hypermail 2.3.0 : Sat May 16 2015 - 14:36:21 CEST