setgid to group "write" so that terminals need not be world writable
[unix-history] / usr / src / usr.bin / who / who.c
CommitLineData
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
8char 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
14static char sccsid[] = "@(#)who.c 5.1 (Berkeley) %G%";
15#endif not lint
16
aba4c79c
BJ
17/*
18 * who
19 */
20
21#include <stdio.h>
22#include <utmp.h>
23#include <pwd.h>
751509f1 24#include <ctype.h>
aba4c79c
BJ
25
26#define NMAX sizeof(utmp.ut_name)
27#define LMAX sizeof(utmp.ut_line)
b899e9ac 28#define HMAX sizeof(utmp.ut_host)
aba4c79c 29
cf288731
BJ
30struct utmp utmp;
31struct passwd *pw;
32struct passwd *getpwuid();
33char hostname[32];
34
35char *ttyname(), *rindex(), *ctime(), *strcpy();
aba4c79c 36
aba4c79c 37main(argc, argv)
cf288731
BJ
38 int argc;
39 char **argv;
aba4c79c
BJ
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];
b899e9ac 49 if (argc == 3) {
aba4c79c
BJ
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());
93b9cc72 55 strncpy(utmp.ut_name, pw ? pw->pw_name : "?", NMAX);
aba4c79c
BJ
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) {
b899e9ac 67 if (argc == 3) {
cf288731 68 gethostname(hostname, sizeof (hostname));
aba4c79c
BJ
69 if (strcmp(utmp.ut_line, tp))
70 continue;
cf288731 71 printf("%s!", hostname);
aba4c79c
BJ
72 putline();
73 exit(0);
74 }
b899e9ac 75 if (utmp.ut_name[0] == '\0' && argc == 1)
aba4c79c
BJ
76 continue;
77 putline();
78 }
79}
80
81putline()
82{
83 register char *cbuf;
84
b899e9ac
SL
85 printf("%-*.*s %-*.*s",
86 NMAX, NMAX, utmp.ut_name,
87 LMAX, LMAX, utmp.ut_line);
aba4c79c 88 cbuf = ctime(&utmp.ut_time);
b899e9ac
SL
89 printf("%.12s", cbuf+4);
90 if (utmp.ut_host[0])
91 printf("\t(%.*s)", HMAX, utmp.ut_host);
92 putchar('\n');
aba4c79c 93}