Add the 'h' flag to operate on symlinks rather than what they point to.

This commit is contained in:
David E. O'Brien 2001-09-22 18:45:03 +00:00
parent f066622bdd
commit 842d1c6c96
2 changed files with 19 additions and 6 deletions

View File

@ -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

View File

@ -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);
}