xref: /freebsd/usr.bin/who/who.c (revision 8e6b01171e30297084bb0b4457c4183c2746aacc)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Michael Fischbein.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 static char copyright[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 static char sccsid[] = "@(#)who.c	8.1 (Berkeley) 6/6/93";
45 #endif /* not lint */
46 
47 #include <sys/types.h>
48 #include <sys/file.h>
49 #include <time.h>
50 #include <pwd.h>
51 #include <utmp.h>
52 #include <stdio.h>
53 #include <locale.h>
54 
55 main(argc, argv)
56 	int argc;
57 	char **argv;
58 {
59 	register char *p;
60 	struct utmp usr;
61 	struct passwd *pw;
62 	FILE *ufp, *file();
63 	char *t, *rindex(), *strcpy(), *strncpy(), *ttyname();
64 
65 	(void) setlocale(LC_TIME, "");
66 
67 	switch (argc) {
68 	case 1:					/* who */
69 		ufp = file(_PATH_UTMP);
70 		/* only entries with both name and line fields */
71 		while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
72 			if (*usr.ut_name && *usr.ut_line)
73 				output(&usr);
74 		break;
75 	case 2:					/* who utmp_file */
76 		ufp = file(argv[1]);
77 		/* all entries */
78 		while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
79 			output(&usr);
80 		break;
81 	case 3:					/* who am i */
82 		ufp = file(_PATH_UTMP);
83 
84 		/* search through the utmp and find an entry for this tty */
85 		if (p = ttyname(0)) {
86 			/* strip any directory component */
87 			if (t = rindex(p, '/'))
88 				p = t + 1;
89 			while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
90 				if (usr.ut_name && !strcmp(usr.ut_line, p)) {
91 					output(&usr);
92 					exit(0);
93 				}
94 			/* well, at least we know what the tty is */
95 			(void)strncpy(usr.ut_line, p, UT_LINESIZE);
96 		} else
97 			(void)strcpy(usr.ut_line, "tty??");
98 		pw = getpwuid(getuid());
99 		(void)strncpy(usr.ut_name, pw ? pw->pw_name : "?", UT_NAMESIZE);
100 		(void)time(&usr.ut_time);
101 		*usr.ut_host = '\0';
102 		output(&usr);
103 		break;
104 	default:
105 		(void)fprintf(stderr, "usage: who [ file ]\n       who am i\n");
106 		exit(1);
107 	}
108 	exit(0);
109 }
110 
111 output(up)
112 	struct utmp *up;
113 {
114 	char buf[80];
115 
116 	(void)printf("%-*.*s %-*.*s", UT_NAMESIZE, UT_NAMESIZE, up->ut_name,
117 	    UT_LINESIZE, UT_LINESIZE, up->ut_line);
118 	(void)strftime(buf, sizeof(buf), "%c", localtime(&up->ut_time));
119 	(void)printf("%.12s", buf + 4);
120 	if (*up->ut_host)
121 		printf("\t(%.*s)", UT_HOSTSIZE, up->ut_host);
122 	(void)putchar('\n');
123 }
124 
125 FILE *
126 file(name)
127 	char *name;
128 {
129 	extern int errno;
130 	FILE *ufp;
131 	char *strerror();
132 
133 	if (!(ufp = fopen(name, "r"))) {
134 		(void)fprintf(stderr, "who: %s: %s.\n", name, strerror(errno));
135 		exit(1);
136 	}
137 	return(ufp);
138 }
139