Module Name: src
Committed By: christos
Date: Wed Jul 25 01:23:46 UTC 2012
Modified Files:
src/usr.bin/touch: Makefile touch.1 touch.c
Log Message:
add an option to parse human dates.
To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/usr.bin/touch/Makefile
cvs rdiff -u -r1.19 -r1.20 src/usr.bin/touch/touch.1
cvs rdiff -u -r1.30 -r1.31 src/usr.bin/touch/touch.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/usr.bin/touch/Makefile
diff -u src/usr.bin/touch/Makefile:1.3 src/usr.bin/touch/Makefile:1.4
--- src/usr.bin/touch/Makefile:1.3 Wed Dec 7 04:19:47 1994
+++ src/usr.bin/touch/Makefile Tue Jul 24 21:23:46 2012
@@ -1,6 +1,8 @@
-# $NetBSD: Makefile,v 1.3 1994/12/07 09:19:47 jtc Exp $
+# $NetBSD: Makefile,v 1.4 2012/07/25 01:23:46 christos Exp $
# @(#)Makefile 8.1 (Berkeley) 6/6/93
PROG= touch
+LDADD+= -lutil
+DPADD+= ${LIBUTIL}
.include <bsd.prog.mk>
Index: src/usr.bin/touch/touch.1
diff -u src/usr.bin/touch/touch.1:1.19 src/usr.bin/touch/touch.1:1.20
--- src/usr.bin/touch/touch.1:1.19 Wed Jun 20 05:56:18 2012
+++ src/usr.bin/touch/touch.1 Tue Jul 24 21:23:46 2012
@@ -1,4 +1,4 @@
-.\" $NetBSD: touch.1,v 1.19 2012/06/20 09:56:18 wiz Exp $
+.\" $NetBSD: touch.1,v 1.20 2012/07/25 01:23:46 christos Exp $
.\"
.\" Copyright (c) 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -32,7 +32,7 @@
.\"
.\" @(#)touch.1 8.3 (Berkeley) 4/28/95
.\"
-.Dd June 20, 2012
+.Dd July 24, 2012
.Dt TOUCH 1
.Os
.Sh NAME
@@ -43,6 +43,7 @@
.Op Fl acfhm
.Op Fl r Ar file
.Op Fl t Ar [[CC]YY]MMDDhhmm[.SS]
+.Op Fl d Ar human-datetime
.Ar file ...
.Sh DESCRIPTION
The
@@ -64,6 +65,9 @@ The
.Nm
utility does not treat this as an error.
No error messages are displayed and the exit value is not affected.
+.It Fl d
+The parse the argument using the human datetime parser
+.Xr parsedate 3 .
.It Fl f
This flag has no effect; it is accepted for compatibility reasons.
.It Fl h
Index: src/usr.bin/touch/touch.c
diff -u src/usr.bin/touch/touch.c:1.30 src/usr.bin/touch/touch.c:1.31
--- src/usr.bin/touch/touch.c:1.30 Tue Sep 6 14:33:18 2011
+++ src/usr.bin/touch/touch.c Tue Jul 24 21:23:46 2012
@@ -1,4 +1,4 @@
-/* $NetBSD: touch.c,v 1.30 2011/09/06 18:33:18 joerg Exp $ */
+/* $NetBSD: touch.c,v 1.31 2012/07/25 01:23:46 christos Exp $ */
/*
* Copyright (c) 1993
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1993\
#if 0
static char sccsid[] = "@(#)touch.c 8.2 (Berkeley) 4/28/95";
#endif
-__RCSID("$NetBSD: touch.c,v 1.30 2011/09/06 18:33:18 joerg Exp $");
+__RCSID("$NetBSD: touch.c,v 1.31 2012/07/25 01:23:46 christos Exp $");
#endif /* not lint */
#include <sys/types.h>
@@ -56,7 +56,9 @@ __RCSID("$NetBSD: touch.c,v 1.30 2011/09
#include <time.h>
#include <tzfile.h>
#include <unistd.h>
+#include <util.h>
+static void stime_arg0(char *, struct timeval *);
static void stime_arg1(char *, struct timeval *);
static void stime_arg2(char *, int, struct timeval *);
static void stime_file(char *, struct timeval *);
@@ -78,7 +80,7 @@ main(int argc, char *argv[])
if (gettimeofday(&tv[0], NULL))
err(1, "gettimeofday");
- while ((ch = getopt(argc, argv, "acfhmr:t:")) != -1)
+ while ((ch = getopt(argc, argv, "acd:fhmr:t:")) != -1)
switch(ch) {
case 'a':
aflag = 1;
@@ -86,6 +88,10 @@ main(int argc, char *argv[])
case 'c':
cflag = 1;
break;
+ case 'd':
+ timeset = 1;
+ stime_arg0(optarg, tv);
+ break;
case 'f':
break;
case 'h':
@@ -142,7 +148,7 @@ main(int argc, char *argv[])
if (*argv == NULL)
usage();
- for (rval = 0; *argv; ++argv) {
+ for (rval = EXIT_SUCCESS; *argv; ++argv) {
/* See if the file exists. */
if ((*get_file_status)(*argv, &sb)) {
if (!cflag) {
@@ -150,7 +156,7 @@ main(int argc, char *argv[])
fd = open(*argv,
O_WRONLY | O_CREAT, DEFFILEMODE);
if (fd == -1 || fstat(fd, &sb) || close(fd)) {
- rval = 1;
+ rval = EXIT_FAILURE;
warn("%s", *argv);
continue;
}
@@ -172,7 +178,7 @@ main(int argc, char *argv[])
/* If the user specified a time, nothing else we can do. */
if (timeset) {
- rval = 1;
+ rval = EXIT_FAILURE;
warn("%s", *argv);
}
@@ -185,7 +191,7 @@ main(int argc, char *argv[])
if (!(*change_file_times)(*argv, NULL))
continue;
- rval = 1;
+ rval = EXIT_FAILURE;
warn("%s", *argv);
}
exit(rval);
@@ -194,6 +200,15 @@ main(int argc, char *argv[])
#define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
static void
+stime_arg0(char *arg, struct timeval *tvp)
+{
+ tvp[1].tv_sec = tvp[0].tv_sec = parsedate(arg, NULL, NULL);
+ if (tvp[0].tv_sec == -1)
+ errx(EXIT_FAILURE, "Could not parse `%s'", arg);
+ tvp[0].tv_usec = tvp[1].tv_usec = 0;
+}
+
+static void
stime_arg1(char *arg, struct timeval *tvp)
{
struct tm *t;
@@ -203,7 +218,7 @@ stime_arg1(char *arg, struct timeval *tv
/* Start with the current time. */
tmptime = tvp[0].tv_sec;
if ((t = localtime(&tmptime)) == NULL)
- err(1, "localtime");
+ err(EXIT_FAILURE, "localtime");
/* [[CC]YY]MMDDhhmm[.SS] */
if ((p = strchr(arg, '.')) == NULL)
t->tm_sec = 0; /* Seconds defaults to 0. */
@@ -251,7 +266,7 @@ stime_arg1(char *arg, struct timeval *tv
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,
+terr: errx(EXIT_FAILURE,
"out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
tvp[0].tv_usec = tvp[1].tv_usec = 0;
@@ -265,7 +280,7 @@ stime_arg2(char *arg, int year, struct t
/* Start with the current time. */
tmptime = tvp[0].tv_sec;
if ((t = localtime(&tmptime)) == NULL)
- err(1, "localtime");
+ err(EXIT_FAILURE, "localtime");
t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */
--t->tm_mon; /* Convert from 01-12 to 00-11 */
@@ -284,7 +299,7 @@ stime_arg2(char *arg, int year, struct t
t->tm_isdst = -1; /* Figure out DST. */
tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
if (tvp[0].tv_sec == -1)
- errx(1,
+ errx(EXIT_FAILURE,
"out of range or illegal time specification: MMDDhhmm[yy]");
tvp[0].tv_usec = tvp[1].tv_usec = 0;
@@ -305,6 +320,7 @@ static void
usage(void)
{
(void)fprintf(stderr,
- "usage: touch [-acfhm] [-r file] [-t time] file ...\n");
- exit(1);
+ "Usage: %s [-acfhm] [-d datetime] [-r file] [-t time] file ...\n",
+ getprogname());
+ exit(EXIT_FAILURE);
}