From a2ccfc7be5e7ffe2740c49d0d68a73597ccd7402 Mon Sep 17 00:00:00 2001 From: obrien Date: Sat, 22 Sep 2001 18:45:03 +0000 Subject: Add the 'h' flag to operate on symlinks rather than what they point to. --- usr.bin/touch/touch.1 | 5 ++++- usr.bin/touch/touch.c | 20 +++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) (limited to 'usr.bin/touch') 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); } -- cgit v1.1