Source-Changes-HG archive

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

[src/trunk]: src/bin/date revert the revert and fix the code properly.



details:   https://anonhg.NetBSD.org/src/rev/1710e22b389c
branches:  trunk
changeset: 761478:1710e22b389c
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Jan 29 02:16:52 2011 +0000

description:
revert the revert and fix the code properly.

diffstat:

 bin/date/date.c    |  45 ++++++++++++++++++++++++++++++-------------
 bin/date/netdate.c |  55 +++++++++++++++++++++++++++--------------------------
 2 files changed, 59 insertions(+), 41 deletions(-)

diffs (285 lines):

diff -r 6477ff157b7b -r 1710e22b389c bin/date/date.c
--- a/bin/date/date.c   Sat Jan 29 01:54:33 2011 +0000
+++ b/bin/date/date.c   Sat Jan 29 02:16:52 2011 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: date.c,v 1.58 2011/01/28 20:23:38 drochner Exp $ */
+/* $NetBSD: date.c,v 1.59 2011/01/29 02:16:52 christos Exp $ */
 
 /*
  * Copyright (c) 1985, 1987, 1988, 1993
@@ -40,7 +40,7 @@
 #if 0
 static char sccsid[] = "@(#)date.c     8.2 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: date.c,v 1.58 2011/01/28 20:23:38 drochner Exp $");
+__RCSID("$NetBSD: date.c,v 1.59 2011/01/29 02:16:52 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -50,6 +50,7 @@
 #include <ctype.h>
 #include <err.h>
 #include <fcntl.h>
+#include <errno.h>
 #include <locale.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -64,13 +65,12 @@
 
 static time_t tval;
 static int aflag, jflag, rflag, nflag;
-int retval;
 
 static void badformat(void);
 static void badtime(void);
 static void badvalue(const char *);
 static void setthetime(const char *);
-static void usage(void);
+static void usage(void) __attribute__((__noreturn__));
 
 int
 main(int argc, char *argv[])
@@ -79,6 +79,8 @@
        size_t bufsiz;
        const char *format;
        int ch;
+       long long val;
+       struct tm *tm;
 
        setprogname(argv[0]);
        (void)setlocale(LC_ALL, "");
@@ -92,8 +94,9 @@
                case 'd':
                        rflag = 1;
                        tval = parsedate(optarg, NULL, NULL);
-                       if (tval == -1)
-                               errx(1, "Cannot parse `%s'", optarg);
+                       if (tval == -1) 
+badarg:                                 errx(EXIT_FAILURE,
+                                   "Cannot parse `%s'", optarg);
                        break;
                case 'j':               /* don't set time */
                        jflag = 1;
@@ -102,8 +105,15 @@
                        nflag = 1;
                        break;
                case 'r':               /* user specified seconds */
+                       errno = 0;
+                       val = strtoll(optarg, &buf, 0);
+                       if (optarg[0] == '\0' || *buf != '\0')
+                               goto badarg;
+                       if (errno == ERANGE && (val == LLONG_MAX ||
+                           val == LLONG_MIN))
+                               err(EXIT_FAILURE, "Bad number `%s'", optarg);
                        rflag = 1;
-                       tval = strtoll(optarg, NULL, 0);
+                       tval = (time_t)val;
                        break;
                case 'u':               /* do everything in UTC */
                        (void)setenv("TZ", "UTC0", 1);
@@ -118,13 +128,13 @@
        if (!rflag && time(&tval) == -1)
                err(EXIT_FAILURE, "time");
 
-       format = "+%a %b %e %H:%M:%S %Z %Y";
 
        /* allow the operands in any order */
        if (*argv && **argv == '+') {
                format = *argv;
                ++argv;
-       }
+       } else
+               format = "+%a %b %e %H:%M:%S %Z %Y";
 
        if (*argv) {
                setthetime(*argv);
@@ -136,14 +146,19 @@
 
        if ((buf = malloc(bufsiz = 1024)) == NULL)
                goto bad;
-       while (strftime(buf, bufsiz, format, localtime(&tval)) == 0)
+
+       if ((tm = localtime(&tval)) == NULL)
+               err(EXIT_FAILURE, "localtime %lld failed", (long long)tval);
+
+       while (strftime(buf, bufsiz, format, tm) == 0)
                if ((buf = realloc(buf, bufsiz <<= 1)) == NULL)
                        goto bad;
-       (void)printf("%s\n", buf+1);
+
+       (void)printf("%s\n", buf + 1);
        free(buf);
        return 0;
 bad:
-       err(1, "Cannot allocate format buffer");
+       err(EXIT_FAILURE, "Cannot allocate format buffer");
 }
 
 static void
@@ -189,7 +204,8 @@
                badformat();
        }
 
-       lt = localtime(&tval);
+       if ((lt = localtime(&tval)) == NULL)
+               err(EXIT_FAILURE, "localtime %lld failed", (long long)tval);
 
        lt->tm_isdst = -1;                      /* Divine correct DST */
 
@@ -317,7 +333,8 @@
 usage(void)
 {
        (void)fprintf(stderr,
-           "usage: %s [-ajnu] [-d date] [-r seconds] [+format]", getprogname());
+           "Usage: %s [-ajnu] [-d date] [-r seconds] [+format]",
+           getprogname());
        (void)fprintf(stderr, " [[[[[[CC]yy]mm]dd]HH]MM[.SS]]\n");
        exit(EXIT_FAILURE);
        /* NOTREACHED */
diff -r 6477ff157b7b -r 1710e22b389c bin/date/netdate.c
--- a/bin/date/netdate.c        Sat Jan 29 01:54:33 2011 +0000
+++ b/bin/date/netdate.c        Sat Jan 29 02:16:52 2011 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: netdate.c,v 1.29 2011/01/28 20:23:38 drochner Exp $ */
+/* $NetBSD: netdate.c,v 1.30 2011/01/29 02:16:52 christos Exp $ */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)netdate.c  8.2 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: netdate.c,v 1.29 2011/01/28 20:23:38 drochner Exp $");
+__RCSID("$NetBSD: netdate.c,v 1.30 2011/01/29 02:16:52 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -59,8 +59,6 @@
 #define        WAITACK         2000    /* milliseconds */
 #define        WAITDATEACK     5000    /* milliseconds */
 
-extern int retval;
-
 static const char *
 tsp_type_to_string(const struct tsp *msg)
 {
@@ -75,7 +73,7 @@
  * new date to the local timedaemon.  If the timedaemon is in the master state,
  * it performs the correction on all slaves.  If it is in the slave state, it
  * notifies the master that a correction is needed.
- * Returns 0 on success.  Returns > 0 on failure, setting retval to 2;
+ * Returns 0 on success.  Returns > 0 on failure.
  */
 int
 netsettime(time_t tval)
@@ -89,7 +87,7 @@
 
        if ((sp = getservbyname("timed", "udp")) == NULL) {
                warnx("udp/timed: unknown service");
-               return (retval = 2);
+               return 2;
        }
 
        (void)memset(&dest, 0, sizeof(dest));
@@ -100,18 +98,18 @@
        dest.sin_port = sp->s_port;
        dest.sin_addr.s_addr = htonl(INADDR_ANY);
        s = socket(AF_INET, SOCK_DGRAM, 0);
-       if (s < 0) {
+       if (s == -1) {
                if (errno != EAFNOSUPPORT)
                        warn("timed");
-               return (retval = 2);
+               return 2;
        }
 
 #ifdef IP_PORTRANGE
        {
                static const int on = IP_PORTRANGE_LOW;
 
-               if (setsockopt(s, IPPROTO_IP, IP_PORTRANGE,
-                           &on, sizeof(on)) < 0) {
+               if (setsockopt(s, IPPROTO_IP, IP_PORTRANGE, &on,
+                   sizeof(on)) == -1) {
                        warn("setsockopt");
                        goto bad;
                }
@@ -120,20 +118,19 @@
 
        msg.tsp_type = TSP_SETDATE;
        msg.tsp_vers = TSPVERSION;
-       if (gethostname(hostname, sizeof(hostname))) {
+       if (gethostname(hostname, sizeof(hostname)) == -1) {
                warn("gethostname");
                goto bad;
        }
-       strncpy(msg.tsp_name, hostname, sizeof(msg.tsp_name));
-       msg.tsp_name[sizeof(msg.tsp_name) - 1] = '\0';
-       msg.tsp_seq = htons((uint16_t)0);
-       msg.tsp_time.tv_sec = htonl((uint32_t)tval); /* XXX: y2038 */
-       msg.tsp_time.tv_usec = htonl((uint32_t)0);
-       if (connect(s, (const struct sockaddr *)&dest, sizeof(dest)) < 0) {
+       (void)strlcpy(msg.tsp_name, hostname, sizeof(msg.tsp_name));
+       msg.tsp_seq = htons((in_port_t)0);
+       msg.tsp_time.tv_sec = htonl((in_addr_t)tval); /* XXX: y2038 */
+       msg.tsp_time.tv_usec = htonl((in_addr_t)0);
+       if (connect(s, (const void *)&dest, sizeof(dest)) == -1) {
                warn("connect");
                goto bad;
        }
-       if (send(s, &msg, sizeof(msg), 0) < 0) {
+       if (send(s, &msg, sizeof(msg), 0) == -1) {
                if (errno != ECONNREFUSED)
                        warn("send");
                goto bad;
@@ -147,14 +144,19 @@
        found = poll(&ready, 1, waittime);
 
        {
-               socklen_t length;
+               socklen_t len;
                int error;
 
-               length = sizeof(error);
-               if (!getsockopt(s, SOL_SOCKET, SO_ERROR, &error, &length)
-                   && error) {
-                       if (error != ECONNREFUSED)
+               len = sizeof(error);
+               if (getsockopt(s, SOL_SOCKET, SO_ERROR, &error, &len) == -1) {
+                       warn("getsockopt");
+                       goto bad;
+               }
+               if (error) {
+                       if (error != ECONNREFUSED) {
+                               errno = error;
                                warn("send (delayed error)");
+                       }
                        goto bad;
                }
        }
@@ -162,8 +164,7 @@
        if (found > 0 && ready.revents & POLLIN) {
                ssize_t ret;
 
-               ret = recv(s, &msg, sizeof(msg), 0);
-               if (ret < 0) {
+               if ((ret = recv(s, &msg, sizeof(msg), 0)) == -1) {
                        if (errno != ECONNREFUSED)
                                warn("recv");
                        goto bad;
@@ -182,7 +183,7 @@
                        goto loop;
                case TSP_DATEACK:
                        (void)close(s);
-                       return (0);
+                       return 0;
                default:
                        warnx("wrong ack received from timed: %s", 
                            tsp_type_to_string(&msg));
@@ -195,5 +196,5 @@
 
 bad:
        (void)close(s);
-       return (retval = 2);
+       return 2;
 }



Home | Main Index | Thread Index | Old Index