who.c revision 1.16 1 /* $NetBSD: who.c,v 1.16 2005/07/22 14:23:05 peter 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT(
38 "@(#) Copyright (c) 1989, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)who.c 8.1 (Berkeley) 6/6/93";
45 #endif
46 __RCSID("$NetBSD: who.c,v 1.16 2005/07/22 14:23:05 peter Exp $");
47 #endif /* not lint */
48
49 #include <sys/types.h>
50 #include <sys/stat.h>
51
52 #include <err.h>
53 #include <locale.h>
54 #include <pwd.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <time.h>
59 #include <unistd.h>
60
61 #include "utmpentry.h"
62
63 static void output_labels(void);
64 static void who_am_i(const char *, int);
65 static void usage(void);
66 static void process(const char *, int);
67 static void print(const char *, const char *, time_t, const char *);
68 static void quick(const char *);
69
70 static int show_term; /* show term state */
71 static int show_idle; /* show idle time */
72
73 extern int maxname, maxline, maxhost;
74
75 int
76 main(int argc, char *argv[])
77 {
78 int c, only_current_term, show_labels, quick_mode, default_mode;
79
80 setlocale(LC_ALL, "");
81
82 only_current_term = show_term = show_idle = show_labels = 0;
83 quick_mode = default_mode = 0;
84
85 while ((c = getopt(argc, argv, "HmqsTu")) != -1) {
86 switch (c) {
87 case 'H':
88 show_labels = 1;
89 break;
90 case 'm':
91 only_current_term = 1;
92 break;
93 case 'q':
94 quick_mode = 1;
95 break;
96 case 's':
97 default_mode = 1;
98 break;
99 case 'T':
100 show_term = 1;
101 break;
102 case 'u':
103 show_idle = 1;
104 break;
105 default:
106 usage();
107 /* NOTREACHED */
108 }
109 }
110 argc -= optind;
111 argv += optind;
112
113 if (chdir("/dev")) {
114 err(EXIT_FAILURE, "cannot change directory to /dev");
115 /* NOTREACHED */
116 }
117
118 if (default_mode)
119 only_current_term = show_term = show_idle = 0;
120
121 switch (argc) {
122 case 0: /* who */
123 if (quick_mode) {
124 quick(NULL);
125 } else if (only_current_term) {
126 who_am_i(NULL, show_labels);
127 } else {
128 process(NULL, show_labels);
129 }
130 break;
131 case 1: /* who utmp_file */
132 if (quick_mode) {
133 quick(*argv);
134 } else if (only_current_term) {
135 who_am_i(*argv, show_labels);
136 } else {
137 process(*argv, show_labels);
138 }
139 break;
140 case 2: /* who am i */
141 who_am_i(NULL, show_labels);
142 break;
143 default:
144 usage();
145 /* NOTREACHED */
146 }
147
148 return 0;
149 }
150
151 static void
152 who_am_i(const char *fname, int show_labels)
153 {
154 struct passwd *pw;
155 const char *p;
156 char *t;
157 time_t now;
158 struct utmpentry *ehead, *ep;
159
160 /* search through the utmp and find an entry for this tty */
161 if ((p = ttyname(STDIN_FILENO)) != NULL) {
162
163 /* strip any directory component */
164 if ((t = strrchr(p, '/')) != NULL)
165 p = t + 1;
166
167 (void)getutentries(fname, &ehead);
168 for (ep = ehead; ep; ep = ep->next)
169 if (strcmp(ep->line, p) == 0) {
170 if (show_labels)
171 output_labels();
172 print(ep->name, ep->line, (time_t)ep->tv.tv_sec,
173 ep->host);
174 return;
175 }
176 } else
177 p = "tty??";
178
179 (void)time(&now);
180 pw = getpwuid(getuid());
181 if (show_labels)
182 output_labels();
183 print(pw ? pw->pw_name : "?", p, now, "");
184 }
185
186 static void
187 process(const char *fname, int show_labels)
188 {
189 struct utmpentry *ehead, *ep;
190 (void)getutentries(fname, &ehead);
191 if (show_labels)
192 output_labels();
193 for (ep = ehead; ep != NULL; ep = ep->next)
194 print(ep->name, ep->line, (time_t)ep->tv.tv_sec, ep->host);
195 }
196
197 static void
198 print(const char *name, const char *line, time_t t, const char *host)
199 {
200 struct stat sb;
201 char state;
202 static time_t now = 0;
203 time_t idle;
204
205 state = '?';
206 idle = 0;
207
208 if (show_term || show_idle) {
209 if (now == 0)
210 time(&now);
211
212 if (stat(line, &sb) == 0) {
213 state = (sb.st_mode & 020) ? '+' : '-';
214 idle = now - sb.st_atime;
215 }
216
217 }
218
219 (void)printf("%-*.*s ", maxname, maxname, name);
220
221 if (show_term)
222 (void)printf("%c ", state);
223
224 (void)printf("%-*.*s ", maxline, maxline, line);
225 (void)printf("%.12s ", ctime(&t) + 4);
226
227 if (show_idle) {
228 if (idle < 60)
229 (void)printf(" . ");
230 else if (idle < (24 * 60 * 60))
231 (void)printf("%02ld:%02ld ",
232 (long)(idle / (60 * 60)),
233 (long)(idle % (60 * 60)) / 60);
234 else
235 (void)printf(" old ");
236 }
237
238 if (*host)
239 (void)printf("\t(%.*s)", maxhost, host);
240 (void)putchar('\n');
241 }
242
243 static void
244 output_labels(void)
245 {
246 (void)printf("%-*.*s ", maxname, maxname, "USER");
247
248 if (show_term)
249 (void)printf("S ");
250
251 (void)printf("%-*.*s ", maxline, maxline, "LINE");
252 (void)printf("WHEN ");
253
254 if (show_idle)
255 (void)printf("IDLE ");
256
257 (void)printf("\t%.*s", maxhost, "FROM");
258
259 (void)putchar('\n');
260 }
261
262 static void
263 quick(const char *fname)
264 {
265 struct utmpentry *ehead, *ep;
266 int num = 0;
267
268 (void)getutentries(fname, &ehead);
269 for (ep = ehead; ep != NULL; ep = ep->next) {
270 (void)printf("%-*s ", maxname, ep->name);
271 if ((++num % 8) == 0)
272 (void)putchar('\n');
273 }
274 if (num % 8)
275 (void)putchar('\n');
276
277 (void)printf("# users = %d\n", num);
278 }
279
280 static void
281 usage(void)
282 {
283 (void)fprintf(stderr, "usage: %s [-HmqsTu] [file]\n %s am i\n",
284 getprogname(), getprogname());
285 exit(EXIT_FAILURE);
286 }
287