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