]> mj.ucw.cz Git - nwho.git/blobdiff - nwho.c
Killed another reference to <asm/types.h>
[nwho.git] / nwho.c
diff --git a/nwho.c b/nwho.c
index c0737f502c4741648e28ed12567252dea424061d..7edcd0cd521caaa07658f353a1cfe137ad550ecd 100644 (file)
--- a/nwho.c
+++ b/nwho.c
@@ -1,10 +1,7 @@
 /*
- *     The Remote User Information Lister 1.8
+ *     The Remote User Information Lister
  *
- *     (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
- *
- *     This software may be freely distributed and used according to the terms
- *     of the GNU General Public License. See file COPYING in any of the GNU packages.
+ *     (c) 1997--2010 Martin Mares <mj@ucw.cz>
  */
 
 #include <stdio.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <dirent.h>
+#include <time.h>
 #include <netinet/in.h>
 
 #include "net.h"
 
 static int is_uptime;
+static time_t now;
 
 static void
 puttime(int s)
@@ -46,10 +45,15 @@ puttime(int s)
 }
 
 static void
-show_uptime(char *name, struct rywho_pkt *p)
+show_uptime(char *name, struct nwho_pkt *p)
 {
   int i;
 
+  if (now - ntohl(p->server_time) >= DEFAULT_DOWN_TIME)
+    {
+      printf("%-16s down\n", name);
+      return;
+    }
   printf("%-16s up ", name);
   puttime(ntohl(p->uptime));
   printf("  load");
@@ -58,20 +62,22 @@ show_uptime(char *name, struct rywho_pkt *p)
       int l = ntohl(p->avl[i]);
       printf(" %2d.%02d", l/100, l%100);
     }
-  putchar('\n');
+  printf(" %3d users\n", (int) ntohl(p->num_users));
 }
 
 static void
-show_users(char *name, struct rywho_pkt *p)
+show_users(char *name, struct nwho_pkt *p)
 {
   int u;
   int m = ntohl(p->num_users);
   struct userinfo *i;
 
+  if (now - ntohl(p->server_time) >= DEFAULT_DOWN_TIME)
+    return;
   for(u=0; u<m; u++)
     {
       i = &p->users[u];
-      printf("%-8s %-3s %c %-16s ", i->name, i->con, (i->mesg_y ? ' ' : '-'), name);
+      printf("%-8.8s %-7s %c %-16s ", i->name, i->con, (i->mesg_y ? ' ' : '-'), name);
       puttime(ntohl(i->login_time));
       putchar(' ');
       puttime(ntohl(i->idle_time));
@@ -86,13 +92,13 @@ scan(void)
 {
   DIR *d;
   struct dirent *e;
-  struct rywho_pkt pkt;
+  struct nwho_pkt pkt;
   int fd, r;
   int is = 0;
 
-  if (chdir(YWHO_SPOOL_DIR) < 0)
+  if (chdir(NWHO_SPOOL_DIR) < 0)
     {
-      fprintf(stderr, "chdir(" YWHO_SPOOL_DIR "): %m\n");
+      fprintf(stderr, "chdir(" NWHO_SPOOL_DIR "): %m\n");
       exit(1);
     }
   d = opendir(".");
@@ -112,8 +118,9 @@ scan(void)
          }
        r = read(fd, &pkt, sizeof(pkt));
        close(fd);
-       if (r < sizeof(struct rywho_pkt) - MAX_USERS*sizeof(struct userinfo)
-           || r != sizeof(struct rywho_pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo))
+       if (r < sizeof(struct nwho_pkt) - MAX_USERS*sizeof(struct userinfo)
+           || pkt.magic != htonl(NWHO_MAGIC)
+           || r != sizeof(struct nwho_pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo))
          {
            fprintf(stderr, "%s: Malformed record\n", e->d_name);
            continue;
@@ -137,7 +144,8 @@ main(int argc, char **argv)
       return 1;
     }
   if (!is_uptime)
-    puts("Name     Li  M Where            LogT  IdleT");
+    puts("Name     Li      M Where            LogT  IdleT");
+  now = time(NULL);
   scan();
   return 0;
 }