Module Name:    src
Committed By:   kre
Date:           Fri Feb  9 23:41:48 UTC 2024

Modified Files:
        src/usr.bin/touch: touch.1 touch.c

Log Message:
Add a -R option, which is identical to -r, except that if the reference file
is a symbolic link, the times to use are taken from those of the symbolic
link itself, instead of from the file it references.  If the reference file
is not a symbolic link, -R and -r are identical.

This allows the BUGS entry in the manual page to be removed.


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 src/usr.bin/touch/touch.1
cvs rdiff -u -r1.38 -r1.39 src/usr.bin/touch/touch.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/touch/touch.1
diff -u src/usr.bin/touch/touch.1:1.27 src/usr.bin/touch/touch.1:1.28
--- src/usr.bin/touch/touch.1:1.27	Thu Feb  8 02:54:13 2024
+++ src/usr.bin/touch/touch.1	Fri Feb  9 23:41:48 2024
@@ -1,4 +1,4 @@
-.\"	$NetBSD: touch.1,v 1.27 2024/02/08 02:54:13 kre Exp $
+.\"	$NetBSD: touch.1,v 1.28 2024/02/09 23:41:48 kre Exp $
 .\"
 .\" Copyright (c) 1991, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -32,7 +32,7 @@
 .\"
 .\"     @(#)touch.1	8.3 (Berkeley) 4/28/95
 .\"
-.Dd February 7, 2024
+.Dd February 9, 2024
 .Dt TOUCH 1
 .Os
 .Sh NAME
@@ -43,6 +43,7 @@
 .Op Fl acfhm
 .Op Fl d Ar posix-datetime|human-datetime
 .Op Fl Fl \|date Ar posix-datetime|human-datetime
+.Op Fl R Ar ref-file
 .Op Fl r Ar ref-file
 .Op Fl Fl \|reference Ar ref-file
 .Op Fl t Ar datetime
@@ -193,6 +194,7 @@ is not changed unless the
 .Fl a
 flag is also specified.
 .Pp
+.It Fl R Ar ref-file
 .It Fl r Ar ref-file
 .It Fl Fl \|reference Ar ref-file
 Use the access and modification times,
@@ -203,7 +205,15 @@ instead of the current time of day.
 If the
 .Ar ref-file
 is a symbolic link,
+then if the
+.Fl R
+form of this option was used,
+the times are taken from the symbolic link itself,
+otherwise
 the times are taken from the file referenced by it.
+If
+.Ar ref-file
+is not a symbolic link, all three forms are identical.
 .Pp
 .It Fl t Ar datetime
 Change the access and modification times of the
@@ -292,6 +302,7 @@ all conversions use decimal numbers.
 .Pp
 The
 .Fl d ,
+.Fl R ,
 .Fl r ,
 and
 .Fl t
@@ -301,10 +312,12 @@ each will be evaluated, and may cause an
 then the result from the last one specified is used.
 .Pp
 The options which specify any part of the time
-.Pq Fl d , Fl r , Fl t
+.Pq Fl d , Fl R, Fl r , Fl t
 apply to both the access and modification times
 (with
 .Fl r
+and
+.Fl R
 obtaining those values independently from the
 .Ar ref-file ) ,
 though which is actually applied depends upon
@@ -421,5 +434,3 @@ A
 .Nm
 utility appeared in
 .At v7 .
-.Sh BUGS
-A symbolic link can't be a reference file of access and/or modification time.

Index: src/usr.bin/touch/touch.c
diff -u src/usr.bin/touch/touch.c:1.38 src/usr.bin/touch/touch.c:1.39
--- src/usr.bin/touch/touch.c:1.38	Thu Feb  8 02:54:07 2024
+++ src/usr.bin/touch/touch.c	Fri Feb  9 23:41:48 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: touch.c,v 1.38 2024/02/08 02:54:07 kre Exp $	*/
+/*	$NetBSD: touch.c,v 1.39 2024/02/09 23:41:48 kre Exp $	*/
 
 /*
  * Copyright (c) 1993
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1993\
 #if 0
 static char sccsid[] = "@(#)touch.c	8.2 (Berkeley) 4/28/95";
 #endif
-__RCSID("$NetBSD: touch.c,v 1.38 2024/02/08 02:54:07 kre Exp $");
+__RCSID("$NetBSD: touch.c,v 1.39 2024/02/09 23:41:48 kre Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -65,7 +65,8 @@ __RCSID("$NetBSD: touch.c,v 1.38 2024/02
 static void	stime_arg0(const char *, struct timespec *);
 static void	stime_arg1(char *, struct timespec *);
 static void	stime_arg2(const char *, int, struct timespec *);
-static void	stime_file(const char *, struct timespec *);
+static void	stime_file(const char *, struct timespec *,
+		   int (const char *, struct stat *));
 static int	stime_posix(const char *, struct timespec *);
 static int	difftm(const struct tm *, const struct tm *);
 __dead static void	usage(void);
@@ -101,7 +102,7 @@ main(int argc, char *argv[])
 	if (clock_gettime(CLOCK_REALTIME, &ts[0]))
 		err(1, "clock_gettime");
 
-	while ((ch = getopt_long(argc, argv, "acd:fhmr:t:", touch_longopts,
+	while ((ch = getopt_long(argc, argv, "acd:fhmR:r:t:", touch_longopts,
 	    NULL)) != -1)
 		switch (ch) {
 		case 'a':
@@ -123,9 +124,13 @@ main(int argc, char *argv[])
 		case 'm':
 			mflag = 1;
 			break;
+		case 'R':
+			timeset = 1;
+			stime_file(optarg, ts, lstat);
+			break;
 		case 'r':
 			timeset = 1;
-			stime_file(optarg, ts);
+			stime_file(optarg, ts, stat);
 			break;
 		case 't':
 			timeset = 1;
@@ -335,11 +340,12 @@ stime_arg2(const char *arg, int year, st
 }
 
 static void
-stime_file(const char *fname, struct timespec *tsp)
+stime_file(const char *fname, struct timespec *tsp,
+    int statfunc(const char *, struct stat *))
 {
 	struct stat sb;
 
-	if (stat(fname, &sb))
+	if (statfunc(fname, &sb))
 		err(1, "%s", fname);
 	tsp[0] = sb.st_atimespec;
 	tsp[1] = sb.st_mtimespec;
@@ -521,7 +527,7 @@ static void
 usage(void)
 {
 	(void)fprintf(stderr,
-	    "Usage: %s [-acfhm] [-d|--date datetime] [-r|--reference file]"
+	    "Usage: %s [-acfhm] [-d|--date datetime] [-R|-r|--reference file]"
 	    " [-t time] file ...\n", getprogname());
 	exit(EXIT_FAILURE);
 }

Reply via email to