#include <sys/cdefs.h>
-__RCSID("$FreeBSD: src/usr.bin/touch/touch.c,v 1.20 2002/09/04 23:29:07 dwmalone Exp $");
+__FBSDID("$FreeBSD: src/usr.bin/touch/touch.c,v 1.25 2010/03/28 13:16:08 ed Exp $");
#ifndef lint
-static const char copyright[] =
+__used static const char copyright[] =
"@(#) Copyright (c) 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif
#ifndef lint
-static const char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93";
+__used static const char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93";
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
+#include <sys/attr.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
+#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
+typedef struct {
+ struct timespec mtime;
+ struct timespec atime;
+} set_ts;
+
int rw(char *, struct stat *, int);
-void stime_arg1(char *, struct timeval *);
-void stime_arg2(char *, int, struct timeval *);
-void stime_file(char *, struct timeval *);
-void usage(void);
+void stime_arg1(char *, set_ts *);
+void stime_arg2(char *, int, set_ts *);
+void stime_file(char *, set_ts *);
+int timeoffset(char *);
+void usage(char *);
int
main(int argc, char *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;
+ int Aflag, aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset;
char *p;
-
- aflag = cflag = fflag = mflag = timeset = 0;
+ char *myname;
+ struct attrlist ts_req = {
+ .bitmapcount = ATTR_BIT_MAP_COUNT,
+ .commonattr = ATTR_CMN_MODTIME | ATTR_CMN_ACCTIME,
+ };
+ set_ts ts_struct = {};
+
+ myname = basename(argv[0]);
+ Aflag = aflag = cflag = fflag = mflag = timeset = 0;
stat_f = stat;
utimes_f = utimes;
- if (gettimeofday(&tv[0], NULL))
- err(1, "gettimeofday");
+ if (clock_gettime(CLOCK_REALTIME, &ts_struct.mtime))
+ err(1, "clock_gettime");
+ ts_struct.atime = ts_struct.mtime;
-#ifndef __APPLE__
- while ((ch = getopt(argc, argv, "acfhmr:t:")) != -1)
-#else
- while ((ch = getopt(argc, argv, "acfmr:t:")) != -1)
-#endif
+ while ((ch = getopt(argc, argv, "A:acfhmr:t:")) != -1)
switch(ch) {
+ case 'A':
+ Aflag = timeoffset(optarg);
+ break;
case 'a':
aflag = 1;
break;
case 'f':
fflag = 1;
break;
-#ifndef __APPLE__
case 'h':
cflag = 1;
stat_f = lstat;
utimes_f = lutimes;
break;
-#endif /* __APPLE__ */
case 'm':
mflag = 1;
break;
case 'r':
timeset = 1;
- stime_file(optarg, tv);
+ stime_file(optarg, &ts_struct);
break;
case 't':
timeset = 1;
- stime_arg1(optarg, tv);
+ stime_arg1(optarg, &ts_struct);
break;
case '?':
default:
- usage();
+ usage(myname);
}
argc -= optind;
argv += optind;
- /* Default is both -a and -m. */
if (aflag == 0 && mflag == 0)
aflag = mflag = 1;
- /*
- * If no -r or -t flag, at least two operands, the first of which
- * is an 8 or 10 digit number, use the obsolete time specification.
- */
- if (!timeset && argc > 1) {
- (void)strtol(argv[0], &p, 10);
- len = p - argv[0];
- if (*p == '\0' && (len == 8 || len == 10)) {
- timeset = 1;
- stime_arg2(*argv++, len == 10, tv);
+ if (timeset) {
+ if (Aflag) {
+ /*
+ * We're setting the time to an offset from a specified
+ * time. God knows why, but it means that we can set
+ * that time once and for all here.
+ */
+ if (aflag)
+ ts_struct.atime.tv_sec += Aflag;
+ if (mflag)
+ ts_struct.mtime.tv_sec += Aflag;
+ Aflag = 0; /* done our job */
+ }
+ } else {
+ /*
+ * If no -r or -t flag, at least two operands, the first of
+ * which is an 8 or 10 digit number, use the obsolete time
+ * specification, otherwise use the current time.
+ */
+ if (argc > 1) {
+ strtol(argv[0], &p, 10);
+ len = p - argv[0];
+ if (*p == '\0' && (len == 8 || len == 10)) {
+ timeset = 1;
+ stime_arg2(*argv++, len == 10, &ts_struct);
+ }
}
}
- /* Otherwise use the current time of day. */
- if (!timeset)
- tv[1] = tv[0];
-
if (*argv == NULL)
- usage();
+ usage(myname);
+
+ if (Aflag)
+ cflag = 1;
for (rval = 0; *argv; ++argv) {
/* See if the file exists. */
if (stat_f(*argv, &sb) != 0) {
+ if (errno != ENOENT) {
+ rval = 1;
+ warn("%s", *argv);
+ continue;
+ }
if (!cflag) {
/* Create the file. */
fd = open(*argv,
}
if (!aflag)
- TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec);
+ ts_struct.atime = sb.st_atimespec;
if (!mflag)
- TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
+ ts_struct.mtime = sb.st_mtimespec;
- /* Try utimes(2). */
- if (!utimes_f(*argv, tv))
+ /*
+ * We're adjusting the times based on the file times, not a
+ * specified time (that gets handled above).
+ */
+ if (Aflag) {
+ if (aflag) {
+ ts_struct.atime = sb.st_atimespec;
+ ts_struct.atime.tv_sec += Aflag;
+ }
+ if (mflag) {
+ ts_struct.mtime = sb.st_mtimespec;
+ ts_struct.mtime.tv_sec += Aflag;
+ }
+ }
+
+ /* Try setattrlist(2). */
+ if (!setattrlist(*argv, &ts_req, &ts_struct, sizeof(ts_struct), 0))
continue;
/* If the user specified a time, nothing else we can do. */
- if (timeset) {
+ if (timeset || Aflag) {
rval = 1;
warn("%s", *argv);
+ continue;
}
/*
continue;
/* Try reading/writing. */
- if (!S_ISLNK(sb.st_mode) && !S_ISDIR(sb.st_mode) &&
- rw(*argv, &sb, fflag))
+ if (!S_ISLNK(sb.st_mode) && !S_ISDIR(sb.st_mode)) {
+ if (rw(*argv, &sb, fflag))
+ rval = 1;
+ } else {
rval = 1;
- else
warn("%s", *argv);
+ }
}
exit(rval);
}
#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
void
-stime_arg1(char *arg, struct timeval *tvp)
+stime_arg1(char *arg, set_ts *tsp)
{
time_t now;
struct tm *t;
int yearset;
char *p;
/* Start with the current time. */
- now = tvp[0].tv_sec;
+ now = tsp->atime.tv_sec;
if ((t = localtime(&now)) == NULL)
err(1, "localtime");
/* [[CC]YY]MMDDhhmm[.SS] */
}
t->tm_isdst = -1; /* Figure out DST. */
- tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
- if (tvp[0].tv_sec == -1)
+ tsp->atime.tv_sec = tsp->mtime.tv_sec = mktime(t);
+ if (tsp->atime.tv_sec == -1)
terr: errx(1,
"out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
- tvp[0].tv_usec = tvp[1].tv_usec = 0;
+ tsp->atime.tv_nsec = tsp->mtime.tv_nsec = 0;
}
void
-stime_arg2(char *arg, int year, struct timeval *tvp)
+stime_arg2(char *arg, int year, set_ts *tsp)
{
time_t now;
struct tm *t;
/* Start with the current time. */
- now = tvp[0].tv_sec;
+ now = tsp->atime.tv_sec;
if ((t = localtime(&now)) == NULL)
err(1, "localtime");
}
t->tm_isdst = -1; /* Figure out DST. */
- tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
- if (tvp[0].tv_sec == -1)
+ tsp->atime.tv_sec = tsp->mtime.tv_sec = mktime(t);
+ if (tsp->atime.tv_sec == -1)
errx(1,
"out of range or illegal time specification: MMDDhhmm[yy]");
- tvp[0].tv_usec = tvp[1].tv_usec = 0;
+ tsp->atime.tv_nsec = tsp->mtime.tv_nsec = 0;
+}
+
+/* Calculate a time offset in seconds, given an arg of the format [-]HHMMSS. */
+int
+timeoffset(char *arg)
+{
+ int offset;
+ int isneg;
+
+ offset = 0;
+ isneg = *arg == '-';
+ if (isneg)
+ arg++;
+ switch (strlen(arg)) {
+ default: /* invalid */
+ errx(1, "Invalid offset spec, must be [-][[HH]MM]SS");
+
+ case 6: /* HHMMSS */
+ offset = ATOI2(arg);
+ /* FALLTHROUGH */
+ case 4: /* MMSS */
+ offset = offset * 60 + ATOI2(arg);
+ /* FALLTHROUGH */
+ case 2: /* SS */
+ offset = offset * 60 + ATOI2(arg);
+ }
+ if (isneg)
+ return (-offset);
+ else
+ return (offset);
}
void
-stime_file(char *fname, struct timeval *tvp)
+stime_file(char *fname, set_ts *ts_struct)
{
struct stat sb;
if (stat(fname, &sb))
err(1, "%s", fname);
- TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec);
- TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec);
+ ts_struct->atime = sb.st_atimespec;
+ ts_struct->mtime = sb.st_mtimespec;
}
int
}
void
-usage(void)
+usage(char *myname)
{
-#ifndef __APPLE__
- (void)fprintf(stderr, "usage: touch [-acfhm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...\n");
-#else
- (void)fprintf(stderr, "usage: touch [-acfm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...\n");
-#endif
+ fprintf(stderr, "usage:\n" "%s [-A [-][[hh]mm]SS] [-acfhm] [-r file] "
+ "[-t [[CC]YY]MMDDhhmm[.SS]] file ...\n", myname);
exit(1);
}