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