1
0
mirror of git://git.suckless.org/sbase synced 2024-12-23 15:34:38 +00:00

touch: Handle nanosecond timestamps

This commit is contained in:
Michael Forney 2015-05-16 04:57:26 +00:00 committed by sin
parent 1d28fbd6cf
commit 4132561c17

24
touch.c
View File

@ -14,14 +14,14 @@
static int aflag; static int aflag;
static int cflag; static int cflag;
static int mflag; static int mflag;
static time_t t; static struct timespec t;
static void static void
touch(const char *file) touch(const char *file)
{ {
int fd; int fd;
struct stat st; struct stat st;
struct utimbuf ut; struct timespec times[2];
int r; int r;
if ((r = stat(file, &st)) < 0) { if ((r = stat(file, &st)) < 0) {
@ -30,10 +30,10 @@ touch(const char *file)
if (cflag) if (cflag)
return; return;
} else if (!r) { } else if (!r) {
ut.actime = aflag ? t : st.st_atime; times[0] = aflag ? t : st.st_atim;
ut.modtime = mflag ? t : st.st_mtime; times[1] = mflag ? t : st.st_mtim;
if (utime(file, &ut) < 0) if (utimensat(AT_FDCWD, file, times, 0) < 0)
eprintf("utime %s:", file); eprintf("utimensat %s:", file);
return; return;
} }
@ -118,7 +118,7 @@ main(int argc, char *argv[])
{ {
struct stat st; struct stat st;
char *ref; char *ref;
t = time(NULL); clock_gettime(CLOCK_REALTIME, &t);
ARGBEGIN { ARGBEGIN {
case 'a': case 'a':
@ -128,7 +128,8 @@ main(int argc, char *argv[])
cflag = 1; cflag = 1;
break; break;
case 'd': case 'd':
t = parsetime(EARGF(usage()), t); case 't':
t.tv_sec = parsetime(EARGF(usage()), t.tv_sec);
break; break;
case 'm': case 'm':
mflag = 1; mflag = 1;
@ -137,13 +138,10 @@ main(int argc, char *argv[])
ref = EARGF(usage()); ref = EARGF(usage());
if (stat(ref, &st) < 0) if (stat(ref, &st) < 0)
eprintf("stat '%s':", ref); eprintf("stat '%s':", ref);
t = st.st_mtime; t = st.st_mtim;
break; break;
case 'T': case 'T':
t = estrtonum(EARGF(usage()), 0, LLONG_MAX); t.tv_sec = estrtonum(EARGF(usage()), 0, LLONG_MAX);
break;
case 't':
t = parsetime(EARGF(usage()), t);
break; break;
default: default:
usage(); usage();