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