]>
git.saurik.com Git - apple/shell_cmds.git/blob - who/who.c
1 /* $NetBSD: who.c,v 1.6 1997/10/20 03:20:29 lukem Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
39 #include <sys/cdefs.h>
42 "@(#) Copyright (c) 1989, 1993\n\
43 The Regents of the University of California. All rights reserved.\n");
48 static char sccsid
[] = "@(#)who.c 8.1 (Berkeley) 6/6/93";
50 __RCSID("$NetBSD: who.c,v 1.6 1997/10/20 03:20:29 lukem Exp $");
53 #include <sys/types.h>
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));
71 int show_term
; /* show term state */
72 int show_idle
; /* show idle time */
74 int main
__P((int, char **));
83 int c
, only_current_term
, show_labels
;
85 setlocale(LC_ALL
, "");
87 only_current_term
= show_term
= show_idle
= show_labels
= 0;
88 while ((c
= getopt(argc
, argv
, "mTuH")) != -1) {
91 only_current_term
= 1;
111 err(1, "cannot change directory to /dev");
120 ufp
= file(_PATH_UTMP
);
122 if (only_current_term
) {
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
)
131 case 1: /* who utmp_file */
134 if (only_current_term
) {
138 while (fread((char *)&usr
, sizeof(usr
), 1, ufp
) == 1)
142 case 2: /* who am i */
143 ufp
= file(_PATH_UTMP
);
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
)
167 while (fread((char *)&usr
, sizeof(usr
), 1, ufp
) == 1)
168 if (usr
.ut_name
&& !strcmp(usr
.ut_line
, p
)) {
172 /* well, at least we know what the tty is */
173 (void)strncpy(usr
.ut_line
, p
, UT_LINESIZE
);
175 (void)strcpy(usr
.ut_line
, "tty??");
177 pw
= getpwuid(getuid());
178 (void)strncpy(usr
.ut_name
, pw
? pw
->pw_name
: "?", UT_NAMESIZE
);
179 (void)time(&usr
.ut_time
);
189 char line
[sizeof (up
->ut_line
) + 1];
191 static time_t now
= 0;
197 if (show_term
|| show_idle
) {
201 strncpy(line
, up
->ut_line
, sizeof (up
->ut_line
));
202 line
[sizeof (up
->ut_line
)] = '\0';
204 if (stat(line
, &sb
) == 0) {
205 state
= (sb
.st_mode
& 020) ? '+' : '-';
206 idle
= now
- sb
.st_atime
;
211 (void)printf("%-*.*s ", UT_NAMESIZE
, UT_NAMESIZE
, up
->ut_name
);
214 (void)printf("%c ", state
);
217 (void)printf("%-*.*s ", UT_LINESIZE
, UT_LINESIZE
, up
->ut_line
);
218 (void)printf("%.12s ", ctime(&up
->ut_time
) + 4);
223 else if (idle
< (24 * 60 * 60))
224 (void)printf("%02ld:%02ld ",
225 (long)(idle
/ (60 * 60)),
226 (long)(idle
% (60 * 60)) / 60);
228 (void)printf(" old ");
232 printf("\t(%.*s)", UT_HOSTSIZE
, up
->ut_host
);
239 (void)printf("%-*.*s ", UT_NAMESIZE
, UT_NAMESIZE
, "USER");
244 (void)printf("%-*.*s ", UT_LINESIZE
, UT_LINESIZE
, "LINE");
245 (void)printf("WHEN ");
248 (void)printf("IDLE ");
250 (void)printf("\t%.*s", UT_HOSTSIZE
, "FROM");
261 if (!(ufp
= fopen(name
, "r"))) {
271 (void)fprintf(stderr
, "usage: who [-mTuH] [ file ]\n who am i\n");