|
|
1.1 root 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:
7: #ifndef lint
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
14: static char sccsid[] = "@(#)who.c 5.4 (Berkeley) 4/20/88";
15: #endif not lint
16:
17: /*
18: * who
19: */
20:
21: #include <stdio.h>
22: #include <utmp.h>
23: #include <pwd.h>
24: #include <ctype.h>
25:
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();
33:
34: char *ttyname(), *rindex(), *ctime(), *strcpy();
35:
36: main(argc, argv)
37: int argc;
38: char **argv;
39: {
40: register char *tp, *s;
41: register FILE *fi;
42:
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: (void)strcpy(utmp.ut_line, "tty??");
52: guess();
53: exit(0);
54: }
55: }
56: if (!(fi = fopen(s, "r"))) {
57: fprintf(stderr, "who: cannot read %s.\n", s);
58: exit(1);
59: }
60: while (fread((char *)&utmp, sizeof(utmp), 1, fi) == 1) {
61: if (argc == 3) {
62: if (strcmp(utmp.ut_line, tp))
63: continue;
64: putline();
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);
76: }
77:
78: putline()
79: {
80: register char *cbuf;
81:
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);
89: putchar('\n');
90: }
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: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.