Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/w Be a bit more careful with errors; if you cannot f...



details:   https://anonhg.NetBSD.org/src/rev/78b59f09f520
branches:  trunk
changeset: 571347:78b59f09f520
user:      christos <christos%NetBSD.org@localhost>
date:      Fri Nov 19 13:17:06 2004 +0000

description:
Be a bit more careful with errors; if you cannot find the pty to stat for
time, then report ? instead of the previous random contents of struct stat.

diffstat:

 usr.bin/w/pr_time.c |  13 ++++++++++---
 usr.bin/w/w.c       |  34 +++++++++++++++++-----------------
 2 files changed, 27 insertions(+), 20 deletions(-)

diffs (137 lines):

diff -r 317bc52921c4 -r 78b59f09f520 usr.bin/w/pr_time.c
--- a/usr.bin/w/pr_time.c       Fri Nov 19 10:17:17 2004 +0000
+++ b/usr.bin/w/pr_time.c       Fri Nov 19 13:17:06 2004 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pr_time.c,v 1.14 2003/08/07 11:17:12 agc Exp $ */
+/*     $NetBSD: pr_time.c,v 1.15 2004/11/19 13:17:06 christos Exp $    */
 
 /*-
  * Copyright (c) 1990, 1993, 1994
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)pr_time.c  8.2 (Berkeley) 4/4/94";
 #else
-__RCSID("$NetBSD: pr_time.c,v 1.14 2003/08/07 11:17:12 agc Exp $");
+__RCSID("$NetBSD: pr_time.c,v 1.15 2004/11/19 13:17:06 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -92,7 +92,14 @@
 void
 pr_idle(time_t idle)
 {
-       int days = idle / SECSPERDAY;
+       int days;
+
+       if (idle == (time_t)-1) {
+               (void)printf("     ? ");
+               return;
+       }
+
+       days = idle / SECSPERDAY;
 
        /* If idle more than 36 hours, print as a number of days. */
        if (idle >= 36 * SECSPERHOUR)
diff -r 317bc52921c4 -r 78b59f09f520 usr.bin/w/w.c
--- a/usr.bin/w/w.c     Fri Nov 19 10:17:17 2004 +0000
+++ b/usr.bin/w/w.c     Fri Nov 19 13:17:06 2004 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: w.c,v 1.63 2004/11/11 00:09:07 christos Exp $  */
+/*     $NetBSD: w.c,v 1.64 2004/11/19 13:17:06 christos Exp $  */
 
 /*-
  * Copyright (c) 1980, 1991, 1993, 1994
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)w.c        8.6 (Berkeley) 6/30/94";
 #else
-__RCSID("$NetBSD: w.c,v 1.63 2004/11/11 00:09:07 christos Exp $");
+__RCSID("$NetBSD: w.c,v 1.64 2004/11/19 13:17:06 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -118,13 +118,14 @@
        pid_t   pid;                    /* pid or ~0 if not known */
 } *ep, *ehead = NULL, **nextp = &ehead;
 
-static void     pr_args(struct kinfo_proc2 *);
-static void     pr_header(time_t *, int);
+static void    pr_args(struct kinfo_proc2 *);
+static void    pr_header(time_t *, int);
 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
-static struct stat *ttystat(char *);
+static int     ttystat(const char *, struct stat *);
 static void    process(struct entry *);
 #endif
-static void     usage(int);
+static void    usage(int);
+
 int    main(int, char **);
 
 int
@@ -524,22 +525,19 @@
 }
 
 #if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
-static struct stat *
-ttystat(char *line)
+static int
+ttystat(const char *line, struct stat *st)
 {
-       static struct stat sb;
        char ttybuf[MAXPATHLEN];
 
        (void)snprintf(ttybuf, sizeof(ttybuf), "%s%s", _PATH_DEV, line);
-       if (stat(ttybuf, &sb))
-               return (NULL);
-       return (&sb);
+       return stat(ttybuf, st);
 }
 
 static void
 process(struct entry *ep)
 {
-       struct stat *stp;
+       struct stat st;
        time_t touched;
        int max;
 
@@ -550,13 +548,15 @@
        if ((max = strlen(ep->host)) > maxhost)
                maxhost = max;
 
+       ep->tdev = 0;
+       ep->idle = (time_t)-1;
 
 #ifdef SUPPORT_UTMP
        /*
         * Hack to recognize and correctly parse
         * ut entry made by ftpd. The "tty" used
         * by ftpd is not a real tty, just identifier in
-        * form ftpSUPPORT_ID. Pid parsed from the "tty name"
+        * form ftpPID. Pid parsed from the "tty name"
         * is used later to match corresponding process.
         * NB: This is only used for utmp entries. For utmpx,
         * we already have the pid.
@@ -566,10 +566,10 @@
                return;
        }
 #endif
-       if ((stp = ttystat(ep->line)) == NULL)
+       if (ttystat(ep->line, &st) == -1)
                return;
 
-       ep->tdev = stp->st_rdev;
+       ep->tdev = st.st_rdev;
        /*
         * If this is the console device, attempt to ascertain
         * the true console device dev_t.
@@ -584,7 +584,7 @@
                (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
        }
 
-       touched = stp->st_atime;
+       touched = st.st_atime;
        if (touched < ep->tv.tv_sec) {
                /* tty untouched since before login */
                touched = ep->tv.tv_sec;



Home | Main Index | Thread Index | Old Index