Annotation of src/usr.bin/who/who.c, Revision 1.14
1.14 ! deraadt 1: /* $OpenBSD: who.c,v 1.13 2003/04/07 21:14:28 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: who.c,v 1.4 1994/12/07 04:28:49 jtc Exp $ */
3:
4: /*
5: * Copyright (c) 1989, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Michael Fischbein.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
19: * 3. All advertising materials mentioning features or use of this software
20: * must display the following acknowledgement:
21: * This product includes software developed by the University of
22: * California, Berkeley and its contributors.
23: * 4. Neither the name of the University nor the names of its contributors
24: * may be used to endorse or promote products derived from this software
25: * without specific prior written permission.
26: *
27: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37: * SUCH DAMAGE.
38: */
39:
40: #ifndef lint
41: static char 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
1.14 ! deraadt 50: static char rcsid[] = "$OpenBSD: who.c,v 1.13 2003/04/07 21:14:28 deraadt Exp $";
1.1 deraadt 51: #endif /* not lint */
52:
53: #include <sys/types.h>
54: #include <sys/stat.h>
1.9 millert 55: #include <paths.h>
1.1 deraadt 56: #include <pwd.h>
57: #include <utmp.h>
58: #include <stdio.h>
59: #include <string.h>
60: #include <stdlib.h>
61: #include <unistd.h>
62: #include <time.h>
63: #include <err.h>
1.5 flipk 64: #include <locale.h>
65:
1.12 millert 66: void output(struct utmp *);
67: void output_labels(void);
68: void who_am_i(FILE *);
69: void usage(void);
70: FILE *file(char *);
1.1 deraadt 71:
72: int only_current_term; /* show info about the current terminal only */
73: int show_term; /* show term state */
74: int show_idle; /* show idle time */
1.5 flipk 75: int show_labels; /* show column labels */
1.6 denny 76: int show_quick; /* quick, names only */
1.1 deraadt 77:
1.10 deraadt 78: #define NAME_WIDTH 8
79: #define HOST_WIDTH 32
80:
1.1 deraadt 81: int
82: main(argc, argv)
83: int argc;
84: char **argv;
85: {
86: struct utmp usr;
1.5 flipk 87: FILE *ufp;
1.1 deraadt 88: int c;
89:
1.5 flipk 90: setlocale(LC_ALL, "");
91:
92: only_current_term = show_term = show_idle = show_labels = 0;
1.6 denny 93: show_quick = 0;
94: while ((c = getopt(argc, argv, "HmqTu")) != -1) {
1.1 deraadt 95: switch (c) {
1.6 denny 96: case 'H':
97: show_labels = 1;
98: break;
1.1 deraadt 99: case 'm':
100: only_current_term = 1;
101: break;
1.6 denny 102: case 'q':
103: show_quick = 1;
104: break;
1.1 deraadt 105: case 'T':
106: show_term = 1;
107: break;
108: case 'u':
109: show_idle = 1;
110: break;
111: default:
112: usage();
113: /* NOTREACHED */
114: }
115: }
116: argc -= optind;
117: argv += optind;
118:
1.6 denny 119: if (show_quick) {
120: only_current_term = show_term = show_idle = show_labels = 0;
121: }
122:
1.5 flipk 123: if (show_labels)
124: output_labels();
125:
1.1 deraadt 126: switch (argc) {
127: case 0: /* who */
128: ufp = file(_PATH_UTMP);
129:
130: if (only_current_term) {
131: who_am_i(ufp);
1.6 denny 132: } else if (show_quick) {
133: int count = 0;
134:
135: while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) {
136: if (*usr.ut_name && *usr.ut_line) {
1.10 deraadt 137: (void)printf("%-*.*s ", NAME_WIDTH,
1.6 denny 138: UT_NAMESIZE, usr.ut_name);
139: if ((++count % 8) == 0)
140: (void) printf("\n");
141: }
142: }
143: if (count % 8)
144: (void) printf("\n");
145: (void) printf ("# users=%d\n", count);
1.1 deraadt 146: } else {
147: /* only entries with both name and line fields */
148: while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
149: if (*usr.ut_name && *usr.ut_line)
150: output(&usr);
151: }
152: break;
153: case 1: /* who utmp_file */
154: ufp = file(*argv);
155:
156: if (only_current_term) {
157: who_am_i(ufp);
1.6 denny 158: } else if (show_quick) {
159: int count = 0;
160:
161: while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1) {
162: if (*usr.ut_name && *usr.ut_line) {
1.10 deraadt 163: (void)printf("%-*.*s ", NAME_WIDTH,
1.6 denny 164: UT_NAMESIZE, usr.ut_name);
165: if ((++count % 8) == 0)
166: (void) printf("\n");
167: }
168: }
169: if (count % 8)
170: (void) printf("\n");
171: (void) printf ("# users=%d\n", count);
1.1 deraadt 172: } else {
173: /* all entries */
174: while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
175: output(&usr);
176: }
177: break;
178: case 2: /* who am i */
179: ufp = file(_PATH_UTMP);
180: who_am_i(ufp);
181: break;
182: default:
183: usage();
184: /* NOTREACHED */
185: }
186: exit(0);
187: }
188:
189: void
190: who_am_i(ufp)
191: FILE *ufp;
192: {
193: struct utmp usr;
194: struct passwd *pw;
1.11 mpech 195: char *p;
1.1 deraadt 196: char *t;
197:
198: /* search through the utmp and find an entry for this tty */
1.5 flipk 199: if ((p = ttyname(0))) {
1.1 deraadt 200: /* strip any directory component */
1.5 flipk 201: if ((t = strrchr(p, '/')))
1.1 deraadt 202: p = t + 1;
203: while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
1.4 deraadt 204: if (*usr.ut_name && !strcmp(usr.ut_line, p)) {
1.1 deraadt 205: output(&usr);
206: return;
207: }
208: /* well, at least we know what the tty is */
209: (void)strncpy(usr.ut_line, p, UT_LINESIZE);
210: } else
1.13 deraadt 211: (void)strncpy(usr.ut_line, "tty??", UT_LINESIZE);
1.1 deraadt 212:
213: pw = getpwuid(getuid());
214: (void)strncpy(usr.ut_name, pw ? pw->pw_name : "?", UT_NAMESIZE);
215: (void)time(&usr.ut_time);
216: *usr.ut_host = '\0';
217: output(&usr);
218: }
219:
220: void
221: output(up)
222: struct utmp *up;
223: {
224: struct stat sb;
1.9 millert 225: char line[sizeof(_PATH_DEV) + sizeof (up->ut_line)];
1.5 flipk 226: char state = '?';
1.1 deraadt 227: static time_t now = 0;
1.5 flipk 228: time_t idle = 0;
1.1 deraadt 229:
230: if (show_term || show_idle) {
231: if (now == 0)
232: time(&now);
233:
1.14 ! deraadt 234: memset(line, 0, sizeof line);
! 235: strlcpy(line, _PATH_DEV, sizeof line);
! 236: strlcat(line, up->ut_line, sizeof line);
1.1 deraadt 237:
238: if (stat(line, &sb) == 0) {
239: state = (sb.st_mode & 020) ? '+' : '-';
240: idle = now - sb.st_atime;
241: } else {
242: state = '?';
243: idle = 0;
244: }
245:
246: }
247:
1.10 deraadt 248: (void)printf("%-*.*s ", NAME_WIDTH, UT_NAMESIZE, up->ut_name);
1.1 deraadt 249:
250: if (show_term) {
251: (void)printf("%c ", state);
252: }
253:
254: (void)printf("%-*.*s ", UT_LINESIZE, UT_LINESIZE, up->ut_line);
255: (void)printf("%.12s ", ctime(&up->ut_time) + 4);
256:
257: if (show_idle) {
258: if (idle < 60)
259: (void)printf(" . ");
260: else if (idle < (24 * 60 * 60))
261: (void)printf("%02d:%02d ",
262: (idle / (60 * 60)),
263: (idle % (60 * 60)) / 60);
264: else
265: (void)printf(" old ");
266: }
267:
268: if (*up->ut_host)
1.10 deraadt 269: printf(" (%.*s)", HOST_WIDTH, up->ut_host);
1.1 deraadt 270: (void)putchar('\n');
271: }
272:
1.5 flipk 273: void
274: output_labels()
275: {
1.10 deraadt 276: (void)printf("%-*.*s ", NAME_WIDTH, UT_NAMESIZE, "USER");
1.5 flipk 277:
278: if (show_term)
279: (void)printf("S ");
280:
281: (void)printf("%-*.*s ", UT_LINESIZE, UT_LINESIZE, "LINE");
282: (void)printf("WHEN ");
283:
284: if (show_idle)
285: (void)printf("IDLE ");
286:
1.10 deraadt 287: (void)printf(" %.*s", HOST_WIDTH, "FROM");
1.5 flipk 288:
289: (void)putchar('\n');
290: }
291:
1.1 deraadt 292: FILE *
293: file(name)
294: char *name;
295: {
296: FILE *ufp;
297:
298: if (!(ufp = fopen(name, "r"))) {
299: err(1, "%s", name);
300: /* NOTREACHED */
301: }
302: return(ufp);
303: }
304:
305: void
306: usage()
307: {
1.6 denny 308: (void)fprintf(stderr, "usage: who [-mqTuH] [ file ]\n who am i\n");
1.1 deraadt 309: exit(1);
310: }