who.c revision 1.9 1 /* $NetBSD: who.c,v 1.9 2002/07/28 21:46:34 christos Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Michael Fischbein.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __COPYRIGHT(
42 "@(#) Copyright (c) 1989, 1993\n\
43 The Regents of the University of California. All rights reserved.\n");
44 #endif /* not lint */
45
46 #ifndef lint
47 #if 0
48 static char sccsid[] = "@(#)who.c 8.1 (Berkeley) 6/6/93";
49 #endif
50 __RCSID("$NetBSD: who.c,v 1.9 2002/07/28 21:46:34 christos Exp $");
51 #endif /* not lint */
52
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include <err.h>
56 #include <locale.h>
57 #include <pwd.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <time.h>
62 #include <unistd.h>
63 #ifdef SUPPORT_UTMP
64 #include <utmp.h>
65 #endif
66 #ifdef SUPPORT_UTMPX
67 #include <utmpx.h>
68 #endif
69
70 struct entry {
71 char name[65];
72 char line[65];
73 char host[257];
74 time_t time;
75 struct entry *next;
76 };
77
78 static void output_labels(void);
79 static void who_am_i(const char *, int);
80 static void usage(void);
81 static void process(const char *, int);
82 #ifdef SUPPORT_UTMP
83 static void getentry(struct entry *, struct utmp *);
84 #endif
85 #ifdef SUPPORT_UTMPX
86 static void getentryx(struct entry *, struct utmpx *);
87 #endif
88 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
89 static int setup(const char *);
90 static void adjust_size(struct entry *e);
91 #endif
92 static void print(const char *, const char *, time_t, const char *);
93
94 int main(int, char **);
95
96 static int show_term; /* show term state */
97 static int show_idle; /* show idle time */
98
99 static int maxname = 8, maxline = 8, maxhost = 16;
100
101 int
102 main(int argc, char **argv)
103 {
104 int c, only_current_term, show_labels;
105
106 setlocale(LC_ALL, "");
107
108 only_current_term = show_term = show_idle = show_labels = 0;
109 while ((c = getopt(argc, argv, "mTuH")) != -1) {
110 switch (c) {
111 case 'm':
112 only_current_term = 1;
113 break;
114 case 'T':
115 show_term = 1;
116 break;
117 case 'u':
118 show_idle = 1;
119 break;
120 case 'H':
121 show_labels = 1;
122 break;
123 default:
124 usage();
125 /* NOTREACHED */
126 }
127 }
128 argc -= optind;
129 argv += optind;
130
131 if (chdir("/dev")) {
132 err(1, "cannot change directory to /dev");
133 /* NOTREACHED */
134 }
135
136 switch (argc) {
137 case 0: /* who */
138 if (only_current_term) {
139 who_am_i(NULL, show_labels);
140 } else {
141 process(NULL, show_labels);
142 }
143 break;
144 case 1: /* who utmp_file */
145 if (only_current_term) {
146 who_am_i(*argv, show_labels);
147 } else {
148 process(*argv, show_labels);
149 }
150 break;
151 case 2: /* who am i */
152 who_am_i(NULL, show_labels);
153 break;
154 default:
155 usage();
156 /* NOTREACHED */
157 }
158 exit(0);
159 }
160
161 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
162 static void
163 adjust_size(struct entry *e)
164 {
165 int max;
166
167 if ((max = strlen(e->name)) > maxname)
168 maxname = max;
169 if ((max = strlen(e->line)) > maxline)
170 maxline = max;
171 if ((max = strlen(e->host)) > maxhost)
172 maxhost = max;
173 }
174
175 static int
176 setup(const char *fname)
177 {
178 int what = 3;
179
180 if (fname == NULL) {
181 #ifdef SUPPORT_UTMPX
182 setutent();
183 #endif
184 #ifdef SUPPORT_UTMP
185 setutxent();
186 #endif
187 } else {
188 size_t len = strlen(fname);
189 if (len == 0)
190 errx(1, "Filename cannot be 0 length.");
191 what = fname[len - 1] == 'x' ? 1 : 2;
192 if (what == 1) {
193 #ifdef SUPPORT_UTMPX
194 if (utmpxname(fname) == 0)
195 err(1, "Cannot open `%s'", fname);
196 #else
197 errx(1, "utmpx support not compiled in");
198 #endif
199 } else {
200 #ifdef SUPPORT_UTMPX
201 if (utmpname(fname) == 0)
202 err(1, "Cannot open `%s'", fname);
203 #else
204 errx(1, "utmp support not compiled in");
205 #endif
206 }
207 }
208 return what;
209 }
210 #endif
211
212 static void
213 who_am_i(const char *fname, int show_labels)
214 {
215 #ifdef SUPPORT_UTMPX
216 struct utmpx *utx;
217 #endif
218 #ifdef SUPPORT_UTMP
219 struct utmp *ut;
220 #endif
221 struct passwd *pw;
222 char *p;
223 char *t;
224 time_t now;
225 #if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX)
226 int what = setup(fname);
227 #endif
228
229 /* search through the utmp and find an entry for this tty */
230 if ((p = ttyname(STDIN_FILENO)) != NULL) {
231
232 /* strip any directory component */
233 if ((t = strrchr(p, '/')) != NULL)
234 p = t + 1;
235 #ifdef SUPPORT_UTMPX
236 while ((what & 1) && (utx = getutxent()) != NULL)
237 if (utx->ut_type == USER_PROCESS &&
238 !strcmp(utx->ut_line, p)) {
239 struct entry e;
240 getentryx(&e, utx);
241 if (show_labels)
242 output_labels();
243 print(e.name, e.line, e.time, e.host);
244 return;
245 }
246 #endif
247 #ifdef SUPPORT_UTMP
248 while ((what & 2) && (ut = getutent()) != NULL)
249 if (!strcmp(ut->ut_line, p)) {
250 struct entry e;
251 getentry(&e, ut);
252 if (show_labels)
253 output_labels();
254 print(e.name, e.line, e.time, e.host);
255 return;
256 }
257 #endif
258 } else
259 p = "tty??";
260
261 (void)time(&now);
262 pw = getpwuid(getuid());
263 if (show_labels)
264 output_labels();
265 print(pw ? pw->pw_name : "?", p, now, "");
266 }
267
268 static void
269 process(const char *fname, int show_labels)
270 {
271 #ifdef SUPPORT_UTMPX
272 struct utmpx *utx;
273 #endif
274 #ifdef SUPPORT_UTMP
275 struct utmp *ut;
276 #endif
277 struct entry *ep, *ehead = NULL;
278 #if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX)
279 int what = setup(fname);
280 struct entry **nextp = &ehead;
281 #endif
282
283 #ifdef SUPPORT_UTMPX
284 while ((what & 1) && (utx = getutxent()) != NULL) {
285 if (fname == NULL && utx->ut_type != USER_PROCESS)
286 continue;
287 if ((ep = calloc(1, sizeof(struct entry))) == NULL)
288 err(1, NULL);
289 getentryx(ep, utx);
290 *nextp = ep;
291 nextp = &(ep->next);
292 }
293 #endif
294
295 #ifdef SUPPORT_UTMP
296 while ((what & 2) && (ut = getutent()) != NULL) {
297 if (fname == NULL && (*ut->ut_name == '\0' ||
298 *ut->ut_line == '\0'))
299 continue;
300 /* Don't process entries that we have utmpx for */
301 for (ep = ehead; ep != NULL; ep = ep->next) {
302 if (strncmp(ep->line, ut->ut_line,
303 sizeof(ut->ut_line)) == 0)
304 break;
305 }
306 if (ep != NULL)
307 continue;
308 if ((ep = calloc(1, sizeof(struct entry))) == NULL)
309 err(1, NULL);
310 getentry(ep, ut);
311 *nextp = ep;
312 nextp = &(ep->next);
313 }
314 #endif
315 #if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX)
316 if (ehead != NULL) {
317 struct entry *from = ehead, *save;
318
319 ehead = NULL;
320 while (from != NULL) {
321 for (nextp = &ehead;
322 (*nextp) && strcmp(from->line, (*nextp)->line) > 0;
323 nextp = &(*nextp)->next)
324 continue;
325 save = from;
326 from = from->next;
327 save->next = *nextp;
328 *nextp = save;
329 }
330 }
331 #endif
332 if (show_labels)
333 output_labels();
334 for (ep = ehead; ep != NULL; ep = ep->next)
335 print(ep->name, ep->line, ep->time, ep->host);
336 }
337
338 #ifdef SUPPORT_UTMP
339 static void
340 getentry(struct entry *e, struct utmp *up)
341 {
342 (void)strncpy(e->name, up->ut_name, sizeof(up->ut_name));
343 e->name[sizeof(e->name) - 1] = '\0';
344 (void)strncpy(e->line, up->ut_line, sizeof(up->ut_line));
345 e->line[sizeof(e->line) - 1] = '\0';
346 (void)strncpy(e->host, up->ut_host, sizeof(up->ut_host));
347 e->name[sizeof(e->host) - 1] = '\0';
348 e->time = up->ut_time;
349 adjust_size(e);
350 }
351 #endif
352
353 #ifdef SUPPORT_UTMPX
354 static void
355 getentryx(struct entry *e, struct utmpx *up)
356 {
357 (void)strncpy(e->name, up->ut_name, sizeof(up->ut_name));
358 e->name[sizeof(e->name) - 1] = '\0';
359 (void)strncpy(e->line, up->ut_line, sizeof(up->ut_line));
360 e->line[sizeof(e->line) - 1] = '\0';
361 (void)strncpy(e->host, up->ut_host, sizeof(up->ut_host));
362 e->name[sizeof(e->host) - 1] = '\0';
363 e->time = (time_t)up->ut_tv.tv_sec;
364 adjust_size(e);
365 }
366 #endif
367
368
369 static void
370 print(const char *name, const char *line, time_t t, const char *host)
371 {
372 struct stat sb;
373 char state;
374 static time_t now = 0;
375 time_t idle;
376
377 state = '?';
378 idle = 0;
379
380 if (show_term || show_idle) {
381 if (now == 0)
382 time(&now);
383
384 if (stat(line, &sb) == 0) {
385 state = (sb.st_mode & 020) ? '+' : '-';
386 idle = now - sb.st_atime;
387 }
388
389 }
390
391 (void)printf("%-*.*s ", maxname, maxname, name);
392
393 if (show_term) {
394 (void)printf("%c ", state);
395 }
396
397 (void)printf("%-*.*s ", maxline, maxline, line);
398 (void)printf("%.12s ", ctime(&t) + 4);
399
400 if (show_idle) {
401 if (idle < 60)
402 (void)printf(" . ");
403 else if (idle < (24 * 60 * 60))
404 (void)printf("%02ld:%02ld ",
405 (long)(idle / (60 * 60)),
406 (long)(idle % (60 * 60)) / 60);
407 else
408 (void)printf(" old ");
409 }
410
411 if (*host)
412 printf("\t(%.*s)", maxhost, host);
413 (void)putchar('\n');
414 }
415
416 static void
417 output_labels()
418 {
419 (void)printf("%-*.*s ", maxname, maxname, "USER");
420
421 if (show_term)
422 (void)printf("S ");
423
424 (void)printf("%-*.*s ", maxline, maxline, "LINE");
425 (void)printf("WHEN ");
426
427 if (show_idle)
428 (void)printf("IDLE ");
429
430 (void)printf("\t%.*s", maxhost, "FROM");
431
432 (void)putchar('\n');
433 }
434
435 static void
436 usage()
437 {
438 (void)fprintf(stderr, "Usage: %s [-mTuH] [ file ]\n %s am i\n",
439 getprogname(), getprogname());
440 exit(1);
441 }
442