]> git.saurik.com Git - apple/shell_cmds.git/blob - who/who.c
shell_cmds-56.tar.gz
[apple/shell_cmds.git] / who / who.c
1 /* $NetBSD: who.c,v 1.6 1997/10/20 03:20:29 lukem 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.6 1997/10/20 03:20:29 lukem 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 && !strcmp(usr.ut_line, p)) {
169 output(&usr);
170 return;
171 }
172 /* well, at least we know what the tty is */
173 (void)strncpy(usr.ut_line, p, UT_LINESIZE);
174 } else
175 (void)strcpy(usr.ut_line, "tty??");
176
177 pw = getpwuid(getuid());
178 (void)strncpy(usr.ut_name, pw ? pw->pw_name : "?", UT_NAMESIZE);
179 (void)time(&usr.ut_time);
180 *usr.ut_host = '\0';
181 output(&usr);
182 }
183
184 void
185 output(up)
186 struct utmp *up;
187 {
188 struct stat sb;
189 char line[sizeof (up->ut_line) + 1];
190 char state;
191 static time_t now = 0;
192 time_t idle;
193
194 state = '?';
195 idle = 0;
196
197 if (show_term || show_idle) {
198 if (now == 0)
199 time(&now);
200
201 strncpy(line, up->ut_line, sizeof (up->ut_line));
202 line[sizeof (up->ut_line)] = '\0';
203
204 if (stat(line, &sb) == 0) {
205 state = (sb.st_mode & 020) ? '+' : '-';
206 idle = now - sb.st_atime;
207 }
208
209 }
210
211 (void)printf("%-*.*s ", UT_NAMESIZE, UT_NAMESIZE, up->ut_name);
212
213 if (show_term) {
214 (void)printf("%c ", state);
215 }
216
217 (void)printf("%-*.*s ", UT_LINESIZE, UT_LINESIZE, up->ut_line);
218 (void)printf("%.12s ", ctime(&up->ut_time) + 4);
219
220 if (show_idle) {
221 if (idle < 60)
222 (void)printf(" . ");
223 else if (idle < (24 * 60 * 60))
224 (void)printf("%02ld:%02ld ",
225 (long)(idle / (60 * 60)),
226 (long)(idle % (60 * 60)) / 60);
227 else
228 (void)printf(" old ");
229 }
230
231 if (*up->ut_host)
232 printf("\t(%.*s)", UT_HOSTSIZE, up->ut_host);
233 (void)putchar('\n');
234 }
235
236 void
237 output_labels()
238 {
239 (void)printf("%-*.*s ", UT_NAMESIZE, UT_NAMESIZE, "USER");
240
241 if (show_term)
242 (void)printf("S ");
243
244 (void)printf("%-*.*s ", UT_LINESIZE, UT_LINESIZE, "LINE");
245 (void)printf("WHEN ");
246
247 if (show_idle)
248 (void)printf("IDLE ");
249
250 (void)printf("\t%.*s", UT_HOSTSIZE, "FROM");
251
252 (void)putchar('\n');
253 }
254
255 FILE *
256 file(name)
257 char *name;
258 {
259 FILE *ufp;
260
261 if (!(ufp = fopen(name, "r"))) {
262 err(1, "%s", name);
263 /* NOTREACHED */
264 }
265 return (ufp);
266 }
267
268 void
269 usage()
270 {
271 (void)fprintf(stderr, "usage: who [-mTuH] [ file ]\n who am i\n");
272 exit(1);
273 }