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.1 (Berkeley) 5/2/85";
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: char hostname[32];
34:
35: char *ttyname(), *rindex(), *ctime(), *strcpy();
36:
37: main(argc, argv)
38: int argc;
39: char **argv;
40: {
41: register char *tp, *s;
42: register FILE *fi;
43: extern char _sobuf[];
44:
45: setbuf(stdout, _sobuf);
46: s = "/etc/utmp";
47: if(argc == 2)
48: s = argv[1];
49: if (argc == 3) {
50: tp = ttyname(0);
51: if (tp)
52: tp = rindex(tp, '/') + 1;
53: else { /* no tty - use best guess from passwd file */
54: pw = getpwuid(getuid());
55: strncpy(utmp.ut_name, pw ? pw->pw_name : "?", NMAX);
56: strcpy(utmp.ut_line, "tty??");
57: time(&utmp.ut_time);
58: putline();
59: exit(0);
60: }
61: }
62: if ((fi = fopen(s, "r")) == NULL) {
63: puts("who: cannot open utmp");
64: exit(1);
65: }
66: while (fread((char *)&utmp, sizeof(utmp), 1, fi) == 1) {
67: if (argc == 3) {
68: gethostname(hostname, sizeof (hostname));
69: if (strcmp(utmp.ut_line, tp))
70: continue;
71: printf("%s!", hostname);
72: putline();
73: exit(0);
74: }
75: if (utmp.ut_name[0] == '\0' && argc == 1)
76: continue;
77: putline();
78: }
79: }
80:
81: putline()
82: {
83: register char *cbuf;
84:
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);
92: putchar('\n');
93: }
Defined functions
main
defined in line
37;
never used
Defined variables
pw
defined in line
31; used 3 times
utmp
defined in line
30; used 15 times
Defined macros
HMAX
defined in line
28; used 1 times
LMAX
defined in line
27; used 2 times
NMAX
defined in line
26; used 3 times