xref: /netbsd-src/usr.bin/who/who.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: who.c,v 1.8 1999/07/17 16:41:44 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.8 1999/07/17 16:41:44 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 #include <utmp.h>
64 
65 void output __P((struct utmp *));
66 void output_labels __P((void));
67 void who_am_i __P((FILE *));
68 FILE *file __P((char *));
69 void usage __P((void));
70 
71 int show_term;			/* show term state */
72 int show_idle;			/* show idle time */
73 
74 int main __P((int, char **));
75 
76 int
77 main(argc, argv)
78 	int argc;
79 	char **argv;
80 {
81 	struct utmp usr;
82 	FILE *ufp;
83 	int c, only_current_term, show_labels;
84 
85 	setlocale(LC_ALL, "");
86 
87 	only_current_term = show_term = show_idle = show_labels = 0;
88 	while ((c = getopt(argc, argv, "mTuH")) != -1) {
89 		switch (c) {
90 		case 'm':
91 			only_current_term = 1;
92 			break;
93 		case 'T':
94 			show_term = 1;
95 			break;
96 		case 'u':
97 			show_idle = 1;
98 			break;
99 		case 'H':
100 			show_labels = 1;
101 			break;
102 		default:
103 			usage();
104 			/* NOTREACHED */
105 		}
106 	}
107 	argc -= optind;
108 	argv += optind;
109 
110 	if (chdir("/dev")) {
111 		err(1, "cannot change directory to /dev");
112 		/* NOTREACHED */
113 	}
114 
115 	if (show_labels)
116 		output_labels();
117 
118 	switch (argc) {
119 	case 0:					/* who */
120 		ufp = file(_PATH_UTMP);
121 
122 		if (only_current_term) {
123 			who_am_i(ufp);
124 		} else {
125 			/* only entries with both name and line fields */
126 			while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
127 				if (*usr.ut_name && *usr.ut_line)
128 					output(&usr);
129 		}
130 		break;
131 	case 1:					/* who utmp_file */
132 		ufp = file(*argv);
133 
134 		if (only_current_term) {
135 			who_am_i(ufp);
136 		} else {
137 			/* all entries */
138 			while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
139 				output(&usr);
140 		}
141 		break;
142 	case 2:					/* who am i */
143 		ufp = file(_PATH_UTMP);
144 		who_am_i(ufp);
145 		break;
146 	default:
147 		usage();
148 		/* NOTREACHED */
149 	}
150 	exit(0);
151 }
152 
153 void
154 who_am_i(ufp)
155 	FILE *ufp;
156 {
157 	struct utmp usr;
158 	struct passwd *pw;
159 	char *p;
160 	char *t;
161 
162 	/* search through the utmp and find an entry for this tty */
163 	if ((p = ttyname(0)) != NULL) {
164 		/* strip any directory component */
165 		if ((t = strrchr(p, '/')) != NULL)
166 			p = t + 1;
167 		while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
168 			if (usr.ut_name[0] != '\0' &&
169 			    !strcmp(usr.ut_line, p)) {
170 				output(&usr);
171 				return;
172 			}
173 		/* well, at least we know what the tty is */
174 		(void)strncpy(usr.ut_line, p, UT_LINESIZE);
175 	} else
176 		(void)strcpy(usr.ut_line, "tty??");
177 
178 	pw = getpwuid(getuid());
179 	(void)strncpy(usr.ut_name, pw ? pw->pw_name : "?", UT_NAMESIZE);
180 	(void)time(&usr.ut_time);
181 	*usr.ut_host = '\0';
182 	output(&usr);
183 }
184 
185 void
186 output(up)
187 	struct utmp *up;
188 {
189 	struct stat sb;
190 	char line[sizeof (up->ut_line) + 1];
191 	char state;
192 	static time_t now = 0;
193 	time_t idle;
194 
195 	state = '?';
196 	idle = 0;
197 
198 	if (show_term || show_idle) {
199 		if (now == 0)
200 			time(&now);
201 
202 		strncpy(line, up->ut_line, sizeof (up->ut_line));
203 		line[sizeof (up->ut_line)] = '\0';
204 
205 		if (stat(line, &sb) == 0) {
206 			state = (sb.st_mode & 020) ? '+' : '-';
207 			idle = now - sb.st_atime;
208 		}
209 
210 	}
211 
212 	(void)printf("%-*.*s ", (int)UT_NAMESIZE, (int)UT_NAMESIZE, up->ut_name);
213 
214 	if (show_term) {
215 		(void)printf("%c ", state);
216 	}
217 
218 	(void)printf("%-*.*s ", (int)UT_LINESIZE, (int)UT_LINESIZE, up->ut_line);
219 	(void)printf("%.12s ", ctime(&up->ut_time) + 4);
220 
221 	if (show_idle) {
222 		if (idle < 60)
223 			(void)printf("  .   ");
224 		else if (idle < (24 * 60 * 60))
225 			(void)printf("%02ld:%02ld ",
226 				     (long)(idle / (60 * 60)),
227 				     (long)(idle % (60 * 60)) / 60);
228 		else
229 			(void)printf(" old  ");
230 	}
231 
232 	if (*up->ut_host)
233 		printf("\t(%.*s)", (int)UT_HOSTSIZE, up->ut_host);
234 	(void)putchar('\n');
235 }
236 
237 void
238 output_labels()
239 {
240 	(void)printf("%-*.*s ", (int)UT_NAMESIZE, (int)UT_NAMESIZE, "USER");
241 
242 	if (show_term)
243 		(void)printf("S ");
244 
245 	(void)printf("%-*.*s ", (int)UT_LINESIZE, (int)UT_LINESIZE, "LINE");
246 	(void)printf("WHEN         ");
247 
248 	if (show_idle)
249 		(void)printf("IDLE  ");
250 
251 	(void)printf("\t%.*s", (int)UT_HOSTSIZE, "FROM");
252 
253 	(void)putchar('\n');
254 }
255 
256 FILE *
257 file(name)
258 	char *name;
259 {
260 	FILE *ufp;
261 
262 	if (!(ufp = fopen(name, "r"))) {
263 		err(1, "%s", name);
264 		/* NOTREACHED */
265 	}
266 	return (ufp);
267 }
268 
269 void
270 usage()
271 {
272 	(void)fprintf(stderr, "usage: who [-mTuH] [ file ]\n       who am i\n");
273 	exit(1);
274 }
275