[Midnightbsd-cvs] src [11315] trunk/usr.bin/touch/touch.c: sync touch with freebsd.
laffer1 at midnightbsd.org
laffer1 at midnightbsd.org
Wed Jul 4 10:46:57 EDT 2018
Revision: 11315
http://svnweb.midnightbsd.org/src/?rev=11315
Author: laffer1
Date: 2018-07-04 10:46:57 -0400 (Wed, 04 Jul 2018)
Log Message:
-----------
sync touch with freebsd. remove f flag
Modified Paths:
--------------
trunk/usr.bin/touch/Makefile
trunk/usr.bin/touch/touch.1
trunk/usr.bin/touch/touch.c
Property Changed:
----------------
trunk/usr.bin/touch/touch.1
Modified: trunk/usr.bin/touch/Makefile
===================================================================
--- trunk/usr.bin/touch/Makefile 2018-07-04 14:45:14 UTC (rev 11314)
+++ trunk/usr.bin/touch/Makefile 2018-07-04 14:46:57 UTC (rev 11315)
@@ -1,5 +1,6 @@
+# $MidnightBSD$
# @(#)Makefile 8.1 (Berkeley) 6/6/93
-# $MidnightBSD$
+# $FreeBSD: stable/10/usr.bin/touch/Makefile 87706 2001-12-11 23:38:24Z markm $
PROG= touch
Modified: trunk/usr.bin/touch/touch.1
===================================================================
--- trunk/usr.bin/touch/touch.1 2018-07-04 14:45:14 UTC (rev 11314)
+++ trunk/usr.bin/touch/touch.1 2018-07-04 14:46:57 UTC (rev 11315)
@@ -1,3 +1,4 @@
+.\" $MidnightBSD$
.\" Copyright (c) 1991, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
@@ -29,7 +30,7 @@
.\" SUCH DAMAGE.
.\"
.\" @(#)touch.1 8.3 (Berkeley) 4/28/95
-.\" $MidnightBSD$
+.\" $FreeBSD: stable/10/usr.bin/touch/touch.1 236852 2012-06-10 14:26:51Z jilles $
.\"
.Dd June 10, 2012
.Dt TOUCH 1
@@ -40,7 +41,7 @@
.Sh SYNOPSIS
.Nm
.Op Fl A Ar [-][[hh]mm]SS
-.Op Fl acfhm
+.Op Fl achm
.Op Fl r Ar file
.Op Fl t Ar [[CC]YY]MMDDhhmm[.SS]
.Op Fl d Ar YYYY-MM-DDThh:mm:SS[.frac][tz]
@@ -112,9 +113,6 @@
.Nm
utility does not treat this as an error.
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 d
Change the access and modification times to the specified time instead
of the current time of day.
Property changes on: trunk/usr.bin/touch/touch.1
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Modified: trunk/usr.bin/touch/touch.c
===================================================================
--- trunk/usr.bin/touch/touch.c 2018-07-04 14:45:14 UTC (rev 11314)
+++ trunk/usr.bin/touch/touch.c 2018-07-04 14:46:57 UTC (rev 11315)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
/*
* Copyright (c) 1993
* The Regents of the University of California. All rights reserved.
@@ -29,7 +30,7 @@
#include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/usr.bin/touch/touch.c 297783 2016-04-10 15:24:07Z jilles $");
#ifndef lint
static const char copyright[] =
@@ -56,31 +57,28 @@
#include <time.h>
#include <unistd.h>
-int rw(char *, struct stat *, int);
-void stime_arg1(char *, struct timeval *);
-void stime_arg2(char *, int, struct timeval *);
-void stime_darg(char *, struct timeval *);
-void stime_file(char *, struct timeval *);
-int timeoffset(char *);
-void usage(char *);
+static void stime_arg1(const char *, struct timespec *);
+static void stime_arg2(const char *, int, struct timespec *);
+static void stime_darg(const char *, struct timespec *);
+static void stime_file(const char *, struct timespec *);
+static int timeoffset(const char *);
+static void usage(const 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, aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset;
+ struct timespec ts[2];
+ int atflag;
+ int Aflag, aflag, cflag, mflag, ch, fd, len, rval, timeset;
char *p;
char *myname;
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");
+ Aflag = aflag = cflag = mflag = timeset = 0;
+ atflag = 0;
+ ts[0].tv_sec = ts[1].tv_sec = 0;
+ ts[0].tv_nsec = ts[1].tv_nsec = UTIME_NOW;
while ((ch = getopt(argc, argv, "A:acd:fhmr:t:")) != -1)
switch(ch) {
@@ -95,15 +93,14 @@
break;
case 'd':
timeset = 1;
- stime_darg(optarg, tv);
+ stime_darg(optarg, ts);
break;
case 'f':
- fflag = 1;
+ /* No-op for compatibility. */
break;
case 'h':
cflag = 1;
- stat_f = lstat;
- utimes_f = lutimes;
+ atflag = AT_SYMLINK_NOFOLLOW;
break;
case 'm':
mflag = 1;
@@ -110,13 +107,12 @@
break;
case 'r':
timeset = 1;
- stime_file(optarg, tv);
+ stime_file(optarg, ts);
break;
case 't':
timeset = 1;
- stime_arg1(optarg, tv);
+ stime_arg1(optarg, ts);
break;
- case '?':
default:
usage(myname);
}
@@ -134,9 +130,9 @@
* that time once and for all here.
*/
if (aflag)
- tv[0].tv_sec += Aflag;
+ ts[0].tv_sec += Aflag;
if (mflag)
- tv[1].tv_sec += Aflag;
+ ts[1].tv_sec += Aflag;
Aflag = 0; /* done our job */
}
} else {
@@ -150,13 +146,18 @@
len = p - argv[0];
if (*p == '\0' && (len == 8 || len == 10)) {
timeset = 1;
- stime_arg2(*argv++, len == 10, tv);
+ stime_arg2(*argv++, len == 10, ts);
}
}
/* Both times default to the same. */
- tv[1] = tv[0];
+ ts[1] = ts[0];
}
+ if (!aflag)
+ ts[0].tv_nsec = UTIME_OMIT;
+ if (!mflag)
+ ts[1].tv_nsec = UTIME_OMIT;
+
if (*argv == NULL)
usage(myname);
@@ -165,7 +166,7 @@
for (rval = 0; *argv; ++argv) {
/* See if the file exists. */
- if (stat_f(*argv, &sb) != 0) {
+ if (fstatat(AT_FDCWD, *argv, &sb, atflag) != 0) {
if (errno != ENOENT) {
rval = 1;
warn("%s", *argv);
@@ -188,11 +189,6 @@
continue;
}
- if (!aflag)
- TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atim);
- if (!mflag)
- TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtim);
-
/*
* We're adjusting the times based on the file times, not a
* specified time (that gets handled above).
@@ -199,43 +195,20 @@
*/
if (Aflag) {
if (aflag) {
- TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atim);
- tv[0].tv_sec += Aflag;
+ ts[0] = sb.st_atim;
+ ts[0].tv_sec += Aflag;
}
if (mflag) {
- TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtim);
- tv[1].tv_sec += Aflag;
+ ts[1] = sb.st_mtim;
+ ts[1].tv_sec += Aflag;
}
}
- /* Try utimes(2). */
- if (!utimes_f(*argv, tv))
+ if (!utimensat(AT_FDCWD, *argv, ts, atflag))
continue;
- /* If the user specified a time, nothing else we can do. */
- if (timeset || Aflag) {
- rval = 1;
- warn("%s", *argv);
- continue;
- }
-
- /*
- * System V and POSIX 1003.1 require that a NULL argument
- * set the access/modification times to the current time.
- * The permission checks are different, too, in that the
- * ability to write the file is sufficient. Take a shot.
- */
- if (!utimes_f(*argv, NULL))
- continue;
-
- /* Try reading/writing. */
- if (!S_ISLNK(sb.st_mode) && !S_ISDIR(sb.st_mode)) {
- if (rw(*argv, &sb, fflag))
- rval = 1;
- } else {
- rval = 1;
- warn("%s", *argv);
- }
+ rval = 1;
+ warn("%s", *argv);
}
exit(rval);
}
@@ -242,15 +215,15 @@
#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
-void
-stime_arg1(char *arg, struct timeval *tvp)
+static void
+stime_arg1(const char *arg, struct timespec *tvp)
{
time_t now;
struct tm *t;
int yearset;
char *p;
- /* Start with the current time. */
- now = tvp[0].tv_sec;
+
+ now = time(NULL);
if ((t = localtime(&now)) == NULL)
err(1, "localtime");
/* [[CC]YY]MMDDhhmm[.SS] */
@@ -297,19 +270,22 @@
t->tm_isdst = -1; /* Figure out DST. */
tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
if (tvp[0].tv_sec == -1)
-terr: errx(1,
- "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
+ goto terr;
- tvp[0].tv_usec = tvp[1].tv_usec = 0;
+ tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
+ return;
+
+terr:
+ errx(1, "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
}
-void
-stime_arg2(char *arg, int year, struct timeval *tvp)
+static void
+stime_arg2(const char *arg, int year, struct timespec *tvp)
{
time_t now;
struct tm *t;
- /* Start with the current time. */
- now = tvp[0].tv_sec;
+
+ now = time(NULL);
if ((t = localtime(&now)) == NULL)
err(1, "localtime");
@@ -330,11 +306,11 @@
errx(1,
"out of range or illegal time specification: MMDDhhmm[yy]");
- tvp[0].tv_usec = tvp[1].tv_usec = 0;
+ tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
}
-void
-stime_darg(char *arg, struct timeval *tvp)
+static void
+stime_darg(const char *arg, struct timespec *tvp)
{
struct tm t = { .tm_sec = 0 };
const char *fmt, *colon;
@@ -341,7 +317,7 @@
char *p;
int val, isutc = 0;
- tvp[0].tv_usec = 0;
+ tvp[0].tv_nsec = 0;
t.tm_isdst = -1;
colon = strchr(arg, ':');
if (colon == NULL || strchr(colon + 1, ':') == NULL)
@@ -354,9 +330,9 @@
/* POSIX: must have at least one digit after dot */
if ((*p == '.' || *p == ',') && isdigit((unsigned char)p[1])) {
p++;
- val = 100000;
+ val = 100000000;
while (isdigit((unsigned char)*p)) {
- tvp[0].tv_usec += val * (*p - '0');
+ tvp[0].tv_nsec += val * (*p - '0');
p++;
val /= 10;
}
@@ -379,7 +355,7 @@
/* Calculate a time offset in seconds, given an arg of the format [-]HHMMSS. */
int
-timeoffset(char *arg)
+timeoffset(const char *arg)
{
int offset;
int isneg;
@@ -407,70 +383,21 @@
return (offset);
}
-void
-stime_file(char *fname, struct timeval *tvp)
+static void
+stime_file(const char *fname, struct timespec *tsp)
{
struct stat sb;
if (stat(fname, &sb))
err(1, "%s", fname);
- TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atim);
- TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtim);
+ tsp[0] = sb.st_atim;
+ tsp[1] = sb.st_mtim;
}
-int
-rw(char *fname, struct stat *sbp, int force)
+static void
+usage(const char *myname)
{
- int fd, needed_chmod, rval;
- u_char byte;
-
- /* Try regular files. */
- if (!S_ISREG(sbp->st_mode)) {
- warnx("%s: %s", fname, strerror(EFTYPE));
- return (1);
- }
-
- needed_chmod = rval = 0;
- if ((fd = open(fname, O_RDWR, 0)) == -1) {
- if (!force || chmod(fname, DEFFILEMODE))
- goto err;
- if ((fd = open(fname, O_RDWR, 0)) == -1)
- goto err;
- needed_chmod = 1;
- }
-
- if (sbp->st_size != 0) {
- if (read(fd, &byte, sizeof(byte)) != sizeof(byte))
- goto err;
- if (lseek(fd, (off_t)0, SEEK_SET) == -1)
- goto err;
- if (write(fd, &byte, sizeof(byte)) != sizeof(byte))
- goto err;
- } else {
- if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) {
-err: rval = 1;
- warn("%s", fname);
- } else if (ftruncate(fd, (off_t)0)) {
- rval = 1;
- warn("%s: file modified", fname);
- }
- }
-
- if (close(fd) && rval != 1) {
- rval = 1;
- warn("%s", fname);
- }
- if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) {
- rval = 1;
- warn("%s: permissions modified", fname);
- }
- return (rval);
-}
-
-void
-usage(char *myname)
-{
- fprintf(stderr, "usage: %s [-A [-][[hh]mm]SS] [-acfhm] [-r file] "
+ fprintf(stderr, "usage: %s [-A [-][[hh]mm]SS] [-achm] [-r file] "
"[-t [[CC]YY]MMDDhhmm[.SS]]\n"
" [-d YYYY-MM-DDThh:mm:SS[.frac][tz]] "
"file ...\n", myname);
More information about the Midnightbsd-cvs
mailing list