Commit | Line | Data |
---|---|---|
51df51c9 | 1 | /* |
82227205 | 2 | * Copyright (c) 1980, 1989 Regents of the University of California. |
51df51c9 DF |
3 | * All rights reserved. The Berkeley software License Agreement |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
b899e9ac | 7 | #ifndef lint |
51df51c9 | 8 | char copyright[] = |
82227205 | 9 | "@(#) Copyright (c) 1980, 1989 Regents of the University of California.\n\ |
51df51c9 DF |
10 | All rights reserved.\n"; |
11 | #endif not lint | |
12 | ||
13 | #ifndef lint | |
82227205 | 14 | static char sccsid[] = "@(#)who.c 5.9 (Berkeley) %G%"; |
51df51c9 DF |
15 | #endif not lint |
16 | ||
aba4c79c BJ |
17 | /* |
18 | * who | |
19 | */ | |
20 | ||
f143abdb | 21 | #include <sys/types.h> |
aba4c79c BJ |
22 | #include <utmp.h> |
23 | #include <pwd.h> | |
f143abdb | 24 | #include <stdio.h> |
751509f1 | 25 | #include <ctype.h> |
aba4c79c | 26 | |
ddb5c47a MK |
27 | #define NMAX sizeof(utmp.ut_name) |
28 | #define LMAX sizeof(utmp.ut_line) | |
29 | #define HMAX sizeof(utmp.ut_host) | |
30 | ||
31 | struct utmp utmp; | |
32 | struct passwd *pw; | |
33 | struct passwd *getpwuid(); | |
aba4c79c | 34 | |
ddb5c47a | 35 | char *ttyname(), *rindex(), *ctime(), *strcpy(); |
cf288731 | 36 | |
ddb5c47a MK |
37 | main(argc, argv) |
38 | int argc; | |
39 | char **argv; | |
aba4c79c | 40 | { |
ddb5c47a MK |
41 | register char *tp, *s; |
42 | register FILE *fi; | |
aba4c79c | 43 | |
f493420a | 44 | s = _PATH_UTMP; |
ddb5c47a MK |
45 | if(argc == 2) |
46 | s = argv[1]; | |
47 | if (argc == 3) { | |
48 | tp = ttyname(0); | |
49 | if (tp) | |
50 | tp = rindex(tp, '/') + 1; | |
51 | else { /* no tty - use best guess from passwd file */ | |
58eafdab | 52 | (void)strcpy(utmp.ut_line, "tty??"); |
ddb5c47a MK |
53 | guess(); |
54 | exit(0); | |
55 | } | |
aba4c79c | 56 | } |
58eafdab KB |
57 | if (!(fi = fopen(s, "r"))) { |
58 | fprintf(stderr, "who: cannot read %s.\n", s); | |
aba4c79c BJ |
59 | exit(1); |
60 | } | |
ddb5c47a | 61 | while (fread((char *)&utmp, sizeof(utmp), 1, fi) == 1) { |
b899e9ac | 62 | if (argc == 3) { |
ddb5c47a MK |
63 | if (strcmp(utmp.ut_line, tp)) |
64 | continue; | |
e2ef4360 KB |
65 | if (!utmp.ut_name[0]) |
66 | guess(); | |
67 | else | |
68 | putline(); | |
ddb5c47a MK |
69 | exit(0); |
70 | } | |
71 | if (utmp.ut_name[0] == '\0' && argc == 1) | |
72 | continue; | |
73 | putline(); | |
74 | } | |
75 | if (argc == 3) { | |
76 | strncpy(utmp.ut_line, tp, sizeof(utmp.ut_line)); | |
77 | guess(); | |
78 | } | |
79 | exit(0); | |
aba4c79c BJ |
80 | } |
81 | ||
82 | putline() | |
83 | { | |
ddb5c47a | 84 | register char *cbuf; |
aba4c79c | 85 | |
ddb5c47a MK |
86 | printf("%-*.*s %-*.*s", |
87 | NMAX, NMAX, utmp.ut_name, | |
88 | LMAX, LMAX, utmp.ut_line); | |
89 | cbuf = ctime(&utmp.ut_time); | |
90 | printf("%.12s", cbuf+4); | |
91 | if (utmp.ut_host[0]) | |
92 | printf("\t(%.*s)", HMAX, utmp.ut_host); | |
b899e9ac | 93 | putchar('\n'); |
aba4c79c | 94 | } |
ddb5c47a MK |
95 | |
96 | guess() | |
97 | { | |
ddb5c47a MK |
98 | pw = getpwuid(getuid()); |
99 | strncpy(utmp.ut_name, pw ? pw->pw_name : "?", NMAX); | |
100 | time(&utmp.ut_time); | |
82227205 | 101 | utmp.ut_host[0] = '\0'; |
ddb5c47a MK |
102 | putline(); |
103 | } |