xref: /csrg-svn/usr.bin/who/who.c (revision 30282)
119910Sdist /*
219910Sdist  * Copyright (c) 1980 Regents of the University of California.
319910Sdist  * All rights reserved.  The Berkeley software License Agreement
419910Sdist  * specifies the terms and conditions for redistribution.
519910Sdist  */
619910Sdist 
712686Ssam #ifndef lint
819910Sdist char copyright[] =
919910Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1019910Sdist  All rights reserved.\n";
1119910Sdist #endif not lint
1219910Sdist 
1319910Sdist #ifndef lint
14*30282Sbostic static char sccsid[] = "@(#)who.c	5.2 (Berkeley) 12/10/86";
1519910Sdist #endif not lint
1619910Sdist 
171165Sbill /*
181165Sbill  * who
191165Sbill  */
201165Sbill 
21*30282Sbostic #include <sys/param.h>
221165Sbill #include <utmp.h>
231165Sbill #include <pwd.h>
24*30282Sbostic #include <stdio.h>
25*30282Sbostic #include <strings.h>
261729Sbill #include <ctype.h>
271165Sbill 
28*30282Sbostic #define NMAX	sizeof(utmp.ut_name)
29*30282Sbostic #define LMAX	sizeof(utmp.ut_line)
30*30282Sbostic #define HMAX	sizeof(utmp.ut_host)
311165Sbill 
32*30282Sbostic static struct utmp	utmp;		/* read buffer */
331165Sbill 
34*30282Sbostic main(argc,argv)
35*30282Sbostic int	argc;
36*30282Sbostic char	**argv;
371165Sbill {
38*30282Sbostic 	register FILE	*fp;			/* utmp file pointer */
39*30282Sbostic 	register char	*tp,			/* tty name */
40*30282Sbostic 			*fname;			/* utmp file name */
41*30282Sbostic 	struct passwd	*pw,			/* user passwd structure */
42*30282Sbostic 			*getpwuid();
43*30282Sbostic 	char	hostname[MAXHOSTNAMELEN],	/* host name */
44*30282Sbostic 		*ttyname();
45*30282Sbostic 	uid_t	getuid();
46*30282Sbostic 	long	time();
471165Sbill 
48*30282Sbostic 	switch(argc) {
49*30282Sbostic 		case 2:
50*30282Sbostic 			fname = argv[1];
51*30282Sbostic 			break;
52*30282Sbostic 		case 3:
53*30282Sbostic 			if (!(tp = ttyname(0))) {
54*30282Sbostic 				/*
55*30282Sbostic 				 * no tty -- use best guess from passwd file.
56*30282Sbostic 				 * next line is a kludge, but as of now getuid
57*30282Sbostic 				 * returns a "uid_t" and getpwuid takes an int.
58*30282Sbostic 				 */
59*30282Sbostic 				pw = getpwuid((int)getuid());
60*30282Sbostic 				strncpy(utmp.ut_name,pw ? pw->pw_name : "?",NMAX);
61*30282Sbostic 				strcpy(utmp.ut_line,"tty??");
62*30282Sbostic 				time(&utmp.ut_time);
63*30282Sbostic 				putline();
64*30282Sbostic 				exit(0);
65*30282Sbostic 			}
66*30282Sbostic 			tp = rindex(tp,'/') + 1;
67*30282Sbostic 			if (gethostname(hostname,sizeof(hostname)) == -1) {
68*30282Sbostic 				perror("gethostname");
69*30282Sbostic 				exit(1);
70*30282Sbostic 			}
71*30282Sbostic 		case 1:
72*30282Sbostic 			fname = "/etc/utmp";
73*30282Sbostic 			break;
74*30282Sbostic 		default:
75*30282Sbostic 			fputs("usage: who [ utmp_file ]\nor who am i\n",stderr);
76*30282Sbostic 			exit(1);
771165Sbill 	}
78*30282Sbostic 	if (!(fp = fopen(fname,"r"))) {
79*30282Sbostic 		perror(fname);
801165Sbill 		exit(1);
811165Sbill 	}
82*30282Sbostic 	while (fread((char *)&utmp,sizeof(utmp),1,fp) == 1)
8312686Ssam 		if (argc == 3) {
84*30282Sbostic 			if (!strcmp(utmp.ut_line,tp)) {
85*30282Sbostic 				printf("%s!",hostname);
86*30282Sbostic 				putline();
87*30282Sbostic 				exit(0);
88*30282Sbostic 			}
89*30282Sbostic 		}
90*30282Sbostic 		else if (argc != 1 || *utmp.ut_name)
911165Sbill 			putline();
921165Sbill }
931165Sbill 
941165Sbill putline()
951165Sbill {
96*30282Sbostic 	register char	*cbuf;
97*30282Sbostic 	char	*ctime();
981165Sbill 
99*30282Sbostic 	cbuf = ctime(&utmp.ut_time) + 4;
100*30282Sbostic 	printf("%-*.*s %-*.*s%.12s",NMAX,NMAX,utmp.ut_name,LMAX,LMAX,utmp.ut_line,cbuf);
101*30282Sbostic 	if (*utmp.ut_host)
102*30282Sbostic 		printf("\t(%.*s)",HMAX,utmp.ut_host);
10312686Ssam 	putchar('\n');
1041165Sbill }
105