Annotation of src/usr.bin/who/who.c, Revision 1.3
1.3 ! millert 1: /* $OpenBSD: who.c,v 1.2 1996/06/26 05:43:00 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.3 ! millert 50: static char rcsid[] = "$OpenBSD: who.c,v 1.2 1996/06/26 05:43:00 deraadt Exp $";
1.1 deraadt 51: #endif /* not lint */
52:
53: #include <sys/types.h>
54: #include <sys/stat.h>
55: #include <pwd.h>
56: #include <utmp.h>
57: #include <stdio.h>
58: #include <string.h>
59: #include <stdlib.h>
60: #include <unistd.h>
61: #include <time.h>
62: #include <err.h>
63:
64: void output __P((struct utmp *));
65: void who_am_i __P((FILE *));
66: void usage __P((void));
67: int only_current_term; /* show info about the current terminal only */
68: int show_term; /* show term state */
69: int show_idle; /* show idle time */
70:
71: int
72: main(argc, argv)
73: int argc;
74: char **argv;
75: {
76: struct utmp usr;
77: FILE *ufp, *file();
78: int c;
79:
80: while ((c = getopt(argc, argv, "mTu")) != -1) {
81: switch (c) {
82: case 'm':
83: only_current_term = 1;
84: break;
85: case 'T':
86: show_term = 1;
87: break;
88: case 'u':
89: show_idle = 1;
90: break;
91: default:
92: usage();
93: /* NOTREACHED */
94: }
95: }
96: argc -= optind;
97: argv += optind;
98:
99: if (chdir("/dev")) {
100: err(1, "cannot change directory to /dev");
101: /* NOTREACHED */
102: }
103:
104: switch (argc) {
105: case 0: /* who */
106: ufp = file(_PATH_UTMP);
107:
108: if (only_current_term) {
109: who_am_i(ufp);
110: } else {
111: /* only entries with both name and line fields */
112: while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
113: if (*usr.ut_name && *usr.ut_line)
114: output(&usr);
115: }
116: break;
117: case 1: /* who utmp_file */
118: ufp = file(*argv);
119:
120: if (only_current_term) {
121: who_am_i(ufp);
122: } else {
123: /* all entries */
124: while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
125: output(&usr);
126: }
127: break;
128: case 2: /* who am i */
129: ufp = file(_PATH_UTMP);
130: who_am_i(ufp);
131: break;
132: default:
133: usage();
134: /* NOTREACHED */
135: }
136: exit(0);
137: }
138:
139: void
140: who_am_i(ufp)
141: FILE *ufp;
142: {
143: struct utmp usr;
144: struct passwd *pw;
145: register char *p;
146: char *t;
147:
148: /* search through the utmp and find an entry for this tty */
149: if (p = ttyname(0)) {
150: /* strip any directory component */
1.3 ! millert 151: if (t = strrchr(p, '/'))
1.1 deraadt 152: p = t + 1;
153: while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
154: if (usr.ut_name && !strcmp(usr.ut_line, p)) {
155: output(&usr);
156: return;
157: }
158: /* well, at least we know what the tty is */
159: (void)strncpy(usr.ut_line, p, UT_LINESIZE);
160: } else
161: (void)strcpy(usr.ut_line, "tty??");
162:
163: pw = getpwuid(getuid());
164: (void)strncpy(usr.ut_name, pw ? pw->pw_name : "?", UT_NAMESIZE);
165: (void)time(&usr.ut_time);
166: *usr.ut_host = '\0';
167: output(&usr);
168: }
169:
170: void
171: output(up)
172: struct utmp *up;
173: {
174: struct stat sb;
175: char line[sizeof (up->ut_line) + 1];
176: char state;
177: static time_t now = 0;
178: time_t idle;
179:
180: if (show_term || show_idle) {
181: if (now == 0)
182: time(&now);
183:
184: strncpy (line, up->ut_line, sizeof (up->ut_line));
185: line[sizeof (up->ut_line)] = '\0';
186:
187: if (stat(line, &sb) == 0) {
188: state = (sb.st_mode & 020) ? '+' : '-';
189: idle = now - sb.st_atime;
190: } else {
191: state = '?';
192: idle = 0;
193: }
194:
195: }
196:
197: (void)printf("%-*.*s ", UT_NAMESIZE, UT_NAMESIZE, up->ut_name);
198:
199: if (show_term) {
200: (void)printf("%c ", state);
201: }
202:
203: (void)printf("%-*.*s ", UT_LINESIZE, UT_LINESIZE, up->ut_line);
204: (void)printf("%.12s ", ctime(&up->ut_time) + 4);
205:
206: if (show_idle) {
207: if (idle < 60)
208: (void)printf(" . ");
209: else if (idle < (24 * 60 * 60))
210: (void)printf("%02d:%02d ",
211: (idle / (60 * 60)),
212: (idle % (60 * 60)) / 60);
213: else
214: (void)printf(" old ");
215: }
216:
217: if (*up->ut_host)
218: printf("\t(%.*s)", UT_HOSTSIZE, up->ut_host);
219: (void)putchar('\n');
220: }
221:
222: FILE *
223: file(name)
224: char *name;
225: {
226: FILE *ufp;
227:
228: if (!(ufp = fopen(name, "r"))) {
229: err(1, "%s", name);
230: /* NOTREACHED */
231: }
232: return(ufp);
233: }
234:
235: void
236: usage()
237: {
238: (void)fprintf(stderr, "usage: who [-mTu] [ file ]\n who am i\n");
239: exit(1);
240: }