]>
git.saurik.com Git - apple/shell_cmds.git/blob - who/who.c
1 /* $NetBSD: who.c,v 1.23 2008/07/24 15:35:41 christos 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.23 2008/07/24 15:35:41 christos Exp $");
48 #include <sys/types.h>
69 #endif /* __APPLE__ */
71 #include "utmpentry.h"
74 #define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
75 #endif /* __APPLE__ */
77 static void output_labels(void);
78 static void who_am_i(const char *, int);
79 static void usage(void) __dead
;
80 static void process(const char *, int);
81 static void eprint(const struct utmpentry
*);
82 static void print(const char *, const char *, time_t, const char *, pid_t pid
,
83 uint16_t term
, uint16_t xit
, uint16_t sess
, uint16_t type
);
84 static void quick(const char *);
86 static int show_term
; /* show term state */
87 static int show_idle
; /* show idle time */
89 static int show_details
; /* show exit status etc. */
90 #endif /* !__APPLE__ */
92 struct ut_type_names
{
98 { RUN_LVL
, "run level" },
99 { BOOT_TIME
, "boot time" },
100 { OLD_TIME
, "old time" },
101 { NEW_TIME
, "new time" },
102 { INIT_PROCESS
, "init process" },
103 { LOGIN_PROCESS
, "login process" },
104 { USER_PROCESS
, "user process" },
105 { DEAD_PROCESS
, "dead process" },
106 #if defined(_NETBSD_SOURCE)
107 { ACCOUNTING
, "accounting" },
108 { SIGNATURE
, "signature" },
109 { DOWN_TIME
, "down time" },
110 #endif /* _NETBSD_SOURCE */
111 #endif /* SUPPORT_UTMPX */
116 main(int argc
, char *argv
[])
118 int c
, only_current_term
, show_labels
, quick_mode
, default_mode
;
121 setlocale(LC_ALL
, "");
123 only_current_term
= show_term
= show_idle
= show_labels
= 0;
124 quick_mode
= default_mode
= 0;
126 while ((c
= getopt(argc
, argv
, "abdHlmpqrsTtuv")) != -1) {
132 #else /* !__APPLE__ */
133 show_idle
= show_details
= 1;
134 #endif /* __APPLE__ */
137 et
|= (1 << BOOT_TIME
);
140 et
|= (1 << DEAD_PROCESS
);
146 et
|= (1 << LOGIN_PROCESS
);
149 only_current_term
= 1;
152 et
|= (1 << INIT_PROCESS
);
158 et
|= (1 << RUN_LVL
);
167 et
|= (1 << NEW_TIME
);
176 #endif /* !__APPLE__ */
190 err(EXIT_FAILURE
, "cannot change directory to /dev");
193 #endif /* !__APPLE__ */
196 only_current_term
= show_term
= show_idle
= 0;
202 } else if (only_current_term
) {
203 who_am_i(NULL
, show_labels
);
205 process(NULL
, show_labels
);
208 case 1: /* who utmp_file */
211 } else if (only_current_term
) {
212 who_am_i(*argv
, show_labels
);
214 process(*argv
, show_labels
);
217 case 2: /* who am i */
218 who_am_i(NULL
, show_labels
);
229 strrstr(const char *str
, const char *pat
)
234 return __UNCONST(str
);
238 for (estr
= str
+ strlen(str
); str
< estr
; estr
--)
239 if (strncmp(estr
, pat
, len
) == 0)
240 return __UNCONST(estr
);
245 who_am_i(const char *fname
, int show_labels
)
251 struct utmpentry
*ehead
, *ep
;
253 /* search through the utmp and find an entry for this tty */
254 if ((p
= ttyname(STDIN_FILENO
)) != NULL
) {
256 /* strip directory prefixes for ttys */
257 if ((t
= strrstr(p
, "/pts/")) != NULL
||
258 (t
= strrchr(p
, '/')) != NULL
)
261 (void)getutentries(fname
, &ehead
);
262 for (ep
= ehead
; ep
; ep
= ep
->next
)
263 if (strcmp(ep
->line
, p
) == 0) {
273 pw
= getpwuid(getuid());
276 print(pw
? pw
->pw_name
: "?", p
, now
, "", getpid(), 0, 0, 0, 0);
280 process(const char *fname
, int show_labels
)
282 struct utmpentry
*ehead
, *ep
;
283 (void)getutentries(fname
, &ehead
);
286 for (ep
= ehead
; ep
!= NULL
; ep
= ep
->next
)
289 if ((etype
& (1 << RUN_LVL
)) != 0) {
290 printf(" . run-level 3\n");
292 #endif /* __APPLE__ */
296 eprint(const struct utmpentry
*ep
)
298 print(ep
->name
, ep
->line
, (time_t)ep
->tv
.tv_sec
, ep
->host
, ep
->pid
,
301 #else /* !__APPLE__ */
302 ep
->term
, ep
->exit
, ep
->sess
, ep
->type
);
303 #endif /* __APPLE__ */
307 print(const char *name
, const char *line
, time_t t
, const char *host
,
308 pid_t pid
, uint16_t term
, uint16_t xit
, uint16_t sess
, uint16_t type
)
312 static time_t now
= 0;
314 const char *types
= NULL
;
320 for (i
= 0; ut_type_names
[i
].type
>= 0; i
++) {
321 types
= ut_type_names
[i
].name
;
322 if (ut_type_names
[i
].type
== type
)
326 if (show_term
|| show_idle
) {
331 char tty
[PATH_MAX
+ 1];
332 snprintf(tty
, sizeof(tty
), "%s%s", _PATH_DEV
, line
);
333 if (stat(tty
, &sb
) == 0) {
334 #else /* !__APPLE__ */
335 if (stat(line
, &sb
) == 0) {
336 #endif /* __APPLE__ */
337 state
= (sb
.st_mode
& 020) ? '+' : '-';
338 idle
= now
- sb
.st_atime
;
346 (void)printf("%-*.*s ", maxname
, maxname
, "LOGIN");
349 (void)printf("%-*.*s ", maxname
, maxname
, "reboot");
352 (void)printf("%-*.*s ", maxname
, maxname
, name
);
355 #else /* !__APPLE__ */
356 (void)printf("%-*.*s ", maxname
, maxname
, name
);
357 #endif /* __APPLE__ */
360 (void)printf("%c ", state
);
363 (void)printf("%-*.*s ", maxline
, maxline
, type
== BOOT_TIME
? "~" : line
);
364 #else /* !__APPLE__ */
365 (void)printf("%-*.*s ", maxline
, maxline
, line
);
366 #endif /* __APPLE__ */
367 (void)printf("%.12s ", ctime(&t
) + 4);
372 else if (idle
< (24 * 60 * 60))
373 (void)printf("%02ld:%02ld ",
374 (long)(idle
/ (60 * 60)),
375 (long)(idle
% (60 * 60)) / 60);
377 (void)printf(" old ");
379 (void)printf("\t%6d", pid
);
384 (void)printf("\tnew=%c old=%c", term
, xit
);
386 (void)printf("\tterm=%d exit=%d", term
, xit
);
387 (void)printf(" sess=%d", sess
);
388 (void)printf(" type=%s ", types
);
390 #endif /* !__APPLE__ */
395 if (type
== DEAD_PROCESS
)
396 (void)printf("\tterm=%d exit=%d", 0, 0);
397 #endif /* __APPLE__ */
400 (void)printf("\t(%.*s)", maxhost
, host
);
407 (void)printf("%-*.*s ", maxname
, maxname
, "USER");
412 (void)printf("%-*.*s ", maxline
, maxline
, "LINE");
413 (void)printf("WHEN ");
416 (void)printf("IDLE ");
417 (void)printf("\t PID");
419 (void)printf("\tCOMMENT");
426 quick(const char *fname
)
428 struct utmpentry
*ehead
, *ep
;
431 (void)getutentries(fname
, &ehead
);
432 for (ep
= ehead
; ep
!= NULL
; ep
= ep
->next
) {
433 (void)printf("%-*s ", maxname
, ep
->name
);
434 if ((++num
% 8) == 0)
440 (void)printf("# users = %d\n", num
);
447 (void)fprintf(stderr
, "Usage: %s [-abdHlmpqrsTtu] [file]\n\t%s am i\n",
448 #else /* !__APPLE__ */
449 (void)fprintf(stderr
, "Usage: %s [-abdHlmqrsTtuv] [file]\n\t%s am i\n",
450 #endif /* __APPLE__ */
451 getprogname(), getprogname());