2 * The Remote User Information Lister
4 * (c) 1997--2010 Martin Mares <mj@ucw.cz>
14 #include <netinet/in.h>
27 struct userinfo *user;
28 struct out_host *host;
31 static struct out_host **host_array;
33 static int max_hosts = 16;
35 static struct out_user *user_array;
37 static int max_users = 16;
42 void *p = malloc(size);
48 static struct out_host *
49 new_host(struct nwho_pkt *orig_pkt, char *name)
51 int len = nwho_pkt_size(orig_pkt);
52 struct nwho_pkt *pkt = xmalloc(len);
53 memcpy(pkt, orig_pkt, len);
55 struct out_host *host = xmalloc(sizeof(*host));
56 snprintf(host->name, sizeof(host->name), "%s", name);
59 if (!host_array || num_hosts >= max_hosts)
62 host_array = realloc(host_array, max_hosts * sizeof(struct out_host));
64 host_array[num_hosts++] = host;
70 new_user(struct out_host *host, struct userinfo *ui)
72 if (!user_array || num_users >= max_users)
75 user_array = realloc(user_array, max_users * sizeof(struct out_user));
77 user_array[num_users++] = (struct out_user) {
96 printf("%02d.%02d", h, m);
102 printf("%2dd%02d", d, h);
112 for (int i=0; i<num_hosts; i++)
114 struct out_host *h = host_array[i];
115 struct nwho_pkt *p = h->pkt;
117 if (now - ntohl(p->server_time) >= DEFAULT_DOWN_TIME)
119 printf("%-16s down\n", h->name);
122 printf("%-16s up ", h->name);
123 puttime(ntohl(p->uptime));
125 for(int j=0; j<3; j++)
127 int l = ntohl(p->avl[j]);
128 printf(" %2d.%02d", l/100, l%100);
130 printf(" %3d users\n", (int) ntohl(p->num_users));
137 puts("Name Li M Where LogT IdleT");
139 for (int i=0; i<num_users; i++)
141 struct out_host *h = user_array[i].host;
142 struct userinfo *u = user_array[i].user;
144 if (now - ntohl(h->pkt->server_time) >= DEFAULT_DOWN_TIME)
146 printf("%-8.8s %-7s %c %-16s ", u->name, u->con, (u->mesg_y ? ' ' : '-'), h->name);
147 puttime(ntohl(u->login_time));
149 puttime(ntohl(u->idle_time));
153 for (int i=0; i<num_hosts; i++)
155 struct out_host *h = host_array[i];
156 if (ntohl(h->pkt->num_users) >= MAX_USERS)
157 printf("%s: MAX_USERS reached!\n", h->name);
169 if (chdir(NWHO_SPOOL_DIR) < 0)
171 fprintf(stderr, "chdir(" NWHO_SPOOL_DIR "): %m\n");
180 while (e = readdir(d))
181 if (e->d_name[0] != '.')
183 fd = open(e->d_name, O_RDONLY);
186 fprintf(stderr, "%s: %m\n", e->d_name);
189 r = read(fd, &pkt, sizeof(pkt));
191 if (r < sizeof(struct nwho_pkt) - MAX_USERS*sizeof(struct userinfo)
192 || pkt.magic != htonl(NWHO_MAGIC)
193 || r != sizeof(struct nwho_pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo))
195 fprintf(stderr, "%s: Malformed record\n", e->d_name);
198 struct out_host *host = new_host(&pkt, e->d_name);
199 for (int i=0; i < ntohl(pkt.num_users); i++)
200 new_user(host, &host->pkt->users[i]);
206 main(int argc, char **argv)
208 if (argc == 2 && !strcmp(argv[1], "--version"))
210 printf("nwho " STRINGIFY(VERSION) "\n");
214 if (strstr(argv[0], "uptime"))
218 fprintf(stderr, "Usage: %s\n", argv[0]);
226 puts("No data available.");