git: aa69e0f21263 - main - touch: Allow setting the timestamp to -1.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Wed, 27 Mar 2024 11:26:18 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=aa69e0f212630bd6d4ec1c3c54e117be16653e8a

commit aa69e0f212630bd6d4ec1c3c54e117be16653e8a
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2024-03-27 10:03:40 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2024-03-27 10:03:40 +0000

    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
---
 usr.bin/touch/touch.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/usr.bin/touch/touch.c b/usr.bin/touch/touch.c
index b43420e4f3ef..70257e320a60 100644
--- a/usr.bin/touch/touch.c
+++ b/usr.bin/touch/touch.c
@@ -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. */