Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/touch add an option to parse human dates.



details:   https://anonhg.NetBSD.org/src/rev/bda6278e5abe
branches:  trunk
changeset: 780450:bda6278e5abe
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Jul 25 01:23:46 2012 +0000

description:
add an option to parse human dates.

diffstat:

 usr.bin/touch/Makefile |   4 +++-
 usr.bin/touch/touch.1  |   8 ++++++--
 usr.bin/touch/touch.c  |  42 +++++++++++++++++++++++++++++-------------
 3 files changed, 38 insertions(+), 16 deletions(-)

diffs (195 lines):

diff -r 322e9f9e85de -r bda6278e5abe usr.bin/touch/Makefile
--- a/usr.bin/touch/Makefile    Wed Jul 25 01:07:49 2012 +0000
+++ b/usr.bin/touch/Makefile    Wed Jul 25 01:23:46 2012 +0000
@@ -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>
diff -r 322e9f9e85de -r bda6278e5abe usr.bin/touch/touch.1
--- a/usr.bin/touch/touch.1     Wed Jul 25 01:07:49 2012 +0000
+++ b/usr.bin/touch/touch.1     Wed Jul 25 01:23:46 2012 +0000
@@ -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 @@
 .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
diff -r 322e9f9e85de -r bda6278e5abe usr.bin/touch/touch.c
--- a/usr.bin/touch/touch.c     Wed Jul 25 01:07:49 2012 +0000
+++ b/usr.bin/touch/touch.c     Wed Jul 25 01:23:46 2012 +0000
@@ -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 @@
 #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 @@
 #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 @@
        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 @@
                case 'c':
                        cflag = 1;
                        break;
+               case 'd':
+                       timeset = 1;
+                       stime_arg0(optarg, tv);
+                       break;
                case 'f':
                        break;
                case 'h':
@@ -142,7 +148,7 @@
        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 @@
                                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 @@
 
                /* 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 @@
                 if (!(*change_file_times)(*argv, NULL))
                        continue;
 
-               rval = 1;
+               rval = EXIT_FAILURE;
                warn("%s", *argv);
        }
        exit(rval);
@@ -194,6 +200,15 @@
 #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 @@
                                        /* 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 @@
        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 @@
                                        /* 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 @@
        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 @@
 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);
 }



Home | Main Index | Thread Index | Old Index