]> mj.ucw.cz Git - nwho.git/blob - nwho.c
c0737f502c4741648e28ed12567252dea424061d
[nwho.git] / nwho.c
1 /*
2  *      The Remote User Information Lister 1.8
3  *
4  *      (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU General Public License. See file COPYING in any of the GNU packages.
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <dirent.h>
16 #include <netinet/in.h>
17
18 #include "net.h"
19
20 static int is_uptime;
21
22 static void
23 puttime(int s)
24 {
25   int d, h, m;
26   if (s < 100)
27     printf("%4ds", s);
28   else
29     {
30       m = s/60;
31       s %= 60;
32       h = m/60;
33       m %= 60;
34       if (h < 100)
35         printf("%02d.%02d", h, m);
36       else
37         {
38           d = h/24;
39           h %= 24;
40           if (d < 100)
41             printf("%2dd%02d", d, h);
42           else
43             printf("%4dd", d);
44         }
45     }
46 }
47
48 static void
49 show_uptime(char *name, struct rywho_pkt *p)
50 {
51   int i;
52
53   printf("%-16s up ", name);
54   puttime(ntohl(p->uptime));
55   printf("  load");
56   for(i=0; i<3; i++)
57     {
58       int l = ntohl(p->avl[i]);
59       printf(" %2d.%02d", l/100, l%100);
60     }
61   putchar('\n');
62 }
63
64 static void
65 show_users(char *name, struct rywho_pkt *p)
66 {
67   int u;
68   int m = ntohl(p->num_users);
69   struct userinfo *i;
70
71   for(u=0; u<m; u++)
72     {
73       i = &p->users[u];
74       printf("%-8s %-3s %c %-16s ", i->name, i->con, (i->mesg_y ? ' ' : '-'), name);
75       puttime(ntohl(i->login_time));
76       putchar(' ');
77       puttime(ntohl(i->idle_time));
78       putchar('\n');
79     }
80   if (m == MAX_USERS)
81     printf("%s: MAX_USERS reached!\n", name);
82 }
83
84 static void
85 scan(void)
86 {
87   DIR *d;
88   struct dirent *e;
89   struct rywho_pkt pkt;
90   int fd, r;
91   int is = 0;
92
93   if (chdir(YWHO_SPOOL_DIR) < 0)
94     {
95       fprintf(stderr, "chdir(" YWHO_SPOOL_DIR "): %m\n");
96       exit(1);
97     }
98   d = opendir(".");
99   if (!d)
100     {
101       perror("opendir");
102       exit(1);
103     }
104   while (e = readdir(d))
105     if (e->d_name[0] != '.')
106       {
107         fd = open(e->d_name, O_RDONLY);
108         if (fd < 0)
109           {
110             fprintf(stderr, "%s: %m\n", e->d_name);
111             continue;
112           }
113         r = read(fd, &pkt, sizeof(pkt));
114         close(fd);
115         if (r < sizeof(struct rywho_pkt) - MAX_USERS*sizeof(struct userinfo)
116             || r != sizeof(struct rywho_pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo))
117           {
118             fprintf(stderr, "%s: Malformed record\n", e->d_name);
119             continue;
120           }
121         (is_uptime ? show_uptime : show_users)(e->d_name, &pkt);
122         is = 1;
123       }
124   closedir(d);
125   if (!is)
126     puts("No data available.");
127 }
128
129 int
130 main(int argc, char **argv)
131 {
132   if (strstr(argv[0], "uptime"))
133     is_uptime = 1;
134   if (argc != 1)
135     {
136       fprintf(stderr, "Usage: %s\n", argv[0]);
137       return 1;
138     }
139   if (!is_uptime)
140     puts("Name     Li  M Where            LogT  IdleT");
141   scan();
142   return 0;
143 }