time: Use clock_gettime
[dragonfly.git] / usr.bin / rwho / rwho.c
blob95202249371cfddc41709268007e62a8096f1cba
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)rwho.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.bin/rwho/rwho.c,v 1.13.2.1 2002/03/12 19:49:09 phantom Exp $
34 #include <sys/param.h>
35 #include <sys/file.h>
37 #include <protocols/rwhod.h>
39 #include <dirent.h>
40 #include <err.h>
41 #include <langinfo.h>
42 #include <locale.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <time.h>
47 #include <unistd.h>
49 struct whod wd;
51 #define NUSERS 1000
53 struct myutmp {
54 char myhost[sizeof(wd.wd_hostname)];
55 int myidle;
56 struct outmp myutmp;
57 } myutmp[NUSERS];
59 #define WHDRSIZE (sizeof (wd) - sizeof (wd.wd_we))
61 * this macro should be shared with ruptime.
63 #define down(w,now) ((now) - (w)->wd_recvtime > 11 * 60)
65 static void usage(void);
66 static int utmpcmp(const void *, const void *);
68 int
69 main(int argc, char **argv)
71 struct dirent *dp;
72 struct whod *w = &wd;
73 struct whoent *we;
74 struct myutmp *mp;
75 int f, n, i, ch;
76 int width, d_first, aflg;
77 int nusers;
78 size_t cc;
79 time_t now;
80 DIR *dirp;
82 aflg = nusers = 0;
83 setlocale(LC_TIME, "");
84 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
86 while ((ch = getopt(argc, argv, "a")) != -1)
87 switch(ch) {
88 case 'a':
89 aflg = 1;
90 break;
91 case '?':
92 default:
93 usage();
95 argc -= optind;
96 argv += optind;
98 if (argc != 0)
99 usage();
101 if (chdir(_PATH_RWHODIR) || (dirp = opendir(".")) == NULL)
102 err(1, "%s", _PATH_RWHODIR);
103 mp = myutmp;
104 time(&now);
105 while ((dp = readdir(dirp))) {
106 if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5))
107 continue;
108 f = open(dp->d_name, O_RDONLY);
109 if (f < 0)
110 continue;
111 cc = read(f, (char *)&wd, sizeof (struct whod));
112 if (cc < WHDRSIZE) {
113 close(f);
114 continue;
116 if (down(w,now)) {
117 close(f);
118 continue;
120 cc -= WHDRSIZE;
121 we = w->wd_we;
122 for (n = cc / sizeof (struct whoent); n > 0; n--) {
123 if (aflg == 0 && we->we_idle >= 60*60) {
124 we++;
125 continue;
127 if (nusers >= NUSERS)
128 errx(1, "too many users");
129 mp->myutmp = we->we_utmp; mp->myidle = we->we_idle;
130 strcpy(mp->myhost, w->wd_hostname);
131 nusers++; we++; mp++;
133 close(f);
135 qsort((char *)myutmp, nusers, sizeof (struct myutmp), utmpcmp);
136 mp = myutmp;
137 width = 0;
138 for (i = 0; i < nusers; i++) {
139 /* append one for the blank and use 8 for the out_line */
140 int j = strlen(mp->myhost) + 1 + sizeof(mp->myutmp.out_line);
141 if (j > width)
142 width = j;
143 mp++;
145 mp = myutmp;
146 for (i = 0; i < nusers; i++) {
147 char buf[BUFSIZ], cbuf[80];
149 strftime(cbuf, sizeof(cbuf),
150 d_first ? "%e %b %R" : "%b %e %R",
151 localtime((time_t *)&mp->myutmp.out_time));
152 sprintf(buf, "%s:%-.*s", mp->myhost,
153 (int)sizeof(mp->myutmp.out_line), mp->myutmp.out_line);
154 printf("%-*.*s %-*s %s",
155 (int)sizeof(mp->myutmp.out_name),
156 (int)sizeof(mp->myutmp.out_name),
157 mp->myutmp.out_name,
158 width,
159 buf,
160 cbuf);
161 mp->myidle /= 60;
162 if (mp->myidle) {
163 if (aflg) {
164 if (mp->myidle >= 100*60)
165 mp->myidle = 100*60 - 1;
166 if (mp->myidle >= 60)
167 printf(" %2d", mp->myidle / 60);
168 else
169 printf(" ");
170 } else
171 printf(" ");
172 printf(":%02d", mp->myidle % 60);
174 printf("\n");
175 mp++;
177 exit(0);
181 static void
182 usage(void)
184 fprintf(stderr, "usage: rwho [-a]\n");
185 exit(1);
188 #define MYUTMP(a) ((const struct myutmp *)(a))
190 static int
191 utmpcmp(const void *u1, const void *u2)
193 int rc;
195 rc = strncmp(MYUTMP(u1)->myutmp.out_name, MYUTMP(u2)->myutmp.out_name,
196 sizeof(MYUTMP(u2)->myutmp.out_name));
197 if (rc)
198 return (rc);
199 rc = strcmp(MYUTMP(u1)->myhost, MYUTMP(u2)->myhost);
200 if (rc)
201 return (rc);
202 return (strncmp(MYUTMP(u1)->myutmp.out_line, MYUTMP(u2)->myutmp.out_line,
203 sizeof(MYUTMP(u2)->myutmp.out_line)));