xref: /csrg-svn/usr.bin/who/who.c (revision 62446)
119910Sdist /*
2*62446Sbostic  * Copyright (c) 1989, 1993
3*62446Sbostic  *	The Regents of the University of California.  All rights reserved.
438370Sbostic  *
538370Sbostic  * This code is derived from software contributed to Berkeley by
638370Sbostic  * Michael Fischbein.
738370Sbostic  *
842787Sbostic  * %sccs.include.redist.c%
919910Sdist  */
1019910Sdist 
1112686Ssam #ifndef lint
12*62446Sbostic static char copyright[] =
13*62446Sbostic "@(#) Copyright (c) 1989, 1993\n\
14*62446Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1538370Sbostic #endif /* not lint */
1619910Sdist 
1719910Sdist #ifndef lint
18*62446Sbostic static char sccsid[] = "@(#)who.c	8.1 (Berkeley) 06/06/93";
1938370Sbostic #endif /* not lint */
2019910Sdist 
2136955Sbostic #include <sys/types.h>
2238370Sbostic #include <sys/file.h>
2338370Sbostic #include <sys/time.h>
2438370Sbostic #include <pwd.h>
251165Sbill #include <utmp.h>
2636955Sbostic #include <stdio.h>
271165Sbill 
main(argc,argv)2830813Skarels main(argc, argv)
2930813Skarels 	int argc;
3030813Skarels 	char **argv;
311165Sbill {
3238370Sbostic 	register char *p;
3338370Sbostic 	struct utmp usr;
3438370Sbostic 	struct passwd *pw;
3538370Sbostic 	FILE *ufp, *file();
3638370Sbostic 	char *t, *rindex(), *strcpy(), *strncpy(), *ttyname();
3738370Sbostic 	time_t time();
381165Sbill 
3938370Sbostic 	switch (argc) {
4038370Sbostic 	case 1:					/* who */
4138370Sbostic 		ufp = file(_PATH_UTMP);
4238370Sbostic 		/* only entries with both name and line fields */
4338370Sbostic 		while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
4438370Sbostic 			if (*usr.ut_name && *usr.ut_line)
4538370Sbostic 				output(&usr);
4638370Sbostic 		break;
4738370Sbostic 	case 2:					/* who utmp_file */
4838370Sbostic 		ufp = file(argv[1]);
4938370Sbostic 		/* all entries */
5038370Sbostic 		while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
5138370Sbostic 			output(&usr);
5238370Sbostic 		break;
5338370Sbostic 	case 3:					/* who am i */
5438370Sbostic 		ufp = file(_PATH_UTMP);
5538370Sbostic 
5638370Sbostic 		/* search through the utmp and find an entry for this tty */
5738370Sbostic 		if (p = ttyname(0)) {
5838370Sbostic 			/* strip any directory component */
5938370Sbostic 			if (t = rindex(p, '/'))
6038370Sbostic 				p = t + 1;
6138370Sbostic 			while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
6238370Sbostic 				if (usr.ut_name && !strcmp(usr.ut_line, p)) {
6338370Sbostic 					output(&usr);
6438370Sbostic 					exit(0);
6538370Sbostic 				}
6638370Sbostic 			/* well, at least we know what the tty is */
6738370Sbostic 			(void)strncpy(usr.ut_line, p, UT_LINESIZE);
6838370Sbostic 		} else
6938370Sbostic 			(void)strcpy(usr.ut_line, "tty??");
7038370Sbostic 		pw = getpwuid(getuid());
7138370Sbostic 		(void)strncpy(usr.ut_name, pw ? pw->pw_name : "?", UT_NAMESIZE);
7238370Sbostic 		(void)time(&usr.ut_time);
7338370Sbostic 		*usr.ut_host = '\0';
7438370Sbostic 		output(&usr);
7538370Sbostic 		break;
7638370Sbostic 	default:
7738370Sbostic 		(void)fprintf(stderr, "usage: who [ file ]\n       who am i\n");
781165Sbill 		exit(1);
791165Sbill 	}
8030813Skarels 	exit(0);
811165Sbill }
821165Sbill 
8338370Sbostic output(up)
8438370Sbostic 	struct utmp *up;
851165Sbill {
8638370Sbostic 	char *ctime();
871165Sbill 
8838370Sbostic 	(void)printf("%-*.*s %-*.*s", UT_NAMESIZE, UT_NAMESIZE, up->ut_name,
8938370Sbostic 	    UT_LINESIZE, UT_LINESIZE, up->ut_line);
9038370Sbostic 	(void)printf("%.12s", ctime(&up->ut_time) + 4);
9138370Sbostic 	if (*up->ut_host)
9238370Sbostic 		printf("\t(%.*s)", UT_HOSTSIZE, up->ut_host);
9338370Sbostic 	(void)putchar('\n');
941165Sbill }
9530813Skarels 
9638370Sbostic FILE *
file(name)9738370Sbostic file(name)
9838370Sbostic 	char *name;
9930813Skarels {
10038370Sbostic 	extern int errno;
10138370Sbostic 	FILE *ufp;
10238370Sbostic 	char *strerror();
10338370Sbostic 
10438370Sbostic 	if (!(ufp = fopen(name, "r"))) {
10538370Sbostic 		(void)fprintf(stderr, "who: %s: %s.\n", name, strerror(errno));
10638370Sbostic 		exit(1);
10738370Sbostic 	}
10838370Sbostic 	return(ufp);
10930813Skarels }
110