xref: /csrg-svn/usr.bin/who/who.c (revision 36955)
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*36955Sbostic static char sccsid[] = "@(#)who.c	5.6 (Berkeley) 03/03/89";
1519910Sdist #endif not lint
1619910Sdist 
171165Sbill /*
181165Sbill  * who
191165Sbill  */
201165Sbill 
21*36955Sbostic #include <sys/types.h>
221165Sbill #include <utmp.h>
231165Sbill #include <pwd.h>
24*36955Sbostic #include <stdio.h>
251729Sbill #include <ctype.h>
261165Sbill 
2730813Skarels #define NMAX sizeof(utmp.ut_name)
2830813Skarels #define LMAX sizeof(utmp.ut_line)
2930813Skarels #define	HMAX sizeof(utmp.ut_host)
301165Sbill 
3130813Skarels struct	utmp utmp;
3230813Skarels struct	passwd *pw;
3330813Skarels struct	passwd *getpwuid();
341165Sbill 
3530813Skarels char	*ttyname(), *rindex(), *ctime(), *strcpy();
3630813Skarels 
3730813Skarels main(argc, argv)
3830813Skarels 	int argc;
3930813Skarels 	char **argv;
401165Sbill {
4130813Skarels 	register char *tp, *s;
4230813Skarels 	register FILE *fi;
431165Sbill 
4430813Skarels 	s = "/etc/utmp";
4530813Skarels 	if(argc == 2)
4630813Skarels 		s = argv[1];
4730813Skarels 	if (argc == 3) {
4830813Skarels 		tp = ttyname(0);
4930813Skarels 		if (tp)
5030813Skarels 			tp = rindex(tp, '/') + 1;
5130813Skarels 		else {	/* no tty - use best guess from passwd file */
5234026Sbostic 			(void)strcpy(utmp.ut_line, "tty??");
5330813Skarels 			guess();
5430813Skarels 			exit(0);
5530813Skarels 		}
561165Sbill 	}
5734026Sbostic 	if (!(fi = fopen(s, "r"))) {
5834026Sbostic 		fprintf(stderr, "who: cannot read %s.\n", s);
591165Sbill 		exit(1);
601165Sbill 	}
6130813Skarels 	while (fread((char *)&utmp, sizeof(utmp), 1, fi) == 1) {
6212686Ssam 		if (argc == 3) {
6330813Skarels 			if (strcmp(utmp.ut_line, tp))
6430813Skarels 				continue;
6535429Sbostic 			if (!utmp.ut_name[0])
6635429Sbostic 				guess();
6735429Sbostic 			else
6835429Sbostic 				putline();
6930813Skarels 			exit(0);
7030282Sbostic 		}
7130813Skarels 		if (utmp.ut_name[0] == '\0' && argc == 1)
7230813Skarels 			continue;
7330813Skarels 		putline();
7430813Skarels 	}
7530813Skarels 	if (argc == 3) {
7630813Skarels 		strncpy(utmp.ut_line, tp, sizeof(utmp.ut_line));
7730813Skarels 		guess();
7830813Skarels 	}
7930813Skarels 	exit(0);
801165Sbill }
811165Sbill 
821165Sbill putline()
831165Sbill {
8430813Skarels 	register char *cbuf;
851165Sbill 
8630813Skarels 	printf("%-*.*s %-*.*s",
8730813Skarels 		NMAX, NMAX, utmp.ut_name,
8830813Skarels 		LMAX, LMAX, utmp.ut_line);
8930813Skarels 	cbuf = ctime(&utmp.ut_time);
9030813Skarels 	printf("%.12s", cbuf+4);
9130813Skarels 	if (utmp.ut_host[0])
9230813Skarels 		printf("\t(%.*s)", HMAX, utmp.ut_host);
9312686Ssam 	putchar('\n');
941165Sbill }
9530813Skarels 
9630813Skarels guess()
9730813Skarels {
9830813Skarels 	pw = getpwuid(getuid());
9930813Skarels 	strncpy(utmp.ut_name, pw ? pw->pw_name : "?", NMAX);
10030813Skarels 	time(&utmp.ut_time);
10130813Skarels 	putline();
10230813Skarels }
103