1 /* $NetBSD: who.c,v 1.22 2008/07/21 14:19:28 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. 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.
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
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)who.c 8.1 (Berkeley) 6/6/93";
45 __RCSID("$NetBSD: who.c,v 1.22 2008/07/21 14:19:28 lukem Exp $");
48 #include <sys/types.h>
66 #include "utmpentry.h"
68 static void output_labels(void);
69 static void who_am_i(const char *, int);
70 static void usage(void) __dead
;
71 static void process(const char *, int);
72 static void eprint(const struct utmpentry
*);
73 static void print(const char *, const char *, time_t, const char *, pid_t pid
,
74 uint16_t term
, uint16_t xit
, uint16_t sess
, uint16_t type
);
75 static void quick(const char *);
77 static int show_term
; /* show term state */
78 static int show_idle
; /* show idle time */
79 static int show_details
; /* show exit status etc. */
81 struct ut_type_names
{
87 { RUN_LVL
, "run level" },
88 { BOOT_TIME
, "boot time" },
89 { OLD_TIME
, "old time" },
90 { NEW_TIME
, "new time" },
91 { INIT_PROCESS
, "init process" },
92 { LOGIN_PROCESS
, "login process" },
93 { USER_PROCESS
, "user process" },
94 { DEAD_PROCESS
, "dead process" },
95 #if defined(_NETBSD_SOURCE)
96 { ACCOUNTING
, "accounting" },
97 { SIGNATURE
, "signature" },
98 { DOWN_TIME
, "down time" },
99 #endif /* _NETBSD_SOURCE */
100 #endif /* SUPPORT_UTMPX */
105 main(int argc
, char *argv
[])
107 int c
, only_current_term
, show_labels
, quick_mode
, default_mode
;
110 setlocale(LC_ALL
, "");
112 only_current_term
= show_term
= show_idle
= show_labels
= 0;
113 quick_mode
= default_mode
= 0;
115 while ((c
= getopt(argc
, argv
, "abdHlmpqrsTtuv")) != -1) {
119 show_idle
= show_details
= 1;
122 et
|= (1 << BOOT_TIME
);
125 et
|= (1 << DEAD_PROCESS
);
131 et
|= (1 << LOGIN_PROCESS
);
134 only_current_term
= 1;
137 et
|= (1 << INIT_PROCESS
);
143 et
|= (1 << RUN_LVL
);
152 et
|= (1 << NEW_TIME
);
172 err(EXIT_FAILURE
, "cannot change directory to /dev");
177 only_current_term
= show_term
= show_idle
= 0;
183 } else if (only_current_term
) {
184 who_am_i(NULL
, show_labels
);
186 process(NULL
, show_labels
);
189 case 1: /* who utmp_file */
192 } else if (only_current_term
) {
193 who_am_i(*argv
, show_labels
);
195 process(*argv
, show_labels
);
198 case 2: /* who am i */
199 who_am_i(NULL
, show_labels
);
210 strrstr(const char *str
, const char *pat
)
215 return __UNCONST(str
);
219 for (estr
= str
+ strlen(str
); str
< estr
; estr
--)
220 if (strncmp(estr
, pat
, len
) == 0)
221 return __UNCONST(estr
);
226 who_am_i(const char *fname
, int show_labels
)
232 struct utmpentry
*ehead
, *ep
;
234 /* search through the utmp and find an entry for this tty */
235 if ((p
= ttyname(STDIN_FILENO
)) != NULL
) {
237 /* strip directory prefixes for ttys */
238 if ((t
= strrstr(p
, "/pts/")) != NULL
||
239 (t
= strrchr(p
, '/')) != NULL
)
242 (void)getutentries(fname
, &ehead
);
243 for (ep
= ehead
; ep
; ep
= ep
->next
)
244 if (strcmp(ep
->line
, p
) == 0) {
254 pw
= getpwuid(getuid());
257 print(pw
? pw
->pw_name
: "?", p
, now
, "", getpid(), 0, 0, 0, 0);
261 process(const char *fname
, int show_labels
)
263 struct utmpentry
*ehead
, *ep
;
264 (void)getutentries(fname
, &ehead
);
267 for (ep
= ehead
; ep
!= NULL
; ep
= ep
->next
)
272 eprint(const struct utmpentry
*ep
)
274 print(ep
->name
, ep
->line
, (time_t)ep
->tv
.tv_sec
, ep
->host
, ep
->pid
,
275 ep
->term
, ep
->exit
, ep
->sess
, ep
->type
);
279 print(const char *name
, const char *line
, time_t t
, const char *host
,
280 pid_t pid
, uint16_t term
, uint16_t xit
, uint16_t sess
, uint16_t type
)
284 static time_t now
= 0;
286 const char *types
= NULL
;
292 for (i
= 0; ut_type_names
[i
].type
>= 0; i
++) {
293 types
= ut_type_names
[i
].name
;
294 if (ut_type_names
[i
].type
== type
)
298 if (show_term
|| show_idle
) {
302 if (stat(line
, &sb
) == 0) {
303 state
= (sb
.st_mode
& 020) ? '+' : '-';
304 idle
= now
- sb
.st_atime
;
309 (void)printf("%-*.*s ", maxname
, maxname
, name
);
312 (void)printf("%c ", state
);
314 (void)printf("%-*.*s ", maxline
, maxline
, line
);
315 (void)printf("%.12s ", ctime(&t
) + 4);
320 else if (idle
< (24 * 60 * 60))
321 (void)printf("%02ld:%02ld ",
322 (long)(idle
/ (60 * 60)),
323 (long)(idle
% (60 * 60)) / 60);
325 (void)printf(" old ");
327 (void)printf("\t%6d", pid
);
331 (void)printf("\tnew=%c old=%c", term
, xit
);
333 (void)printf("\tterm=%d exit=%d", term
, xit
);
334 (void)printf(" sess=%d", sess
);
335 (void)printf(" type=%s ", types
);
340 (void)printf("\t(%.*s)", maxhost
, host
);
347 (void)printf("%-*.*s ", maxname
, maxname
, "USER");
352 (void)printf("%-*.*s ", maxline
, maxline
, "LINE");
353 (void)printf("WHEN ");
356 (void)printf("IDLE ");
357 (void)printf("\t PID");
359 (void)printf("\tCOMMENT");
366 quick(const char *fname
)
368 struct utmpentry
*ehead
, *ep
;
371 (void)getutentries(fname
, &ehead
);
372 for (ep
= ehead
; ep
!= NULL
; ep
= ep
->next
) {
373 (void)printf("%-*s ", maxname
, ep
->name
);
374 if ((++num
% 8) == 0)
380 (void)printf("# users = %d\n", num
);
386 (void)fprintf(stderr
, "Usage: %s [-abdHlmqrsTtuv] [file]\n\t%s am i\n",
387 getprogname(), getprogname());