summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorobrien <obrien@FreeBSD.org>2001-09-22 18:45:03 +0000
committerobrien <obrien@FreeBSD.org>2001-09-22 18:45:03 +0000
commita2ccfc7be5e7ffe2740c49d0d68a73597ccd7402 (patch)
tree702a5a0324e29c17786f26baa4ee4e26a641f348
parentc167877c16412949c9a85f4856790fe70a8837c7 (diff)
downloadFreeBSD-src-a2ccfc7be5e7ffe2740c49d0d68a73597ccd7402.zip
FreeBSD-src-a2ccfc7be5e7ffe2740c49d0d68a73597ccd7402.tar.gz
Add the 'h' flag to operate on symlinks rather than what they point to.
-rw-r--r--usr.bin/touch/touch.15
-rw-r--r--usr.bin/touch/touch.c20
2 files changed, 19 insertions, 6 deletions
diff --git a/usr.bin/touch/touch.1 b/usr.bin/touch/touch.1
index 73e537a..46ed028 100644
--- a/usr.bin/touch/touch.1
+++ b/usr.bin/touch/touch.1
@@ -43,7 +43,7 @@
.Nd change file access and modification times
.Sh SYNOPSIS
.Nm
-.Op Fl acfm
+.Op Fl acfhm
.Op Fl r Ar file
.Op Fl t Ar [[CC]YY]MMDDhhmm[.SS]
.Ar
@@ -70,6 +70,9 @@ No error messages are displayed and the exit value is not affected.
.It Fl f
Attempt to force the update, even if the file permissions do not
currently permit it.
+.It Fl h
+If the file is a symbolic link, change the times of the link
+itself rather than the file that the link points to.
.It Fl m
Change the modification time of the file.
The access time of the file is not changed unless the
diff --git a/usr.bin/touch/touch.c b/usr.bin/touch/touch.c
index f8e6cce..d5c14cb 100644
--- a/usr.bin/touch/touch.c
+++ b/usr.bin/touch/touch.c
@@ -72,14 +72,18 @@ main(argc, argv)
{
struct stat sb;
struct timeval tv[2];
+ int (*stat_f)(const char *, struct stat *);
+ int (*utimes_f)(const char *, const struct timeval *);
int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset;
char *p;
aflag = cflag = fflag = mflag = timeset = 0;
+ stat_f = stat;
+ utimes_f = utimes;
if (gettimeofday(&tv[0], NULL))
err(1, "gettimeofday");
- while ((ch = getopt(argc, argv, "acfmr:t:")) != -1)
+ while ((ch = getopt(argc, argv, "acfhmr:t:")) != -1)
switch(ch) {
case 'a':
aflag = 1;
@@ -90,6 +94,11 @@ main(argc, argv)
case 'f':
fflag = 1;
break;
+ case 'h':
+ cflag = 1;
+ stat_f = lstat;
+ utimes_f = lutimes;
+ break;
case 'm':
mflag = 1;
break;
@@ -134,7 +143,8 @@ main(argc, argv)
for (rval = 0; *argv; ++argv) {
/* See if the file exists. */
- if (stat(*argv, &sb)) {
+ if (stat_f(*argv, &sb) != 0) {
+fprintf(stderr, "not exist\n");
if (!cflag) {
/* Create the file. */
fd = open(*argv,
@@ -158,7 +168,7 @@ main(argc, argv)
TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
/* Try utimes(2). */
- if (!utimes(*argv, tv))
+ if (!utimes_f(*argv, tv))
continue;
/* If the user specified a time, nothing else we can do. */
@@ -173,7 +183,7 @@ main(argc, argv)
* The permission checks are different, too, in that the
* ability to write the file is sufficient. Take a shot.
*/
- if (!utimes(*argv, NULL))
+ if (!utimes_f(*argv, NULL))
continue;
/* Try reading/writing. */
@@ -349,6 +359,6 @@ err: rval = 1;
void
usage()
{
- (void)fprintf(stderr, "usage: touch [-acfm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...\n");
+ (void)fprintf(stderr, "usage: touch [-acfhm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...\n");
exit(1);
}
OpenPOWER on IntegriCloud