Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/last Add -n flag to print numeric entries.



details:   https://anonhg.NetBSD.org/src/rev/b911964d6d6b
branches:  trunk
changeset: 571098:b911964d6d6b
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Nov 11 00:54:23 2004 +0000

description:
Add -n flag to print numeric entries.

diffstat:

 usr.bin/last/last.1 |   8 ++++++--
 usr.bin/last/last.c |  24 +++++++++++++++---------
 usr.bin/last/want.c |  47 ++++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 63 insertions(+), 16 deletions(-)

diffs (212 lines):

diff -r 31785371d540 -r b911964d6d6b usr.bin/last/last.1
--- a/usr.bin/last/last.1       Thu Nov 11 00:40:13 2004 +0000
+++ b/usr.bin/last/last.1       Thu Nov 11 00:54:23 2004 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: last.1,v 1.12 2003/08/07 11:14:17 agc Exp $
+.\"    $NetBSD: last.1,v 1.13 2004/11/11 00:54:23 christos Exp $
 .\"
 .\" Copyright (c) 1980, 1990, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"     @(#)last.1     8.1 (Berkeley) 6/6/93
 .\"
-.Dd February 17, 2003
+.Dd November 10, 2004
 .Dt LAST 1
 .Os
 .Sh NAME
@@ -44,6 +44,7 @@
 .Op Fl h Ar host
 .Op Fl L Ar linesize
 .Op Fl N Ar namesize
+.Op Fl n
 .Op Fl t Ar tty
 .Op user ...
 .Sh DESCRIPTION
@@ -92,6 +93,9 @@
 Use the provided linesize as the width to format the tty field.
 .It Fl N Ar namesize
 Use the provided namesize as the width to format the login name field.
+.It Fl n
+Print host addresses numerically. This option works only on wtmpx entries,
+and prints nothing on wtmp entries.
 .It Fl T
 Display better time information, including the year and seconds.
 .It Fl t Ar tty
diff -r 31785371d540 -r b911964d6d6b usr.bin/last/last.c
--- a/usr.bin/last/last.c       Thu Nov 11 00:40:13 2004 +0000
+++ b/usr.bin/last/last.c       Thu Nov 11 00:54:23 2004 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: last.c,v 1.22 2004/01/05 23:23:35 jmmv Exp $   */
+/*     $NetBSD: last.c,v 1.23 2004/11/11 00:54:23 christos Exp $       */
 
 /*
  * Copyright (c) 1987, 1993, 1994
@@ -40,7 +40,7 @@
 #if 0
 static char sccsid[] = "@(#)last.c     8.2 (Berkeley) 4/2/94";
 #endif
-__RCSID("$NetBSD: last.c,v 1.22 2004/01/05 23:23:35 jmmv Exp $");
+__RCSID("$NetBSD: last.c,v 1.23 2004/11/11 00:54:23 christos Exp $");
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -56,6 +56,7 @@
 #include <time.h>
 #include <tzfile.h>
 #include <unistd.h>
+#include <arpa/inet.h>
 #ifdef SUPPORT_UTMPX
 #include <utmpx.h>
 #endif
@@ -118,10 +119,10 @@
 static void     hostconv(char *);
 static char    *ttyconv(char *);
 #ifdef SUPPORT_UTMPX
-static void     wtmpx(const char *, int, int, int);
+static void     wtmpx(const char *, int, int, int, int);
 #endif
 #ifdef SUPPORT_UTMP
-static void     wtmp(const char *, int, int, int);
+static void     wtmp(const char *, int, int, int, int);
 #endif
 static char    *fmttime(time_t, int);
 static void     usage(void);
@@ -150,10 +151,11 @@
        int namesize = UT_NAMESIZE;
        int linesize = UT_LINESIZE;
        int hostsize = UT_HOSTSIZE;
+       int numeric = 0;
 
        maxrec = -1;
 
-       while ((ch = getopt(argc, argv, "0123456789f:H:h:L:N:Tt:")) != -1)
+       while ((ch = getopt(argc, argv, "0123456789f:H:h:L:nN:Tt:")) != -1)
                switch (ch) {
                case '0': case '1': case '2': case '3': case '4':
                case '5': case '6': case '7': case '8': case '9':
@@ -187,6 +189,9 @@
                case 'N':
                        namesize = atoi(optarg);
                        break;
+               case 'n':
+                       numeric = 1;
+                       break;
                case 'T':
                        fulltime = 1;
                        break;
@@ -233,13 +238,13 @@
        }
 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
        if (file[strlen(file) - 1] == 'x')
-               wtmpx(file, namesize, linesize, hostsize);
+               wtmpx(file, namesize, linesize, hostsize, numeric);
        else
-               wtmp(file, namesize, linesize, hostsize);
+               wtmp(file, namesize, linesize, hostsize, numeric);
 #elif defined(SUPPORT_UTMPX)
-       wtmpx(file, namesize, linesize, hostsize);
+       wtmpx(file, namesize, linesize, hostsize, numeric);
 #elif defined(SUPPORT_UTMP)
-       wtmp(file, namesize, linesize, hostsize);
+       wtmp(file, namesize, linesize, hostsize, numeric);
 #else
        errx(1, "No utmp or utmpx support compiled in.");
 #endif
@@ -373,6 +378,7 @@
 #define utmp utmpx
 #define want wantx
 #define wtmp wtmpx
+#define gethost gethostx
 #define buf bufx
 #define onintr onintrx
 #define TYPE(a) (a)->ut_type
diff -r 31785371d540 -r b911964d6d6b usr.bin/last/want.c
--- a/usr.bin/last/want.c       Thu Nov 11 00:40:13 2004 +0000
+++ b/usr.bin/last/want.c       Thu Nov 11 00:54:23 2004 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: want.c,v 1.2 2003/08/07 11:14:18 agc Exp $     */
+/*     $NetBSD: want.c,v 1.3 2004/11/11 00:54:23 christos Exp $        */
 
 /*
  * Copyright (c) 1987, 1993, 1994
@@ -32,13 +32,48 @@
 
 static void onintr(int);
 static int want(struct utmp *, int);
+static const char *gethost(struct utmp *, int);
+
+static const char *
+gethost(struct utmp* ut, int numeric)
+{
+#if FIRSTVALID == 0
+       return numeric ? "" : ut->ut_host;
+#else
+       if (numeric) {
+               static char buf[512];
+               const char *p;
+               struct sockaddr_storage *ss = &ut->ut_ss;
+               void *a;
+               switch (ss->ss_family) {
+               default:
+                       (void)snprintf(buf, sizeof(buf), "%d: unknown family",
+                               ss->ss_family);
+                       return buf;
+               case 0: /* reboot etc. entries */
+                       return "";
+               case AF_INET:
+                       a = &((struct sockaddr_in *)(void *)ss)->sin_addr;
+                       break;
+               case AF_INET6:
+                       a = &((struct sockaddr_in6 *)(void *)ss)->sin6_addr;
+                       break;
+               }
+               if ((p = inet_ntop(ss->ss_family, a, buf, ss->ss_len)) != NULL)
+                       return p;
+               (void)snprintf(buf, sizeof(buf), "%s", strerror(errno));
+               return buf;
+       } else
+               return ut->ut_host;
+#endif
+}
 
 /*
  * wtmp --
  *     read through the wtmp file
  */
 void
-wtmp(const char *file, int namesz, int linesz, int hostsz)
+wtmp(const char *file, int namesz, int linesz, int hostsz, int numeric)
 {
        struct utmp     *bp;            /* current structure */
        TTY     *T;                     /* tty list entry */
@@ -83,7 +118,8 @@
                                        printf("%-*.*s  %-*.*s %-*.*s %s\n",
                                            namesz, namesz, bp->ut_name,
                                            linesz, linesz, bp->ut_line,
-                                           hostsz, hostsz, bp->ut_host, ct);
+                                           hostsz, hostsz,
+                                           gethost(bp, numeric), ct);
                                        if (maxrec != -1 && !--maxrec)
                                                return;
                                }
@@ -103,7 +139,7 @@
                                    linesz, linesz,
                                    bp->ut_line,
                                    hostsz, hostsz,
-                                   bp->ut_host,
+                                   gethost(bp, numeric),
                                    ct);
                                        if (maxrec && !--maxrec)
                                                return;
@@ -127,7 +163,8 @@
                                printf("%-*.*s  %-*.*s %-*.*s %s ",
                                    namesz, namesz, bp->ut_name,
                                    linesz, linesz, bp->ut_line,
-                                   hostsz, hostsz, bp->ut_host,
+                                   hostsz, hostsz,
+                                   gethost(bp, numeric),
                                    ct);
                                if (!T->logout)
                                        puts("  still logged in");



Home | Main Index | Thread Index | Old Index