touch: Allow setting the timestamp to -1.

Note that VFS internally interprets a timestamp of -1 as “do not set”,
so this has no effect, but at least touch won't incorrectly reject the
given date / time (1969-12-31 23:59:59 UTC) as invalid.

While here, fix some style issues.

MFC after:	1 week
Reviewed by:	allanjude
Differential Revision:	https://reviews.freebsd.org/D44504
This commit is contained in:
Dag-Erling Smørgrav 2024-03-27 11:03:40 +01:00
parent 7534109d13
commit aa69e0f212

View file

@ -232,7 +232,7 @@ stime_arg1(const char *arg, struct timespec *tvp)
}
yearset = 0;
switch(strlen(arg)) {
switch (strlen(arg)) {
case 12: /* CCYYMMDDhhmm */
t->tm_year = ATOI2(arg);
t->tm_year *= 100;
@ -263,15 +263,17 @@ stime_arg1(const char *arg, struct timespec *tvp)
}
t->tm_isdst = -1; /* Figure out DST. */
t->tm_yday = -1;
tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
if (tvp[0].tv_sec == -1)
if (t->tm_yday == -1)
goto terr;
tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
return;
terr:
errx(1, "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
errx(1, "out of range or illegal time specification: "
"[[CC]YY]MMDDhhmm[.SS]");
}
static void
@ -296,10 +298,11 @@ stime_arg2(const char *arg, int year, struct timespec *tvp)
}
t->tm_isdst = -1; /* Figure out DST. */
t->tm_yday = -1;
tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
if (tvp[0].tv_sec == -1)
errx(1,
"out of range or illegal time specification: MMDDhhmm[yy]");
if (t->tm_yday == -1)
errx(1, "out of range or illegal time specification: "
"MMDDhhmm[yy]");
tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
}
@ -339,13 +342,17 @@ stime_darg(const char *arg, struct timespec *tvp)
if (*p != '\0')
goto bad;
t.tm_yday = -1;
tvp[0].tv_sec = isutc ? timegm(&t) : mktime(&t);
if (t.tm_yday == -1)
goto bad;
tvp[1] = tvp[0];
return;
bad:
errx(1, "out of range or illegal time specification: YYYY-MM-DDThh:mm:SS[.frac][tz]");
errx(1, "out of range or illegal time specification: "
"YYYY-MM-DDThh:mm:SS[.frac][tz]");
}
/* Calculate a time offset in seconds, given an arg of the format [-]HHMMSS. */