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: #if defined(DOSCCS) && !defined(lint)
8: char copyright[] =
9: "@(#) Copyright (c) 1980 Regents of the University of California.\n\
10: All rights reserved.\n";
11:
12: static char sccsid[] = "@(#)who.c 5.1.3 (2.11BSD GTE) 1997/7/29";
13: #endif
14:
15: #include <stdio.h>
16: #include <utmp.h>
17: #include <pwd.h>
18: #include <ctype.h>
19: #include <sys/param.h> /* for MAXHOSTNAMELEN */
20: #include <string.h>
21: #include <time.h>
22: #include <unistd.h>
23:
24: #define NMAX sizeof(utmp.ut_name)
25: #define LMAX sizeof(utmp.ut_line)
26: #define HMAX sizeof(utmp.ut_host)
27:
28: struct utmp utmp;
29: struct passwd *pw;
30: char hostname[MAXHOSTNAMELEN];
31:
32: main(argc, argv)
33: int argc;
34: char **argv;
35: {
36: register char *tp, *s;
37: register FILE *fi;
38:
39: s = _PATH_UTMP;
40: if(argc == 2)
41: s = argv[1];
42: if (argc == 3) {
43: tp = ttyname(0);
44: if (tp)
45: tp = rindex(tp, '/') + 1;
46: else { /* no tty - use best guess from passwd file */
47: pw = getpwuid(getuid());
48: strncpy(utmp.ut_name, pw ? pw->pw_name : "?", NMAX);
49: strcpy(utmp.ut_line, "tty??");
50: time(&utmp.ut_time);
51: putline();
52: exit(0);
53: }
54: }
55: if ((fi = fopen(s, "r")) == NULL) {
56: fprintf(stderr, "who: cannot open %s", s);
57: exit(1);
58: }
59: while (fread((char *)&utmp, sizeof(utmp), 1, fi) == 1) {
60: if (argc == 3) {
61: gethostname(hostname, sizeof (hostname));
62: if (strcmp(utmp.ut_line, tp))
63: continue;
64: printf("%s!", hostname);
65: putline();
66: exit(0);
67: }
68: if (utmp.ut_name[0] == '\0' && argc == 1)
69: continue;
70: putline();
71: }
72: }
73:
74: putline()
75: {
76: register char *cbuf;
77:
78: printf("%-*.*s %-*.*s",
79: NMAX, NMAX, utmp.ut_name,
80: LMAX, LMAX, utmp.ut_line);
81: cbuf = ctime(&utmp.ut_time);
82: printf("%.12s", cbuf+4);
83: if (utmp.ut_host[0])
84: printf("\t(%.*s)", HMAX, utmp.ut_host);
85: putchar('\n');
86: }
Defined functions
main
defined in line
32;
never used
Defined variables
pw
defined in line
29; used 3 times
utmp
defined in line
28; used 15 times
Defined macros
HMAX
defined in line
26; used 1 times
LMAX
defined in line
25; used 2 times
NMAX
defined in line
24; used 3 times