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 | |
e2ef4360 | 14 | static char sccsid[] = "@(#)who.c 5.5 (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 */ | |
58eafdab | 51 | (void)strcpy(utmp.ut_line, "tty??"); |
ddb5c47a MK |
52 | guess(); |
53 | exit(0); | |
54 | } | |
aba4c79c | 55 | } |
58eafdab KB |
56 | if (!(fi = fopen(s, "r"))) { |
57 | fprintf(stderr, "who: cannot read %s.\n", s); | |
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; | |
e2ef4360 KB |
64 | if (!utmp.ut_name[0]) |
65 | guess(); | |
66 | else | |
67 | putline(); | |
ddb5c47a MK |
68 | exit(0); |
69 | } | |
70 | if (utmp.ut_name[0] == '\0' && argc == 1) | |
71 | continue; | |
72 | putline(); | |
73 | } | |
74 | if (argc == 3) { | |
75 | strncpy(utmp.ut_line, tp, sizeof(utmp.ut_line)); | |
76 | guess(); | |
77 | } | |
78 | exit(0); | |
aba4c79c BJ |
79 | } |
80 | ||
81 | putline() | |
82 | { | |
ddb5c47a | 83 | register char *cbuf; |
aba4c79c | 84 | |
ddb5c47a MK |
85 | printf("%-*.*s %-*.*s", |
86 | NMAX, NMAX, utmp.ut_name, | |
87 | LMAX, LMAX, utmp.ut_line); | |
88 | cbuf = ctime(&utmp.ut_time); | |
89 | printf("%.12s", cbuf+4); | |
90 | if (utmp.ut_host[0]) | |
91 | printf("\t(%.*s)", HMAX, utmp.ut_host); | |
b899e9ac | 92 | putchar('\n'); |
aba4c79c | 93 | } |
ddb5c47a MK |
94 | |
95 | guess() | |
96 | { | |
ddb5c47a MK |
97 | pw = getpwuid(getuid()); |
98 | strncpy(utmp.ut_name, pw ? pw->pw_name : "?", NMAX); | |
99 | time(&utmp.ut_time); | |
100 | putline(); | |
101 | } |