xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/finger.c (revision 2923:da82ab368162)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2923Sraf  * Common Development and Distribution License (the "License").
6*2923Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*2923Sraf 
220Sstevel@tonic-gate /*
23*2923Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * This is a finger program.  It prints out useful information about users
440Sstevel@tonic-gate  * by digging it up from various system files.
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * There are three output formats, all of which give login name, teletype
470Sstevel@tonic-gate  * line number, and login time.  The short output format is reminiscent
480Sstevel@tonic-gate  * of finger on ITS, and gives one line of information per user containing
490Sstevel@tonic-gate  * in addition to the minimum basic requirements (MBR), the user's full name,
500Sstevel@tonic-gate  * idle time and location.
510Sstevel@tonic-gate  * The quick style output is UNIX who-like, giving only name, teletype and
520Sstevel@tonic-gate  * login time.  Finally, the long style output give the same information
530Sstevel@tonic-gate  * as the short (in more legible format), the home directory and shell
540Sstevel@tonic-gate  * of the user, and, if it exits, a copy of the file .plan in the users
550Sstevel@tonic-gate  * home directory.  Finger may be called with or without a list of people
560Sstevel@tonic-gate  * to finger -- if no list is given, all the people currently logged in
570Sstevel@tonic-gate  * are fingered.
580Sstevel@tonic-gate  *
590Sstevel@tonic-gate  * The program is validly called by one of the following:
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  *	finger			{short form list of users}
620Sstevel@tonic-gate  *	finger -l		{long form list of users}
630Sstevel@tonic-gate  *	finger -b		{briefer long form list of users}
640Sstevel@tonic-gate  *	finger -q		{quick list of users}
650Sstevel@tonic-gate  *	finger -i		{quick list of users with idle times}
660Sstevel@tonic-gate  *	finger -m		{matches arguments against only username}
670Sstevel@tonic-gate  *	finger -f		{suppress header in non-long form}
680Sstevel@tonic-gate  *	finger -p		{suppress printing of .plan file}
690Sstevel@tonic-gate  *	finger -h		{suppress printing of .project file}
700Sstevel@tonic-gate  *	finger -i		{forces "idle" output format}
710Sstevel@tonic-gate  *	finger namelist		{long format list of specified users}
720Sstevel@tonic-gate  *	finger -s namelist	{short format list of specified users}
730Sstevel@tonic-gate  *	finger -w namelist	{narrow short format list of specified users}
740Sstevel@tonic-gate  *
750Sstevel@tonic-gate  * where 'namelist' is a list of users login names.
760Sstevel@tonic-gate  * The other options can all be given after one '-', or each can have its
770Sstevel@tonic-gate  * own '-'.  The -f option disables the printing of headers for short and
780Sstevel@tonic-gate  * quick outputs.  The -b option briefens long format outputs.  The -p
790Sstevel@tonic-gate  * option turns off plans for long format outputs.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate 
820Sstevel@tonic-gate #include <sys/types.h>
830Sstevel@tonic-gate #include <sys/stat.h>
840Sstevel@tonic-gate #include <utmpx.h>
850Sstevel@tonic-gate #include <sys/signal.h>
860Sstevel@tonic-gate #include <pwd.h>
870Sstevel@tonic-gate #include <stdio.h>
880Sstevel@tonic-gate #include <lastlog.h>
890Sstevel@tonic-gate #include <ctype.h>
900Sstevel@tonic-gate #include <sys/time.h>
910Sstevel@tonic-gate #include <time.h>
920Sstevel@tonic-gate #include <sys/socket.h>
930Sstevel@tonic-gate #include <netinet/in.h>
940Sstevel@tonic-gate #include <netdb.h>
950Sstevel@tonic-gate #include <locale.h>
960Sstevel@tonic-gate #include <sys/select.h>
970Sstevel@tonic-gate #include <stdlib.h>
980Sstevel@tonic-gate #include <strings.h>
990Sstevel@tonic-gate #include <fcntl.h>
1000Sstevel@tonic-gate #include <curses.h>
1010Sstevel@tonic-gate #include <unctrl.h>
1020Sstevel@tonic-gate #include <maillock.h>
1030Sstevel@tonic-gate #include <deflt.h>
1040Sstevel@tonic-gate #include <unistd.h>
1050Sstevel@tonic-gate #include <arpa/inet.h>
1060Sstevel@tonic-gate #include <macros.h>
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static char gecos_ignore_c = '*';	/* ignore this in real name */
1090Sstevel@tonic-gate static char gecos_sep_c = ',';		/* separator in pw_gecos field */
1100Sstevel@tonic-gate static char gecos_samename = '&';	/* repeat login name in real name */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate #define	TALKABLE	0220		/* tty is writable if this mode */
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate #define	NMAX	sizeof (((struct utmpx *)0)->ut_name)
1150Sstevel@tonic-gate #define	LMAX	sizeof (((struct utmpx *)0)->ut_line)
1160Sstevel@tonic-gate #define	HMAX	sizeof (((struct utmpx *)0)->ut_host)
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate struct person {				/* one for each person fingered */
1190Sstevel@tonic-gate 	char *name;			/* name */
1200Sstevel@tonic-gate 	char tty[LMAX+1];		/* null terminated tty line */
1210Sstevel@tonic-gate 	char host[HMAX+1];		/* null terminated remote host name */
1220Sstevel@tonic-gate 	char *ttyloc;			/* location of tty line, if any */
1230Sstevel@tonic-gate 	time_t loginat;			/* time of (last) login */
1240Sstevel@tonic-gate 	time_t idletime;		/* how long idle (if logged in) */
1250Sstevel@tonic-gate 	char *realname;			/* pointer to full name */
1260Sstevel@tonic-gate 	struct passwd *pwd;		/* structure of /etc/passwd stuff */
1270Sstevel@tonic-gate 	char loggedin;			/* person is logged in */
1280Sstevel@tonic-gate 	char writable;			/* tty is writable */
1290Sstevel@tonic-gate 	char original;			/* this is not a duplicate entry */
1300Sstevel@tonic-gate 	struct person *link;		/* link to next person */
1310Sstevel@tonic-gate };
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate char LASTLOG[] = "/var/adm/lastlog";	/* last login info */
1340Sstevel@tonic-gate char PLAN[] = "/.plan";			/* what plan file is */
1350Sstevel@tonic-gate char PROJ[] = "/.project";		/* what project file */
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate int unbrief = 1;			/* -b option default */
1380Sstevel@tonic-gate int header = 1;				/* -f option default */
1390Sstevel@tonic-gate int hack = 1;				/* -h option default */
1400Sstevel@tonic-gate int idle = 0;				/* -i option default */
1410Sstevel@tonic-gate int large = 0;				/* -l option default */
1420Sstevel@tonic-gate int match = 1;				/* -m option default */
1430Sstevel@tonic-gate int plan = 1;				/* -p option default */
1440Sstevel@tonic-gate int unquick = 1;			/* -q option default */
1450Sstevel@tonic-gate int small = 0;				/* -s option default */
1460Sstevel@tonic-gate int wide = 1;				/* -w option default */
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate  * RFC 1288 says that system administrators should have the option of
1500Sstevel@tonic-gate  * separately allowing ASCII characters less than 32 or greater than
1510Sstevel@tonic-gate  * 126.  The termpass variable keeps track of this.
1520Sstevel@tonic-gate  */
1530Sstevel@tonic-gate char	defaultfile[] = "/etc/default/finger";
1540Sstevel@tonic-gate char	passvar[] = "PASS=";
1550Sstevel@tonic-gate int	termpass = 0;			/* default is ASCII only */
1560Sstevel@tonic-gate char *termopts[] = {
1570Sstevel@tonic-gate #define	TERM_LOW	0
1580Sstevel@tonic-gate 	"low",
1590Sstevel@tonic-gate #define	TERM_HIGH	1
1600Sstevel@tonic-gate 	"high",
1610Sstevel@tonic-gate 	(char *)NULL
1620Sstevel@tonic-gate };
1630Sstevel@tonic-gate #define	TS_LOW	(1 << TERM_LOW)		/* print characters less than 32 */
1640Sstevel@tonic-gate #define	TS_HIGH	(1 << TERM_HIGH)	/* print characters greater than 126 */
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate int unshort;
1680Sstevel@tonic-gate FILE *lf;				/* LASTLOG file pointer */
1690Sstevel@tonic-gate struct person *person1;			/* list of people */
1700Sstevel@tonic-gate time_t tloc;				/* current time */
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate char usagestr[] = "Usage: "
1730Sstevel@tonic-gate 	"finger [-bfhilmpqsw] [name1 [name2 ...] ]\n";
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate int AlreadyPrinted(uid_t uid);
1760Sstevel@tonic-gate void AnyMail(char *name);
1770Sstevel@tonic-gate void catfile(char *s, mode_t mode, int trunc_at_nl);
1780Sstevel@tonic-gate void decode(struct person *pers);
1790Sstevel@tonic-gate void doall(void);
1800Sstevel@tonic-gate void donames(char **argv);
1810Sstevel@tonic-gate void findidle(struct person *pers);
1820Sstevel@tonic-gate void findwhen(struct person *pers);
1830Sstevel@tonic-gate void fwclose(void);
1840Sstevel@tonic-gate void fwopen(void);
1850Sstevel@tonic-gate void initscreening(void);
1860Sstevel@tonic-gate void ltimeprint(char *before, time_t *dt, char *after);
1870Sstevel@tonic-gate int matchcmp(char *gname, char *login, char *given);
1880Sstevel@tonic-gate int namecmp(char *name1, char *name2);
1890Sstevel@tonic-gate int netfinger(char *name);
1900Sstevel@tonic-gate void personprint(struct person *pers);
1910Sstevel@tonic-gate void print(void);
1920Sstevel@tonic-gate struct passwd *pwdcopy(const struct passwd *pfrom);
1930Sstevel@tonic-gate void quickprint(struct person *pers);
1940Sstevel@tonic-gate void shortprint(struct person *pers);
1950Sstevel@tonic-gate void stimeprint(time_t *dt);
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate int
1990Sstevel@tonic-gate main(int argc, char **argv)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate 	int c;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2040Sstevel@tonic-gate 	/* parse command line for (optional) arguments */
2050Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "bfhilmpqsw")) != EOF)
2060Sstevel@tonic-gate 			switch (c) {
2070Sstevel@tonic-gate 			case 'b':
2080Sstevel@tonic-gate 				unbrief = 0;
2090Sstevel@tonic-gate 				break;
2100Sstevel@tonic-gate 			case 'f':
2110Sstevel@tonic-gate 				header = 0;
2120Sstevel@tonic-gate 				break;
2130Sstevel@tonic-gate 			case 'h':
2140Sstevel@tonic-gate 				hack = 0;
2150Sstevel@tonic-gate 				break;
2160Sstevel@tonic-gate 			case 'i':
2170Sstevel@tonic-gate 				idle = 1;
2180Sstevel@tonic-gate 				unquick = 0;
2190Sstevel@tonic-gate 				break;
2200Sstevel@tonic-gate 			case 'l':
2210Sstevel@tonic-gate 				large = 1;
2220Sstevel@tonic-gate 				break;
2230Sstevel@tonic-gate 			case 'm':
2240Sstevel@tonic-gate 				match = 0;
2250Sstevel@tonic-gate 				break;
2260Sstevel@tonic-gate 			case 'p':
2270Sstevel@tonic-gate 				plan = 0;
2280Sstevel@tonic-gate 				break;
2290Sstevel@tonic-gate 			case 'q':
2300Sstevel@tonic-gate 				unquick = 0;
2310Sstevel@tonic-gate 				break;
2320Sstevel@tonic-gate 			case 's':
2330Sstevel@tonic-gate 				small = 1;
2340Sstevel@tonic-gate 				break;
2350Sstevel@tonic-gate 			case 'w':
2360Sstevel@tonic-gate 				wide = 0;
2370Sstevel@tonic-gate 				break;
2380Sstevel@tonic-gate 			default:
2390Sstevel@tonic-gate 				(void) fprintf(stderr, usagestr);
2400Sstevel@tonic-gate 				exit(1);
2410Sstevel@tonic-gate 			}
2420Sstevel@tonic-gate 	if (unquick || idle)
2430Sstevel@tonic-gate 		tloc = time(NULL);
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	/* find out what filtering on .plan/.project files we should do */
2460Sstevel@tonic-gate 	initscreening();
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	/*
2490Sstevel@tonic-gate 	 * optind == argc means no names given
2500Sstevel@tonic-gate 	 */
2510Sstevel@tonic-gate 	if (optind == argc)
2520Sstevel@tonic-gate 		doall();
2530Sstevel@tonic-gate 	else
2540Sstevel@tonic-gate 		donames(&argv[optind]);
2550Sstevel@tonic-gate 	if (person1)
2560Sstevel@tonic-gate 		print();
2570Sstevel@tonic-gate 	return (0);
2580Sstevel@tonic-gate 	/* NOTREACHED */
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate void
2620Sstevel@tonic-gate doall(void)
2630Sstevel@tonic-gate {
2640Sstevel@tonic-gate 	struct person *p;
2650Sstevel@tonic-gate 	struct passwd *pw;
2660Sstevel@tonic-gate 	struct utmpx *u;
2670Sstevel@tonic-gate 	char name[NMAX + 1];
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	unshort = large;
2700Sstevel@tonic-gate 	setutxent();
2710Sstevel@tonic-gate 	if (unquick) {
2720Sstevel@tonic-gate 		setpwent();
2730Sstevel@tonic-gate 		fwopen();
2740Sstevel@tonic-gate 	}
2750Sstevel@tonic-gate 	while (u = getutxent()) {
2760Sstevel@tonic-gate 		if (u->ut_name[0] == 0 ||
2770Sstevel@tonic-gate 		    nonuserx(*u) ||
2780Sstevel@tonic-gate 		    u->ut_type != USER_PROCESS)
2790Sstevel@tonic-gate 			continue;
2800Sstevel@tonic-gate 		if (person1 == 0)
2810Sstevel@tonic-gate 			p = person1 = malloc(sizeof (*p));
2820Sstevel@tonic-gate 		else {
2830Sstevel@tonic-gate 			p->link = malloc(sizeof (*p));
2840Sstevel@tonic-gate 			p = p->link;
2850Sstevel@tonic-gate 		}
2860Sstevel@tonic-gate 		bcopy(u->ut_name, name, NMAX);
2870Sstevel@tonic-gate 		name[NMAX] = 0;
2880Sstevel@tonic-gate 		bcopy(u->ut_line, p->tty, LMAX);
2890Sstevel@tonic-gate 		p->tty[LMAX] = 0;
2900Sstevel@tonic-gate 		bcopy(u->ut_host, p->host, HMAX);
2910Sstevel@tonic-gate 		p->host[HMAX] = 0;
2920Sstevel@tonic-gate 		p->loginat = u->ut_tv.tv_sec;
2930Sstevel@tonic-gate 		p->pwd = 0;
2940Sstevel@tonic-gate 		p->loggedin = 1;
2950Sstevel@tonic-gate 		if (unquick && (pw = getpwnam(name))) {
2960Sstevel@tonic-gate 			p->pwd = pwdcopy(pw);
2970Sstevel@tonic-gate 			decode(p);
2980Sstevel@tonic-gate 			p->name = p->pwd->pw_name;
2990Sstevel@tonic-gate 		} else
3000Sstevel@tonic-gate 			p->name = strdup(name);
3010Sstevel@tonic-gate 		p->ttyloc = NULL;
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 	if (unquick) {
3040Sstevel@tonic-gate 		fwclose();
3050Sstevel@tonic-gate 		endpwent();
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 	endutxent();
3080Sstevel@tonic-gate 	if (person1 == 0) {
3090Sstevel@tonic-gate 		(void) printf("No one logged on\n");
3100Sstevel@tonic-gate 		return;
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 	p->link = 0;
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate void
3160Sstevel@tonic-gate donames(char **argv)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate 	struct person	*p;
3190Sstevel@tonic-gate 	struct passwd	*pw;
3200Sstevel@tonic-gate 	struct utmpx	*u;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	/*
3230Sstevel@tonic-gate 	 * get names from command line and check to see if they're
3240Sstevel@tonic-gate 	 * logged in
3250Sstevel@tonic-gate 	 */
3260Sstevel@tonic-gate 	unshort = !small;
3270Sstevel@tonic-gate 	for (; *argv != 0; argv++) {
3280Sstevel@tonic-gate 		if (netfinger(*argv))
3290Sstevel@tonic-gate 			continue;
3300Sstevel@tonic-gate 		if (person1 == 0)
3310Sstevel@tonic-gate 			p = person1 = malloc(sizeof (*p));
3320Sstevel@tonic-gate 		else {
3330Sstevel@tonic-gate 			p->link = malloc(sizeof (*p));
3340Sstevel@tonic-gate 			p = p->link;
3350Sstevel@tonic-gate 		}
3360Sstevel@tonic-gate 		p->name = *argv;
3370Sstevel@tonic-gate 		p->loggedin = 0;
3380Sstevel@tonic-gate 		p->original = 1;
3390Sstevel@tonic-gate 		p->pwd = 0;
3400Sstevel@tonic-gate 	}
3410Sstevel@tonic-gate 	if (person1 == 0)
3420Sstevel@tonic-gate 		return;
3430Sstevel@tonic-gate 	p->link = 0;
3440Sstevel@tonic-gate 	/*
3450Sstevel@tonic-gate 	 * if we are doing it, read /etc/passwd for the useful info
3460Sstevel@tonic-gate 	 */
3470Sstevel@tonic-gate 	if (unquick) {
3480Sstevel@tonic-gate 		setpwent();
3490Sstevel@tonic-gate 		if (!match) {
3500Sstevel@tonic-gate 			for (p = person1; p != 0; p = p->link) {
3510Sstevel@tonic-gate 				if (pw = getpwnam(p->name))
3520Sstevel@tonic-gate 					p->pwd = pwdcopy(pw);
3530Sstevel@tonic-gate 			}
3540Sstevel@tonic-gate 		} else {
3550Sstevel@tonic-gate 			while ((pw = getpwent()) != 0) {
3560Sstevel@tonic-gate 				for (p = person1; p != 0; p = p->link) {
3570Sstevel@tonic-gate 					if (!p->original)
3580Sstevel@tonic-gate 						continue;
3590Sstevel@tonic-gate 					if (strcmp(p->name, pw->pw_name) != 0 &&
3600Sstevel@tonic-gate 					    !matchcmp(pw->pw_gecos, pw->pw_name,
3610Sstevel@tonic-gate 					    p->name)) {
3620Sstevel@tonic-gate 						continue;
3630Sstevel@tonic-gate 					}
3640Sstevel@tonic-gate 					if (p->pwd == 0) {
3650Sstevel@tonic-gate 						p->pwd = pwdcopy(pw);
3660Sstevel@tonic-gate 					} else {
3670Sstevel@tonic-gate 						struct person *new;
3680Sstevel@tonic-gate 						/*
3690Sstevel@tonic-gate 						 * Handle multiple login names.
3700Sstevel@tonic-gate 						 * Insert new "duplicate" entry
3710Sstevel@tonic-gate 						 * behind.
3720Sstevel@tonic-gate 						 */
3730Sstevel@tonic-gate 						new = malloc(sizeof (*new));
3740Sstevel@tonic-gate 						new->pwd = pwdcopy(pw);
3750Sstevel@tonic-gate 						new->name = p->name;
3760Sstevel@tonic-gate 						new->original = 1;
3770Sstevel@tonic-gate 						new->loggedin = 0;
3780Sstevel@tonic-gate 						new->ttyloc = NULL;
3790Sstevel@tonic-gate 						new->link = p->link;
3800Sstevel@tonic-gate 						p->original = 0;
3810Sstevel@tonic-gate 						p->link = new;
3820Sstevel@tonic-gate 						p = new;
3830Sstevel@tonic-gate 					}
3840Sstevel@tonic-gate 				}
3850Sstevel@tonic-gate 			}
3860Sstevel@tonic-gate 		}
3870Sstevel@tonic-gate 		endpwent();
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 	/* Now get login information */
3900Sstevel@tonic-gate 	setutxent();
3910Sstevel@tonic-gate 	while (u = getutxent()) {
3920Sstevel@tonic-gate 		if (u->ut_name[0] == 0 || u->ut_type != USER_PROCESS)
3930Sstevel@tonic-gate 			continue;
3940Sstevel@tonic-gate 		for (p = person1; p != 0; p = p->link) {
3950Sstevel@tonic-gate 			p->ttyloc = NULL;
3960Sstevel@tonic-gate 			if (p->loggedin == 2)
3970Sstevel@tonic-gate 				continue;
3980Sstevel@tonic-gate 			if (strncmp(p->pwd ? p->pwd->pw_name : p->name,
3990Sstevel@tonic-gate 			    u->ut_name, NMAX) != 0)
4000Sstevel@tonic-gate 				continue;
4010Sstevel@tonic-gate 			if (p->loggedin == 0) {
4020Sstevel@tonic-gate 				bcopy(u->ut_line, p->tty, LMAX);
4030Sstevel@tonic-gate 				p->tty[LMAX] = 0;
4040Sstevel@tonic-gate 				bcopy(u->ut_host, p->host, HMAX);
4050Sstevel@tonic-gate 				p->host[HMAX] = 0;
4060Sstevel@tonic-gate 				p->loginat = u->ut_tv.tv_sec;
4070Sstevel@tonic-gate 				p->loggedin = 1;
4080Sstevel@tonic-gate 			} else {	/* p->loggedin == 1 */
4090Sstevel@tonic-gate 				struct person *new;
4100Sstevel@tonic-gate 				new = malloc(sizeof (*new));
4110Sstevel@tonic-gate 				new->name = p->name;
4120Sstevel@tonic-gate 				bcopy(u->ut_line, new->tty, LMAX);
4130Sstevel@tonic-gate 				new->tty[LMAX] = 0;
4140Sstevel@tonic-gate 				bcopy(u->ut_host, new->host, HMAX);
4150Sstevel@tonic-gate 				new->host[HMAX] = 0;
4160Sstevel@tonic-gate 				new->loginat = u->ut_tv.tv_sec;
4170Sstevel@tonic-gate 				new->pwd = p->pwd;
4180Sstevel@tonic-gate 				new->loggedin = 1;
4190Sstevel@tonic-gate 				new->original = 0;
4200Sstevel@tonic-gate 				new->link = p->link;
4210Sstevel@tonic-gate 				p->loggedin = 2;
4220Sstevel@tonic-gate 				p->link = new;
4230Sstevel@tonic-gate 				p = new;
4240Sstevel@tonic-gate 			}
4250Sstevel@tonic-gate 		}
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 	endutxent();
4280Sstevel@tonic-gate 	if (unquick) {
4290Sstevel@tonic-gate 		fwopen();
4300Sstevel@tonic-gate 		for (p = person1; p != 0; p = p->link)
4310Sstevel@tonic-gate 			decode(p);
4320Sstevel@tonic-gate 		fwclose();
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate void
4370Sstevel@tonic-gate print(void)
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate 	struct person *p;
4400Sstevel@tonic-gate 	char *s;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	/*
4430Sstevel@tonic-gate 	 * print out what we got
4440Sstevel@tonic-gate 	 */
4450Sstevel@tonic-gate 	if (header) {
4460Sstevel@tonic-gate 		if (unquick) {
4470Sstevel@tonic-gate 			if (!unshort) {
4480Sstevel@tonic-gate 				if (wide) {
4490Sstevel@tonic-gate 					(void) printf("Login       "
4500Sstevel@tonic-gate 					    "Name               TTY         "
4510Sstevel@tonic-gate 					    "Idle    When    Where\n");
4520Sstevel@tonic-gate 				} else {
4530Sstevel@tonic-gate 					(void) printf("Login    TTY Idle    "
4540Sstevel@tonic-gate 					    "When    Where\n");
4550Sstevel@tonic-gate 				}
4560Sstevel@tonic-gate 			}
4570Sstevel@tonic-gate 		} else {
4580Sstevel@tonic-gate 			(void) printf("Login      TTY                When");
4590Sstevel@tonic-gate 			if (idle)
4600Sstevel@tonic-gate 				(void) printf("             Idle");
4610Sstevel@tonic-gate 			(void) putchar('\n');
4620Sstevel@tonic-gate 		}
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 	for (p = person1; p != 0; p = p->link) {
4650Sstevel@tonic-gate 		if (!unquick) {
4660Sstevel@tonic-gate 			quickprint(p);
4670Sstevel@tonic-gate 			continue;
4680Sstevel@tonic-gate 		}
4690Sstevel@tonic-gate 		if (!unshort) {
4700Sstevel@tonic-gate 			shortprint(p);
4710Sstevel@tonic-gate 			continue;
4720Sstevel@tonic-gate 		}
4730Sstevel@tonic-gate 		personprint(p);
4740Sstevel@tonic-gate 		if (p->pwd != 0 && !AlreadyPrinted(p->pwd->pw_uid)) {
4750Sstevel@tonic-gate 			AnyMail(p->pwd->pw_name);
4760Sstevel@tonic-gate 			if (hack) {
4770Sstevel@tonic-gate 				struct stat sbuf;
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 				s = malloc(strlen(p->pwd->pw_dir) +
4800Sstevel@tonic-gate 					sizeof (PROJ));
4810Sstevel@tonic-gate 				if (s) {
4820Sstevel@tonic-gate 					(void) strcpy(s, p->pwd->pw_dir);
4830Sstevel@tonic-gate 					(void) strcat(s, PROJ);
4840Sstevel@tonic-gate 					if (stat(s, &sbuf) != -1 &&
4850Sstevel@tonic-gate 					    (S_ISREG(sbuf.st_mode) ||
4860Sstevel@tonic-gate 					    S_ISFIFO(sbuf.st_mode)) &&
4870Sstevel@tonic-gate 					    (sbuf.st_mode & S_IROTH)) {
4880Sstevel@tonic-gate 						(void) printf("Project: ");
4890Sstevel@tonic-gate 						catfile(s, sbuf.st_mode, 1);
4900Sstevel@tonic-gate 						(void) putchar('\n');
4910Sstevel@tonic-gate 					}
4920Sstevel@tonic-gate 					free(s);
4930Sstevel@tonic-gate 				}
4940Sstevel@tonic-gate 			}
4950Sstevel@tonic-gate 			if (plan) {
4960Sstevel@tonic-gate 				struct stat sbuf;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 				s = malloc(strlen(p->pwd->pw_dir) +
4990Sstevel@tonic-gate 					sizeof (PLAN));
5000Sstevel@tonic-gate 				if (s) {
5010Sstevel@tonic-gate 					(void) strcpy(s, p->pwd->pw_dir);
5020Sstevel@tonic-gate 					(void) strcat(s, PLAN);
5030Sstevel@tonic-gate 					if (stat(s, &sbuf) == -1 ||
5040Sstevel@tonic-gate 					    (!S_ISREG(sbuf.st_mode) &&
5050Sstevel@tonic-gate 					    !S_ISFIFO(sbuf.st_mode)) ||
5060Sstevel@tonic-gate 					    ((sbuf.st_mode & S_IROTH) == 0))
5070Sstevel@tonic-gate 						(void) printf("No Plan.\n");
5080Sstevel@tonic-gate 					else {
5090Sstevel@tonic-gate 						(void) printf("Plan:\n");
5100Sstevel@tonic-gate 						catfile(s, sbuf.st_mode, 0);
5110Sstevel@tonic-gate 					}
5120Sstevel@tonic-gate 					free(s);
5130Sstevel@tonic-gate 				}
5140Sstevel@tonic-gate 			}
5150Sstevel@tonic-gate 		}
5160Sstevel@tonic-gate 		if (p->link != 0)
5170Sstevel@tonic-gate 			(void) putchar('\n');
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate }
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate /*
5220Sstevel@tonic-gate  * Duplicate a pwd entry.
5230Sstevel@tonic-gate  * Note: Only the useful things (what the program currently uses) are copied.
5240Sstevel@tonic-gate  */
5250Sstevel@tonic-gate struct passwd *
5260Sstevel@tonic-gate pwdcopy(const struct passwd *pfrom)
5270Sstevel@tonic-gate {
5280Sstevel@tonic-gate 	struct passwd *pto;
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	pto = malloc(sizeof (*pto));
5310Sstevel@tonic-gate 	pto->pw_name = strdup(pfrom->pw_name);
5320Sstevel@tonic-gate 	pto->pw_uid = pfrom->pw_uid;
5330Sstevel@tonic-gate 	pto->pw_gecos = strdup(pfrom->pw_gecos);
5340Sstevel@tonic-gate 	pto->pw_dir = strdup(pfrom->pw_dir);
5350Sstevel@tonic-gate 	pto->pw_shell = strdup(pfrom->pw_shell);
5360Sstevel@tonic-gate 	return (pto);
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate /*
5400Sstevel@tonic-gate  * print out information on quick format giving just name, tty, login time
5410Sstevel@tonic-gate  * and idle time if idle is set.
5420Sstevel@tonic-gate  */
5430Sstevel@tonic-gate void
5440Sstevel@tonic-gate quickprint(struct person *pers)
5450Sstevel@tonic-gate {
5460Sstevel@tonic-gate 	(void) printf("%-8.8s  ", pers->name);
5470Sstevel@tonic-gate 	if (pers->loggedin) {
5480Sstevel@tonic-gate 		if (idle) {
5490Sstevel@tonic-gate 			findidle(pers);
5500Sstevel@tonic-gate 			(void) printf("%c%-12s %-16.16s",
5510Sstevel@tonic-gate 				pers->writable ? ' ' : '*',
5520Sstevel@tonic-gate 				pers->tty, ctime(&pers->loginat));
5530Sstevel@tonic-gate 			ltimeprint("   ", &pers->idletime, "");
5540Sstevel@tonic-gate 		} else {
5550Sstevel@tonic-gate 			(void) printf(" %-12s %-16.16s",
5560Sstevel@tonic-gate 				pers->tty, ctime(&pers->loginat));
5570Sstevel@tonic-gate 		}
5580Sstevel@tonic-gate 		(void) putchar('\n');
5590Sstevel@tonic-gate 	} else {
5600Sstevel@tonic-gate 		(void) printf("          Not Logged In\n");
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate /*
5650Sstevel@tonic-gate  * print out information in short format, giving login name, full name,
5660Sstevel@tonic-gate  * tty, idle time, login time, and host.
5670Sstevel@tonic-gate  */
5680Sstevel@tonic-gate void
5690Sstevel@tonic-gate shortprint(struct person *pers)
5700Sstevel@tonic-gate {
5710Sstevel@tonic-gate 	char *p;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	if (pers->pwd == 0) {
5740Sstevel@tonic-gate 		(void) printf("%-15s       ???\n", pers->name);
5750Sstevel@tonic-gate 		return;
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 	(void) printf("%-8s", pers->pwd->pw_name);
5780Sstevel@tonic-gate 	if (wide) {
5790Sstevel@tonic-gate 		if (pers->realname) {
5800Sstevel@tonic-gate 			(void) printf(" %-20.20s", pers->realname);
5810Sstevel@tonic-gate 		} else {
5820Sstevel@tonic-gate 			(void) printf("        ???          ");
5830Sstevel@tonic-gate 		}
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 	(void) putchar(' ');
5860Sstevel@tonic-gate 	if (pers->loggedin && !pers->writable) {
5870Sstevel@tonic-gate 		(void) putchar('*');
5880Sstevel@tonic-gate 	} else {
5890Sstevel@tonic-gate 		(void) putchar(' ');
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 	if (*pers->tty) {
5920Sstevel@tonic-gate 		(void) printf("%-11.11s ", pers->tty);
5930Sstevel@tonic-gate 	} else {
5940Sstevel@tonic-gate 		(void) printf("            ");  /* 12 spaces */
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 	p = ctime(&pers->loginat);
5970Sstevel@tonic-gate 	if (pers->loggedin) {
5980Sstevel@tonic-gate 		stimeprint(&pers->idletime);
5990Sstevel@tonic-gate 		(void) printf(" %3.3s %-5.5s ", p, p + 11);
6000Sstevel@tonic-gate 	} else if (pers->loginat == 0) {
6010Sstevel@tonic-gate 		(void) printf(" < .  .  .  . >");
6020Sstevel@tonic-gate 	} else if (tloc - pers->loginat >= 180 * 24 * 60 * 60) {
6030Sstevel@tonic-gate 		(void) printf(" <%-6.6s, %-4.4s>", p + 4, p + 20);
6040Sstevel@tonic-gate 	} else {
6050Sstevel@tonic-gate 		(void) printf(" <%-12.12s>", p + 4);
6060Sstevel@tonic-gate 	}
6070Sstevel@tonic-gate 	if (*pers->host) {
6080Sstevel@tonic-gate 		(void) printf(" %-20.20s", pers->host);
6090Sstevel@tonic-gate 	} else {
6100Sstevel@tonic-gate 		if (pers->ttyloc != NULL)
6110Sstevel@tonic-gate 			(void) printf(" %-20.20s", pers->ttyloc);
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 	(void) putchar('\n');
6140Sstevel@tonic-gate }
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate /*
6180Sstevel@tonic-gate  * print out a person in long format giving all possible information.
6190Sstevel@tonic-gate  * directory and shell are inhibited if unbrief is clear.
6200Sstevel@tonic-gate  */
6210Sstevel@tonic-gate void
6220Sstevel@tonic-gate personprint(struct person *pers)
6230Sstevel@tonic-gate {
6240Sstevel@tonic-gate 	if (pers->pwd == 0) {
6250Sstevel@tonic-gate 		(void) printf("Login name: %-10s\t\t\tIn real life: ???\n",
6260Sstevel@tonic-gate 			pers->name);
6270Sstevel@tonic-gate 		return;
6280Sstevel@tonic-gate 	}
6290Sstevel@tonic-gate 	(void) printf("Login name: %-10s", pers->pwd->pw_name);
6300Sstevel@tonic-gate 	if (pers->loggedin && !pers->writable) {
6310Sstevel@tonic-gate 		(void) printf("	(messages off)	");
6320Sstevel@tonic-gate 	} else {
6330Sstevel@tonic-gate 		(void) printf("			");
6340Sstevel@tonic-gate 	}
6350Sstevel@tonic-gate 	if (pers->realname) {
6360Sstevel@tonic-gate 		(void) printf("In real life: %s", pers->realname);
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 	if (unbrief) {
6390Sstevel@tonic-gate 		(void) printf("\nDirectory: %-25s", pers->pwd->pw_dir);
6400Sstevel@tonic-gate 		if (*pers->pwd->pw_shell)
6410Sstevel@tonic-gate 			(void) printf("\tShell: %-s", pers->pwd->pw_shell);
6420Sstevel@tonic-gate 	}
6430Sstevel@tonic-gate 	if (pers->loggedin) {
6440Sstevel@tonic-gate 		char *ep = ctime(&pers->loginat);
6450Sstevel@tonic-gate 		if (*pers->host) {
6460Sstevel@tonic-gate 			(void) printf("\nOn since %15.15s on %s from %s",
6470Sstevel@tonic-gate 				&ep[4], pers->tty, pers->host);
6480Sstevel@tonic-gate 			ltimeprint("\n", &pers->idletime, " Idle Time");
6490Sstevel@tonic-gate 		} else {
6500Sstevel@tonic-gate 			(void) printf("\nOn since %15.15s on %-12s",
6510Sstevel@tonic-gate 				&ep[4], pers->tty);
6520Sstevel@tonic-gate 			ltimeprint("\n", &pers->idletime, " Idle Time");
6530Sstevel@tonic-gate 		}
6540Sstevel@tonic-gate 	} else if (pers->loginat == 0) {
6550Sstevel@tonic-gate 		(void) printf("\nNever logged in.");
6560Sstevel@tonic-gate 	} else if (tloc - pers->loginat > 180 * 24 * 60 * 60) {
6570Sstevel@tonic-gate 		char *ep = ctime(&pers->loginat);
6580Sstevel@tonic-gate 		(void) printf("\nLast login %10.10s, %4.4s on %s",
6590Sstevel@tonic-gate 			ep, ep+20, pers->tty);
6600Sstevel@tonic-gate 		if (*pers->host) {
6610Sstevel@tonic-gate 			(void) printf(" from %s", pers->host);
6620Sstevel@tonic-gate 		}
6630Sstevel@tonic-gate 	} else {
6640Sstevel@tonic-gate 		char *ep = ctime(&pers->loginat);
6650Sstevel@tonic-gate 		(void) printf("\nLast login %16.16s on %s", ep, pers->tty);
6660Sstevel@tonic-gate 		if (*pers->host) {
6670Sstevel@tonic-gate 			(void) printf(" from %s", pers->host);
6680Sstevel@tonic-gate 		}
6690Sstevel@tonic-gate 	}
6700Sstevel@tonic-gate 	(void) putchar('\n');
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate /*
6750Sstevel@tonic-gate  * decode the information in the gecos field of /etc/passwd
6760Sstevel@tonic-gate  */
6770Sstevel@tonic-gate void
6780Sstevel@tonic-gate decode(struct person *pers)
6790Sstevel@tonic-gate {
6800Sstevel@tonic-gate 	char buffer[256];
6810Sstevel@tonic-gate 	char *bp, *gp, *lp;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	pers->realname = 0;
6840Sstevel@tonic-gate 	if (pers->pwd == 0)
6850Sstevel@tonic-gate 		return;
6860Sstevel@tonic-gate 	gp = pers->pwd->pw_gecos;
6870Sstevel@tonic-gate 	bp = buffer;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	if (gecos_ignore_c != '\0' &&
6900Sstevel@tonic-gate 	    *gp == gecos_ignore_c) {
6910Sstevel@tonic-gate 		gp++;
6920Sstevel@tonic-gate 	}
6930Sstevel@tonic-gate 	while (*gp != '\0' &&
6940Sstevel@tonic-gate 	    *gp != gecos_sep_c)	{			/* name */
6950Sstevel@tonic-gate 		if (*gp == gecos_samename) {
6960Sstevel@tonic-gate 			lp = pers->pwd->pw_name;
6970Sstevel@tonic-gate 			if (islower(*lp))
6980Sstevel@tonic-gate 				*bp++ = toupper(*lp++);
6990Sstevel@tonic-gate 			while (*bp++ = *lp++)
7000Sstevel@tonic-gate 				;
7010Sstevel@tonic-gate 			bp--;
7020Sstevel@tonic-gate 			gp++;
7030Sstevel@tonic-gate 		} else {
7040Sstevel@tonic-gate 			*bp++ = *gp++;
7050Sstevel@tonic-gate 		}
7060Sstevel@tonic-gate 	}
7070Sstevel@tonic-gate 	*bp++ = 0;
7080Sstevel@tonic-gate 	if (bp > (buffer + 1))
7090Sstevel@tonic-gate 		pers->realname = strdup(buffer);
7100Sstevel@tonic-gate 	if (pers->loggedin)
7110Sstevel@tonic-gate 		findidle(pers);
7120Sstevel@tonic-gate 	else
7130Sstevel@tonic-gate 		findwhen(pers);
7140Sstevel@tonic-gate }
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate /*
7170Sstevel@tonic-gate  * find the last log in of a user by checking the LASTLOG file.
7180Sstevel@tonic-gate  * the entry is indexed by the uid, so this can only be done if
7190Sstevel@tonic-gate  * the uid is known (which it isn't in quick mode)
7200Sstevel@tonic-gate  */
7210Sstevel@tonic-gate void
7220Sstevel@tonic-gate fwopen(void)
7230Sstevel@tonic-gate {
7240Sstevel@tonic-gate 	if ((lf = fopen(LASTLOG, "r")) == (FILE *)NULL)
7250Sstevel@tonic-gate 		(void) fprintf(stderr, "finger: %s open error\n", LASTLOG);
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate void
7290Sstevel@tonic-gate findwhen(struct person *pers)
7300Sstevel@tonic-gate {
7310Sstevel@tonic-gate 	struct lastlog ll;
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	if (lf != (FILE *)NULL) {
7340Sstevel@tonic-gate 		if (fseeko(lf, (off_t)pers->pwd->pw_uid * (off_t)sizeof (ll),
7350Sstevel@tonic-gate 		    SEEK_SET) == 0) {
7360Sstevel@tonic-gate 			if (fread((char *)&ll, sizeof (ll), 1, lf) == 1) {
7370Sstevel@tonic-gate 				int l_max, h_max;
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 				l_max = min(LMAX, sizeof (ll.ll_line));
7400Sstevel@tonic-gate 				h_max = min(HMAX, sizeof (ll.ll_host));
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 				bcopy(ll.ll_line, pers->tty, l_max);
7430Sstevel@tonic-gate 				pers->tty[l_max] = '\0';
7440Sstevel@tonic-gate 				bcopy(ll.ll_host, pers->host, h_max);
7450Sstevel@tonic-gate 				pers->host[h_max] = '\0';
7460Sstevel@tonic-gate 				pers->loginat = ll.ll_time;
7470Sstevel@tonic-gate 			} else {
7480Sstevel@tonic-gate 				if (ferror(lf))
7490Sstevel@tonic-gate 					(void) fprintf(stderr,
7500Sstevel@tonic-gate 					    "finger: %s read error\n", LASTLOG);
7510Sstevel@tonic-gate 				pers->tty[0] = 0;
7520Sstevel@tonic-gate 				pers->host[0] = 0;
7530Sstevel@tonic-gate 				pers->loginat = 0L;
7540Sstevel@tonic-gate 			}
7550Sstevel@tonic-gate 		} else {
7560Sstevel@tonic-gate 			(void) fprintf(stderr, "finger: %s fseeko error\n",
7570Sstevel@tonic-gate 			    LASTLOG);
7580Sstevel@tonic-gate 		}
7590Sstevel@tonic-gate 	} else {
7600Sstevel@tonic-gate 		pers->tty[0] = 0;
7610Sstevel@tonic-gate 		pers->host[0] = 0;
7620Sstevel@tonic-gate 		pers->loginat = 0L;
7630Sstevel@tonic-gate 	}
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate void
7670Sstevel@tonic-gate fwclose(void)
7680Sstevel@tonic-gate {
7690Sstevel@tonic-gate 	if (lf != (FILE *)0)
7700Sstevel@tonic-gate 		(void) fclose(lf);
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate /*
7740Sstevel@tonic-gate  * find the idle time of a user by doing a stat on /dev/tty??,
7750Sstevel@tonic-gate  * where tty?? has been gotten from UTMPX_FILE, supposedly.
7760Sstevel@tonic-gate  */
7770Sstevel@tonic-gate void
7780Sstevel@tonic-gate findidle(struct person *pers)
7790Sstevel@tonic-gate {
7800Sstevel@tonic-gate 	struct stat ttystatus;
7810Sstevel@tonic-gate #ifdef sun
7820Sstevel@tonic-gate 	struct stat inputdevstatus;
7830Sstevel@tonic-gate #endif
7840Sstevel@tonic-gate #define	TTYLEN (sizeof ("/dev/") - 1)
7850Sstevel@tonic-gate 	static char buffer[TTYLEN + LMAX + 1] = "/dev/";
7860Sstevel@tonic-gate 	time_t t;
7870Sstevel@tonic-gate 	time_t lastinputtime;
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	(void) strcpy(buffer + TTYLEN, pers->tty);
7900Sstevel@tonic-gate 	buffer[TTYLEN+LMAX] = 0;
7910Sstevel@tonic-gate 	if (stat(buffer, &ttystatus) < 0) {
7920Sstevel@tonic-gate 		(void) fprintf(stderr, "finger: Can't stat %s\n", buffer);
7930Sstevel@tonic-gate 		exit(4);
7940Sstevel@tonic-gate 	}
7950Sstevel@tonic-gate 	lastinputtime = ttystatus.st_atime;
7960Sstevel@tonic-gate #ifdef sun
7970Sstevel@tonic-gate 	if (strcmp(pers->tty, "console") == 0) {
7980Sstevel@tonic-gate 		/*
7990Sstevel@tonic-gate 		 * On the console, the user may be running a window system; if
8000Sstevel@tonic-gate 		 * so, their activity will show up in the last-access times of
8010Sstevel@tonic-gate 		 * "/dev/kbd" and "/dev/mouse", so take the minimum of the idle
8020Sstevel@tonic-gate 		 * times on those two devices and "/dev/console" and treat that
8030Sstevel@tonic-gate 		 * as the idle time.
8040Sstevel@tonic-gate 		 */
8050Sstevel@tonic-gate 		if (stat("/dev/kbd", &inputdevstatus) == 0) {
8060Sstevel@tonic-gate 			if (lastinputtime < inputdevstatus.st_atime)
8070Sstevel@tonic-gate 				lastinputtime = inputdevstatus.st_atime;
8080Sstevel@tonic-gate 		}
8090Sstevel@tonic-gate 		if (stat("/dev/mouse", &inputdevstatus) == 0) {
8100Sstevel@tonic-gate 			if (lastinputtime < inputdevstatus.st_atime)
8110Sstevel@tonic-gate 				lastinputtime = inputdevstatus.st_atime;
8120Sstevel@tonic-gate 		}
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate #endif
8150Sstevel@tonic-gate 	t = time(NULL);
8160Sstevel@tonic-gate 	if (t < lastinputtime)
8170Sstevel@tonic-gate 		pers->idletime = (time_t)0;
8180Sstevel@tonic-gate 	else
8190Sstevel@tonic-gate 		pers->idletime = t - lastinputtime;
8200Sstevel@tonic-gate 	pers->writable = (ttystatus.st_mode & TALKABLE) == TALKABLE;
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate /*
8240Sstevel@tonic-gate  * print idle time in short format; this program always prints 4 characters;
8250Sstevel@tonic-gate  * if the idle time is zero, it prints 4 blanks.
8260Sstevel@tonic-gate  */
8270Sstevel@tonic-gate void
8280Sstevel@tonic-gate stimeprint(time_t *dt)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate 	struct tm *delta;
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	delta = gmtime(dt);
8330Sstevel@tonic-gate 	if (delta->tm_yday == 0)
8340Sstevel@tonic-gate 		if (delta->tm_hour == 0)
8350Sstevel@tonic-gate 			if (delta->tm_min == 0)
8360Sstevel@tonic-gate 				(void) printf("    ");
8370Sstevel@tonic-gate 			else
8380Sstevel@tonic-gate 				(void) printf("  %2d", delta->tm_min);
8390Sstevel@tonic-gate 		else
8400Sstevel@tonic-gate 			if (delta->tm_hour >= 10)
8410Sstevel@tonic-gate 				(void) printf("%3d:", delta->tm_hour);
8420Sstevel@tonic-gate 			else
8430Sstevel@tonic-gate 				(void) printf("%1d:%02d",
8440Sstevel@tonic-gate 					delta->tm_hour, delta->tm_min);
8450Sstevel@tonic-gate 	else
8460Sstevel@tonic-gate 		(void) printf("%3dd", delta->tm_yday);
8470Sstevel@tonic-gate }
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate /*
8500Sstevel@tonic-gate  * print idle time in long format with care being taken not to pluralize
8510Sstevel@tonic-gate  * 1 minutes or 1 hours or 1 days.
8520Sstevel@tonic-gate  * print "prefix" first.
8530Sstevel@tonic-gate  */
8540Sstevel@tonic-gate void
8550Sstevel@tonic-gate ltimeprint(char *before, time_t *dt, char *after)
8560Sstevel@tonic-gate {
8570Sstevel@tonic-gate 	struct tm *delta;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	delta = gmtime(dt);
8600Sstevel@tonic-gate 	if (delta->tm_yday == 0 && delta->tm_hour == 0 && delta->tm_min == 0 &&
8610Sstevel@tonic-gate 	    delta->tm_sec <= 10)
8620Sstevel@tonic-gate 		return;
8630Sstevel@tonic-gate 	(void) printf("%s", before);
8640Sstevel@tonic-gate 	if (delta->tm_yday >= 10)
8650Sstevel@tonic-gate 		(void) printf("%d days", delta->tm_yday);
8660Sstevel@tonic-gate 	else if (delta->tm_yday > 0)
8670Sstevel@tonic-gate 		(void) printf("%d day%s %d hour%s",
8680Sstevel@tonic-gate 			delta->tm_yday, delta->tm_yday == 1 ? "" : "s",
8690Sstevel@tonic-gate 			delta->tm_hour, delta->tm_hour == 1 ? "" : "s");
8700Sstevel@tonic-gate 	else
8710Sstevel@tonic-gate 		if (delta->tm_hour >= 10)
8720Sstevel@tonic-gate 			(void) printf("%d hours", delta->tm_hour);
8730Sstevel@tonic-gate 		else if (delta->tm_hour > 0)
8740Sstevel@tonic-gate 			(void) printf("%d hour%s %d minute%s",
8750Sstevel@tonic-gate 				delta->tm_hour, delta->tm_hour == 1 ? "" : "s",
8760Sstevel@tonic-gate 				delta->tm_min, delta->tm_min == 1 ? "" : "s");
8770Sstevel@tonic-gate 		else
8780Sstevel@tonic-gate 			if (delta->tm_min >= 10)
8790Sstevel@tonic-gate 				(void) printf("%2d minutes", delta->tm_min);
8800Sstevel@tonic-gate 			else if (delta->tm_min == 0)
8810Sstevel@tonic-gate 				(void) printf("%2d seconds", delta->tm_sec);
8820Sstevel@tonic-gate 			else
8830Sstevel@tonic-gate 				(void) printf("%d minute%s %d second%s",
8840Sstevel@tonic-gate 					delta->tm_min,
8850Sstevel@tonic-gate 					delta->tm_min == 1 ? "" : "s",
8860Sstevel@tonic-gate 					delta->tm_sec,
8870Sstevel@tonic-gate 					delta->tm_sec == 1 ? "" : "s");
8880Sstevel@tonic-gate 	(void) printf("%s", after);
8890Sstevel@tonic-gate }
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate /*
8920Sstevel@tonic-gate  * The grammar of the pw_gecos field is sufficiently complex that the
8930Sstevel@tonic-gate  * best way to parse it is by using an explicit finite-state machine,
8940Sstevel@tonic-gate  * in which a table defines the rules of interpretation.
8950Sstevel@tonic-gate  *
8960Sstevel@tonic-gate  * Some special rules are necessary to handle the fact that names
8970Sstevel@tonic-gate  * may contain certain punctuation characters.  At this writing,
8980Sstevel@tonic-gate  * the possible punctuation characters are '.', '-', and '_'.
8990Sstevel@tonic-gate  *
9000Sstevel@tonic-gate  * Other rules are needed to account for characters that require special
9010Sstevel@tonic-gate  * processing when they appear in the pw_gecos field.  At present, there
9020Sstevel@tonic-gate  * are three such characters, with these default values and effects:
9030Sstevel@tonic-gate  *
9040Sstevel@tonic-gate  *    gecos_ignore_c   '*'    This character is ignored.
9050Sstevel@tonic-gate  *    gecos_sep_c      ','    Delimits displayed and nondisplayed contents.
9060Sstevel@tonic-gate  *    gecos_samename   '&'    Copies the login name into the output.
9070Sstevel@tonic-gate  *
9080Sstevel@tonic-gate  * As the program examines each successive character in the returned
9090Sstevel@tonic-gate  * pw_gecos value, it fetches (from the table) the FSM rule applicable
9100Sstevel@tonic-gate  * for that character in the current machine state, and thus determines
9110Sstevel@tonic-gate  * the next state.
9120Sstevel@tonic-gate  *
9130Sstevel@tonic-gate  * The possible states are:
9140Sstevel@tonic-gate  *    S0 start
9150Sstevel@tonic-gate  *    S1 in a word
9160Sstevel@tonic-gate  *    S2 not in a word
9170Sstevel@tonic-gate  *    S3 copy login name into output
9180Sstevel@tonic-gate  *    S4 end of GECOS field
9190Sstevel@tonic-gate  *
9200Sstevel@tonic-gate  * Here follows a depiction of the state transitions.
9210Sstevel@tonic-gate  *
9220Sstevel@tonic-gate  *
9230Sstevel@tonic-gate  *              gecos_ignore_c OR isspace OR any other character
9240Sstevel@tonic-gate  *                  +--+
9250Sstevel@tonic-gate  *                  |  |
9260Sstevel@tonic-gate  *                  |  V
9270Sstevel@tonic-gate  *                 +-----+
9280Sstevel@tonic-gate  *    NULL OR      | S0  |  isalpha OR isdigit
9290Sstevel@tonic-gate  * +---------------|start|------------------------+
9300Sstevel@tonic-gate  * |  gecos_sep_c  +-----+                        |     isalpha OR isdigit
9310Sstevel@tonic-gate  * |                |  |                          |   +---------------------+
9320Sstevel@tonic-gate  * |                |  |                          |   | OR '.' '-' '_'      |
9330Sstevel@tonic-gate  * |                |  |isspace                   |   |                     |
9340Sstevel@tonic-gate  * |                |  +-------+                  V   V                     |
9350Sstevel@tonic-gate  * |                |          |              +-----------+                 |
9360Sstevel@tonic-gate  * |                |          |              |    S1     |<--+             |
9370Sstevel@tonic-gate  * |                |          |              | in a word |   | isalpha OR  |
9380Sstevel@tonic-gate  * |                |          |              +-----------+   | isdigit OR  |
9390Sstevel@tonic-gate  * |                |          |               |  |  |  |     | '.' '-' '_' |
9400Sstevel@tonic-gate  * |                |    +----- ---------------+  |  |  +-----+             |
9410Sstevel@tonic-gate  * |                |    |     |                  |  |                      |
9420Sstevel@tonic-gate  * |                |    |     |   gecos_ignore_c |  |                      |
9430Sstevel@tonic-gate  * |                |    |     |   isspace        |  |                      |
9440Sstevel@tonic-gate  * |                |    |     |   ispunct/other  |  |                      |
9450Sstevel@tonic-gate  * |                |    |     |   any other char |  |                      |
9460Sstevel@tonic-gate  * |                |    |     |  +---------------+  |                      |
9470Sstevel@tonic-gate  * |                |    |     |  |                  |NULL OR gecos_sep_c   |
9480Sstevel@tonic-gate  * |                |    |     |  |                  +------------------+   |
9490Sstevel@tonic-gate  * |  gecos_samename|    |     V  V                                     |   |
9500Sstevel@tonic-gate  * |  +-------------+    |    +---------------+                         |   |
9510Sstevel@tonic-gate  * |  |                  |    |       S2      | isspace OR '.' '-' '_'  |   |
9520Sstevel@tonic-gate  * |  |  gecos_samename  |    | not in a word |<---------------------+  |   |
9530Sstevel@tonic-gate  * |  |  +---------------+    +---------------+ OR gecos_ignore_c    |  |   |
9540Sstevel@tonic-gate  * |  |  |                        |    ^  |  |  OR ispunct OR other  |  |   |
9550Sstevel@tonic-gate  * |  |  |                        |    |  |  |                       |  |   |
9560Sstevel@tonic-gate  * |  |  |  gecos_samename        |    |  |  +-----------------------+  |   |
9570Sstevel@tonic-gate  * |  |  |  +---------------------+    |  |                             |   |
9580Sstevel@tonic-gate  * |  |  |  |                          |  |                             |   |
9590Sstevel@tonic-gate  * |  |  |  |            gecos_ignore_c|  | NULL OR gecos_sep_c         |   |
9600Sstevel@tonic-gate  * |  |  |  |            gecos_samename|  +-----------------------+     |   |
9610Sstevel@tonic-gate  * |  |  |  |            ispunct/other |                          |     |   |
9620Sstevel@tonic-gate  * |  V  V  V            isspace       |                          |     |   |
9630Sstevel@tonic-gate  * | +-----------------+ any other char|                          |     |   |
9640Sstevel@tonic-gate  * | |      S3         |---------------+  isalpha OR isdigit OR   |     |   |
9650Sstevel@tonic-gate  * | |insert login name|------------------------------------------ ----- ---+
9660Sstevel@tonic-gate  * | +-----------------+                  '.' '-' '_'             |     |
9670Sstevel@tonic-gate  * |                |    NULL OR gecos_sep_c                      |     |
9680Sstevel@tonic-gate  * |                +------------------------------------------+  |     |
9690Sstevel@tonic-gate  * |                                                           |  |     |
9700Sstevel@tonic-gate  * |                                                           V  V     V
9710Sstevel@tonic-gate  * |                                                         +------------+
9720Sstevel@tonic-gate  * | NULL OR gecos_sep_c                                     |     S4     |
9730Sstevel@tonic-gate  * +-------------------------------------------------------->|end of gecos|<--+
9740Sstevel@tonic-gate  *                                                           +------------+   |
9750Sstevel@tonic-gate  *                                                                      | all |
9760Sstevel@tonic-gate  *                                                                      +-----+
9770Sstevel@tonic-gate  *
9780Sstevel@tonic-gate  *
9790Sstevel@tonic-gate  *  The transitions from the above diagram are summarized in
9800Sstevel@tonic-gate  *  the following table of target states, which is implemented
9810Sstevel@tonic-gate  *  in code as the gecos_fsm array.
9820Sstevel@tonic-gate  *
9830Sstevel@tonic-gate  * Input:
9840Sstevel@tonic-gate  *        +--gecos_ignore_c
9850Sstevel@tonic-gate  *        |    +--gecos_sep_c
9860Sstevel@tonic-gate  *        |    |    +--gecos_samename
9870Sstevel@tonic-gate  *        |    |    |    +--isalpha
9880Sstevel@tonic-gate  *        |    |    |    |    +--isdigit
9890Sstevel@tonic-gate  *        |    |    |    |    |      +--isspace
9900Sstevel@tonic-gate  *        |    |    |    |    |      |    +--punctuation possible in name
9910Sstevel@tonic-gate  *        |    |    |    |    |      |    |    +--other punctuation
9920Sstevel@tonic-gate  *        |    |    |    |    |      |    |    |    +--NULL character
9930Sstevel@tonic-gate  *        |    |    |    |    |      |    |    |    |    +--any other character
9940Sstevel@tonic-gate  *        |    |    |    |    |      |    |    |    |    |
9950Sstevel@tonic-gate  *        V    V    V    V    V      V    V    V    V    V
9960Sstevel@tonic-gate  * From: ---------------------------------------------------
9970Sstevel@tonic-gate  * S0   | S0 | S4 | S3 | S1 | S1 |   S0 | S1 | S2 | S4 | S0 |
9980Sstevel@tonic-gate  * S1   | S2 | S4 | S3 | S1 | S1 |   S2 | S1 | S2 | S4 | S2 |
9990Sstevel@tonic-gate  * S2   | S2 | S4 | S3 | S1 | S1 |   S2 | S2 | S2 | S4 | S2 |
10000Sstevel@tonic-gate  * S3   | S2 | S4 | S2 | S1 | S1 |   S2 | S1 | S2 | S4 | S2 |
10010Sstevel@tonic-gate  * S4   | S4 | S4 | S4 | S4 | S4 |   S4 | S4 | S4 | S4 | S4 |
10020Sstevel@tonic-gate  *
10030Sstevel@tonic-gate  */
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate /*
10060Sstevel@tonic-gate  * Data types and structures for scanning the pw_gecos field.
10070Sstevel@tonic-gate  */
10080Sstevel@tonic-gate typedef enum gecos_state {
10090Sstevel@tonic-gate 	S0,		/* start */
10100Sstevel@tonic-gate 	S1,		/* in a word */
10110Sstevel@tonic-gate 	S2,		/* not in a word */
10120Sstevel@tonic-gate 	S3,		/* copy login */
10130Sstevel@tonic-gate 	S4		/* end of gecos */
10140Sstevel@tonic-gate } gecos_state_t;
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate #define	GFSM_ROWS 5
10170Sstevel@tonic-gate #define	GFSM_COLS 10
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate gecos_state_t gecos_fsm[GFSM_ROWS][GFSM_COLS] = {
10200Sstevel@tonic-gate 	{S0, S4, S3, S1, S1,	S0, S1, S2, S4, S0},	/* S0 */
10210Sstevel@tonic-gate 	{S2, S4, S3, S1, S1,	S2, S1, S2, S4, S2},	/* S1 */
10220Sstevel@tonic-gate 	{S2, S4, S3, S1, S1,	S2, S2, S2, S4, S2},	/* S2 */
10230Sstevel@tonic-gate 	{S2, S4, S2, S1, S1,	S2, S1, S2, S4, S2},	/* S3 */
10240Sstevel@tonic-gate 	{S4, S4, S4, S4, S4,	S4, S4, S4, S4, S4}	/* S4 */
10250Sstevel@tonic-gate };
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate /*
10280Sstevel@tonic-gate  * Scan the pw_gecos field according to defined state table;
10290Sstevel@tonic-gate  * return the next state according the the rules.
10300Sstevel@tonic-gate  */
10310Sstevel@tonic-gate gecos_state_t
10320Sstevel@tonic-gate gecos_scan_state(gecos_state_t instate, char ch)
10330Sstevel@tonic-gate {
10340Sstevel@tonic-gate 	if (ch == gecos_ignore_c) {
10350Sstevel@tonic-gate 		return (gecos_fsm[instate][0]);
10360Sstevel@tonic-gate 	} else if (ch == gecos_sep_c) {
10370Sstevel@tonic-gate 		return (gecos_fsm[instate][1]);
10380Sstevel@tonic-gate 	} else if (ch == gecos_samename) {
10390Sstevel@tonic-gate 		return (gecos_fsm[instate][2]);
10400Sstevel@tonic-gate 	} else if (isalpha(ch)) {
10410Sstevel@tonic-gate 		return (gecos_fsm[instate][3]);
10420Sstevel@tonic-gate 	} else if (isdigit(ch)) {
10430Sstevel@tonic-gate 		return (gecos_fsm[instate][4]);
10440Sstevel@tonic-gate 	} else if (isspace(ch)) {
10450Sstevel@tonic-gate 		return (gecos_fsm[instate][5]);
10460Sstevel@tonic-gate 	} else if (ch == '.' || ch == '-' || ch == '_') {
10470Sstevel@tonic-gate 		return (gecos_fsm[instate][6]);
10480Sstevel@tonic-gate 	} else if (ispunct(ch)) {
10490Sstevel@tonic-gate 		return (gecos_fsm[instate][7]);
10500Sstevel@tonic-gate 	} else if (ch == '\0') {
10510Sstevel@tonic-gate 		return (gecos_fsm[instate][8]);
10520Sstevel@tonic-gate 	}
10530Sstevel@tonic-gate 	return (gecos_fsm[instate][9]);
10540Sstevel@tonic-gate }
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate /*
10580Sstevel@tonic-gate  * Compare the given argument, which is taken to be a username, with
10590Sstevel@tonic-gate  * the login name and with strings in the the pw_gecos field.
10600Sstevel@tonic-gate  */
10610Sstevel@tonic-gate int
10620Sstevel@tonic-gate matchcmp(char *gname, char *login, char *given)
10630Sstevel@tonic-gate {
10640Sstevel@tonic-gate 	char	buffer[100];
10650Sstevel@tonic-gate 	char	*bp, *lp, *gp;
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	gecos_state_t kstate = S0;
10680Sstevel@tonic-gate 	gecos_state_t kstate_next = S0;
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	if (*gname == '\0' && *given == '\0')
10710Sstevel@tonic-gate 		return (1);
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	bp = buffer;
10740Sstevel@tonic-gate 	gp = gname;
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	do {
10770Sstevel@tonic-gate 		kstate_next = gecos_scan_state(kstate, *gp);
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 		switch (kstate_next) {
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 		case S0:
10820Sstevel@tonic-gate 			gp++;
10830Sstevel@tonic-gate 			break;
10840Sstevel@tonic-gate 		case S1:
10850Sstevel@tonic-gate 			if (bp < buffer + sizeof (buffer)) {
10860Sstevel@tonic-gate 				*bp++ = *gp++;
10870Sstevel@tonic-gate 			}
10880Sstevel@tonic-gate 			break;
10890Sstevel@tonic-gate 		case S2:
10900Sstevel@tonic-gate 			if (kstate == S1 || kstate == S3) {
10910Sstevel@tonic-gate 				*bp++ = ' ';
10920Sstevel@tonic-gate 			}
10930Sstevel@tonic-gate 			gp++;
10940Sstevel@tonic-gate 			break;
10950Sstevel@tonic-gate 		case S3:
10960Sstevel@tonic-gate 			lp = login;
10970Sstevel@tonic-gate 			do {
10980Sstevel@tonic-gate 				*bp++ = *lp++;
10990Sstevel@tonic-gate 			} while (*bp != '\0' && bp < buffer + sizeof (buffer));
11000Sstevel@tonic-gate 			bp--;
11010Sstevel@tonic-gate 			break;
11020Sstevel@tonic-gate 		case S4:
11030Sstevel@tonic-gate 			*bp++ = '\0';
11040Sstevel@tonic-gate 			break;
11050Sstevel@tonic-gate 		default:
11060Sstevel@tonic-gate 			*bp++ = '\0';
11070Sstevel@tonic-gate 			break;
11080Sstevel@tonic-gate 		}
11090Sstevel@tonic-gate 		kstate = kstate_next;
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	} while ((bp < buffer + sizeof (buffer)) && kstate != S4);
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	gp = strtok(buffer, " ");
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	while (gp != NULL) {
11160Sstevel@tonic-gate 		if (namecmp(gp, given) > 0) {
11170Sstevel@tonic-gate 			return (1);
11180Sstevel@tonic-gate 		}
11190Sstevel@tonic-gate 		gp = strtok(NULL, " ");
11200Sstevel@tonic-gate 	}
11210Sstevel@tonic-gate 	return (0);
11220Sstevel@tonic-gate }
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate /*
11250Sstevel@tonic-gate  * Perform the character-by-character comparison.
11260Sstevel@tonic-gate  * It is intended that "finger foo" should match "foo2", but an argument
11270Sstevel@tonic-gate  * consisting entirely of digits should not be matched too broadly.
11280Sstevel@tonic-gate  * Also, we do not want "finger foo123" to match "Mr. Foo" in the gecos.
11290Sstevel@tonic-gate  */
11300Sstevel@tonic-gate int
11310Sstevel@tonic-gate namecmp(char *name1, char *name2)
11320Sstevel@tonic-gate {
11330Sstevel@tonic-gate 	char c1, c2;
11340Sstevel@tonic-gate 	boolean_t alphaseen = B_FALSE;
11350Sstevel@tonic-gate 	boolean_t digitseen = B_FALSE;
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	for (;;) {
11380Sstevel@tonic-gate 		c1 = *name1++;
11390Sstevel@tonic-gate 		if (isalpha(c1))
11400Sstevel@tonic-gate 			alphaseen = B_TRUE;
11410Sstevel@tonic-gate 		if (isdigit(c1))
11420Sstevel@tonic-gate 			digitseen = B_TRUE;
11430Sstevel@tonic-gate 		if (isupper(c1))
11440Sstevel@tonic-gate 			c1 = tolower(c1);
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 		c2 = *name2++;
11470Sstevel@tonic-gate 		if (isupper(c2))
11480Sstevel@tonic-gate 			c2 = tolower(c2);
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 		if (c1 != c2)
11510Sstevel@tonic-gate 			break;
11520Sstevel@tonic-gate 		if (c1 == '\0')
11530Sstevel@tonic-gate 			return (1);
11540Sstevel@tonic-gate 	}
11550Sstevel@tonic-gate 	if (!c1) {
11560Sstevel@tonic-gate 		for (name2--; isdigit(*name2); name2++)
11570Sstevel@tonic-gate 			;
11580Sstevel@tonic-gate 		if (*name2 == '\0' && digitseen) {
11590Sstevel@tonic-gate 			return (1);
11600Sstevel@tonic-gate 		}
11610Sstevel@tonic-gate 	} else if (!c2) {
11620Sstevel@tonic-gate 		for (name1--; isdigit(*name1); name1++)
11630Sstevel@tonic-gate 			;
11640Sstevel@tonic-gate 		if (*name1 == '\0' && alphaseen) {
11650Sstevel@tonic-gate 			return (1);
11660Sstevel@tonic-gate 		}
11670Sstevel@tonic-gate 	}
11680Sstevel@tonic-gate 	return (0);
11690Sstevel@tonic-gate }
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate int
11730Sstevel@tonic-gate netfinger(char *name)
11740Sstevel@tonic-gate {
11750Sstevel@tonic-gate 	char *host;
11760Sstevel@tonic-gate 	struct hostent *hp;
11770Sstevel@tonic-gate 	struct sockaddr_in6 sin6;
11780Sstevel@tonic-gate 	struct in6_addr ipv6addr;
11790Sstevel@tonic-gate 	struct in_addr ipv4addr;
11800Sstevel@tonic-gate 	int s;
11810Sstevel@tonic-gate 	FILE *f;
11820Sstevel@tonic-gate 	int c;
11830Sstevel@tonic-gate 	int lastc;
11840Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
11850Sstevel@tonic-gate 	int error_num;
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	if (name == NULL)
11880Sstevel@tonic-gate 		return (0);
11890Sstevel@tonic-gate 	host = strrchr(name, '@');
11900Sstevel@tonic-gate 	if (host == NULL)
11910Sstevel@tonic-gate 		return (0);
11920Sstevel@tonic-gate 	*host++ = 0;
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	if ((hp = getipnodebyname(host, AF_INET6, AI_ALL | AI_ADDRCONFIG |
11950Sstevel@tonic-gate 	    AI_V4MAPPED, &error_num)) == NULL) {
11960Sstevel@tonic-gate 		if (error_num == TRY_AGAIN) {
11970Sstevel@tonic-gate 			(void) fprintf(stderr,
11980Sstevel@tonic-gate 			    "unknown host: %s (try again later)\n", host);
11990Sstevel@tonic-gate 		} else {
12000Sstevel@tonic-gate 			(void) fprintf(stderr, "unknown host: %s\n", host);
12010Sstevel@tonic-gate 		}
12020Sstevel@tonic-gate 		return (1);
12030Sstevel@tonic-gate 	}
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	/*
12060Sstevel@tonic-gate 	 * If hp->h_name is a IPv4-mapped IPv6 literal, we'll convert it to
12070Sstevel@tonic-gate 	 * IPv4 literal address.
12080Sstevel@tonic-gate 	 */
12090Sstevel@tonic-gate 	if ((inet_pton(AF_INET6, hp->h_name, &ipv6addr) > 0) &&
12100Sstevel@tonic-gate 	    IN6_IS_ADDR_V4MAPPED(&ipv6addr)) {
12110Sstevel@tonic-gate 		IN6_V4MAPPED_TO_INADDR(&ipv6addr, &ipv4addr);
12120Sstevel@tonic-gate 		(void) printf("[%s] ", inet_ntop(AF_INET, &ipv4addr, abuf,
12130Sstevel@tonic-gate 		    sizeof (abuf)));
12140Sstevel@tonic-gate 	} else {
12150Sstevel@tonic-gate 		(void) printf("[%s] ", hp->h_name);
12160Sstevel@tonic-gate 	}
12170Sstevel@tonic-gate 	bzero(&sin6, sizeof (sin6));
12180Sstevel@tonic-gate 	sin6.sin6_family = hp->h_addrtype;
12190Sstevel@tonic-gate 	bcopy(hp->h_addr_list[0], (char *)&sin6.sin6_addr, hp->h_length);
12200Sstevel@tonic-gate 	sin6.sin6_port = htons(IPPORT_FINGER);
12210Sstevel@tonic-gate 	s = socket(sin6.sin6_family, SOCK_STREAM, 0);
12220Sstevel@tonic-gate 	if (s < 0) {
12230Sstevel@tonic-gate 		(void) fflush(stdout);
12240Sstevel@tonic-gate 		perror("socket");
12250Sstevel@tonic-gate 		freehostent(hp);
12260Sstevel@tonic-gate 		return (1);
12270Sstevel@tonic-gate 	}
12280Sstevel@tonic-gate 	while (connect(s, (struct sockaddr *)&sin6, sizeof (sin6)) < 0) {
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 		if (hp && hp->h_addr_list[1]) {
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 			hp->h_addr_list++;
12330Sstevel@tonic-gate 			bcopy(hp->h_addr_list[0],
12340Sstevel@tonic-gate 			    (caddr_t)&sin6.sin6_addr, hp->h_length);
12350Sstevel@tonic-gate 			(void) close(s);
12360Sstevel@tonic-gate 			s = socket(sin6.sin6_family, SOCK_STREAM, 0);
12370Sstevel@tonic-gate 			if (s < 0) {
12380Sstevel@tonic-gate 				(void) fflush(stdout);
12390Sstevel@tonic-gate 				perror("socket");
12400Sstevel@tonic-gate 				freehostent(hp);
12410Sstevel@tonic-gate 				return (0);
12420Sstevel@tonic-gate 			}
12430Sstevel@tonic-gate 			continue;
12440Sstevel@tonic-gate 		}
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 		(void) fflush(stdout);
12470Sstevel@tonic-gate 		perror("connect");
12480Sstevel@tonic-gate 		(void) close(s);
12490Sstevel@tonic-gate 		freehostent(hp);
12500Sstevel@tonic-gate 		return (1);
12510Sstevel@tonic-gate 	}
12520Sstevel@tonic-gate 	freehostent(hp);
12530Sstevel@tonic-gate 	hp = NULL;
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 	(void) printf("\n");
12560Sstevel@tonic-gate 	if (large)
12570Sstevel@tonic-gate 		(void) write(s, "/W ", 3);
12580Sstevel@tonic-gate 	(void) write(s, name, strlen(name));
12590Sstevel@tonic-gate 	(void) write(s, "\r\n", 2);
12600Sstevel@tonic-gate 	f = fdopen(s, "r");
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	lastc = '\n';
12630Sstevel@tonic-gate 	while ((c = getc(f)) != EOF) {
12640Sstevel@tonic-gate 		/* map CRLF -> newline */
12650Sstevel@tonic-gate 		if ((lastc == '\r') && (c != '\n'))
12660Sstevel@tonic-gate 			/* print out saved CR */
12670Sstevel@tonic-gate 			(void) putchar('\r');
12680Sstevel@tonic-gate 		lastc = c;
12690Sstevel@tonic-gate 		if (c == '\r')
12700Sstevel@tonic-gate 			continue;
12710Sstevel@tonic-gate 		(void) putchar(c);
12720Sstevel@tonic-gate 	}
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 	if (lastc != '\n')
12750Sstevel@tonic-gate 		(void) putchar('\n');
12760Sstevel@tonic-gate 	(void) fclose(f);
12770Sstevel@tonic-gate 	return (1);
12780Sstevel@tonic-gate }
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate /*
12810Sstevel@tonic-gate  *	AnyMail - takes a username (string pointer thereto), and
12820Sstevel@tonic-gate  *	prints on standard output whether there is any unread mail,
12830Sstevel@tonic-gate  *	and if so, how old it is.	(JCM@Shasta 15 March 80)
12840Sstevel@tonic-gate  */
12850Sstevel@tonic-gate void
12860Sstevel@tonic-gate AnyMail(char *name)
12870Sstevel@tonic-gate {
12880Sstevel@tonic-gate 	struct stat buf;		/* space for file status buffer */
12890Sstevel@tonic-gate 	char *mbxdir = MAILDIR; 	/* string with path preamble */
12900Sstevel@tonic-gate 	char *mbxpath;			/* space for entire pathname */
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 	char *timestr;
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 	mbxpath = malloc(strlen(name) + strlen(MAILDIR) + 1);
12950Sstevel@tonic-gate 	if (mbxpath == (char *)NULL)
12960Sstevel@tonic-gate 		return;
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	(void) strcpy(mbxpath, mbxdir);	/* copy preamble into path name */
12990Sstevel@tonic-gate 	(void) strcat(mbxpath, name);	/* concatenate user name to path */
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 	if (stat(mbxpath, &buf) == -1 || buf.st_size == 0) {
13020Sstevel@tonic-gate 		/* Mailbox is empty or nonexistent */
13030Sstevel@tonic-gate 		(void) printf("No unread mail\n");
13040Sstevel@tonic-gate 	} else {
13050Sstevel@tonic-gate 		if (buf.st_mtime < buf.st_atime) {
13060Sstevel@tonic-gate 			/*
13070Sstevel@tonic-gate 			 * No new mail since the last time the user read it.
13080Sstevel@tonic-gate 			 */
13090Sstevel@tonic-gate 			(void) printf("Mail last read ");
13100Sstevel@tonic-gate 			(void) printf("%s", ctime(&buf.st_atime));
13110Sstevel@tonic-gate 		} else if (buf.st_mtime > buf.st_atime) {
13120Sstevel@tonic-gate 			/*
13130Sstevel@tonic-gate 			 * New mail has definitely arrived since the last time
13140Sstevel@tonic-gate 			 * mail was read.  mtime is the time the most recent
13150Sstevel@tonic-gate 			 * message arrived; atime is either the time the oldest
13160Sstevel@tonic-gate 			 * unread message arrived, or the last time the mail
13170Sstevel@tonic-gate 			 * was read.
13180Sstevel@tonic-gate 			 */
13190Sstevel@tonic-gate 			(void) printf("New mail received ");
13200Sstevel@tonic-gate 			timestr = ctime(&buf.st_mtime); /* time last modified */
13210Sstevel@tonic-gate 			timestr[24] = '\0';	/* suppress newline (ugh) */
13220Sstevel@tonic-gate 			(void) printf("%s", timestr);
13230Sstevel@tonic-gate 			(void) printf(";\n  unread since ");
13240Sstevel@tonic-gate 			(void) printf("%s", ctime(&buf.st_atime));
13250Sstevel@tonic-gate 		} else {
13260Sstevel@tonic-gate 			/*
13270Sstevel@tonic-gate 			 * There is something in mailbox, but we can't really
13280Sstevel@tonic-gate 			 * be sure whether it is mail held there by the user
13290Sstevel@tonic-gate 			 * or a (single) new message that was placed in a newly
13300Sstevel@tonic-gate 			 * recreated mailbox, so punt and call it "unread mail."
13310Sstevel@tonic-gate 			 */
13320Sstevel@tonic-gate 			(void) printf("Unread mail since ");
13330Sstevel@tonic-gate 			(void) printf("%s", ctime(&buf.st_mtime));
13340Sstevel@tonic-gate 		}
13350Sstevel@tonic-gate 	}
13360Sstevel@tonic-gate 	free(mbxpath);
13370Sstevel@tonic-gate }
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate /*
13400Sstevel@tonic-gate  * return true iff we've already printed project/plan for this uid;
13410Sstevel@tonic-gate  * if not, enter this uid into table (so this function has a side-effect.)
13420Sstevel@tonic-gate  */
13430Sstevel@tonic-gate #define	PPMAX	4096		/* assume no more than 4096 logged-in users */
13440Sstevel@tonic-gate uid_t	PlanPrinted[PPMAX+1];
13450Sstevel@tonic-gate int	PPIndex = 0;		/* index of next unused table entry */
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate int
13480Sstevel@tonic-gate AlreadyPrinted(uid_t uid)
13490Sstevel@tonic-gate {
13500Sstevel@tonic-gate 	int i = 0;
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	while (i++ < PPIndex) {
13530Sstevel@tonic-gate 		if (PlanPrinted[i] == uid)
13540Sstevel@tonic-gate 		return (1);
13550Sstevel@tonic-gate 	}
13560Sstevel@tonic-gate 	if (i < PPMAX) {
13570Sstevel@tonic-gate 		PlanPrinted[i] = uid;
13580Sstevel@tonic-gate 		PPIndex++;
13590Sstevel@tonic-gate 	}
13600Sstevel@tonic-gate 	return (0);
13610Sstevel@tonic-gate }
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate #define	FIFOREADTIMEOUT	(60)	/* read timeout on select */
13640Sstevel@tonic-gate /* BEGIN CSTYLED */
13650Sstevel@tonic-gate #define	PRINT_CHAR(c)						\
13660Sstevel@tonic-gate 	(							\
13670Sstevel@tonic-gate 		((termpass & TS_HIGH) && ((int)c) > 126)	\
13680Sstevel@tonic-gate 		||						\
13690Sstevel@tonic-gate 		(isascii((int)c) && 				\
13700Sstevel@tonic-gate 			 (isprint((int)c) || isspace((int)c))	\
13710Sstevel@tonic-gate 		)						\
13720Sstevel@tonic-gate 		||						\
13730Sstevel@tonic-gate 		((termpass & TS_LOW) && ((int)c) < 32)		\
13740Sstevel@tonic-gate 	)
13750Sstevel@tonic-gate /* END CSTYLED */
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate void
13790Sstevel@tonic-gate catfile(char *s, mode_t mode, int trunc_at_nl)
13800Sstevel@tonic-gate {
13810Sstevel@tonic-gate 	if (S_ISFIFO(mode)) {
13820Sstevel@tonic-gate 		int fd;
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 		fd = open(s, O_RDONLY | O_NONBLOCK);
13850Sstevel@tonic-gate 		if (fd != -1) {
13860Sstevel@tonic-gate 			fd_set readfds, exceptfds;
13870Sstevel@tonic-gate 			struct timeval tv;
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 			FD_ZERO(&readfds);
13900Sstevel@tonic-gate 			FD_ZERO(&exceptfds);
13910Sstevel@tonic-gate 			FD_SET(fd, &readfds);
13920Sstevel@tonic-gate 			FD_SET(fd, &exceptfds);
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 			timerclear(&tv);
13950Sstevel@tonic-gate 			tv.tv_sec = FIFOREADTIMEOUT;
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 			(void) fflush(stdout);
13980Sstevel@tonic-gate 			while (select(fd + 1, &readfds, (fd_set *) 0,
13990Sstevel@tonic-gate 				&exceptfds, &tv) != -1) {
14000Sstevel@tonic-gate 				unsigned char buf[BUFSIZ];
14010Sstevel@tonic-gate 				int nread;
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 				nread = read(fd, buf, sizeof (buf));
14040Sstevel@tonic-gate 				if (nread > 0) {
14050Sstevel@tonic-gate 					unsigned char *p;
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 					FD_SET(fd, &readfds);
14080Sstevel@tonic-gate 					FD_SET(fd, &exceptfds);
14090Sstevel@tonic-gate 					for (p = buf; p < buf + nread; p++) {
14100Sstevel@tonic-gate 						if (trunc_at_nl && *p == '\n')
14110Sstevel@tonic-gate 							goto out;
14120Sstevel@tonic-gate 						if (PRINT_CHAR(*p))
14130Sstevel@tonic-gate 							(void) putchar((int)*p);
14140Sstevel@tonic-gate 						else if (isascii(*p))
14150Sstevel@tonic-gate 							(void) fputs(unctrl(*p),
14160Sstevel@tonic-gate 								stdout);
14170Sstevel@tonic-gate 					}
14180Sstevel@tonic-gate 				} else
14190Sstevel@tonic-gate 					break;
14200Sstevel@tonic-gate 			}
14210Sstevel@tonic-gate out:
14220Sstevel@tonic-gate 			(void) close(fd);
14230Sstevel@tonic-gate 		}
14240Sstevel@tonic-gate 	} else {
14250Sstevel@tonic-gate 		int c;
14260Sstevel@tonic-gate 		FILE *fp;
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 		fp = fopen(s, "r");
14290Sstevel@tonic-gate 		if (fp) {
14300Sstevel@tonic-gate 			while ((c = getc(fp)) != EOF) {
14310Sstevel@tonic-gate 				if (trunc_at_nl && c == '\n')
14320Sstevel@tonic-gate 					break;
14330Sstevel@tonic-gate 				if (PRINT_CHAR(c))
14340Sstevel@tonic-gate 					(void) putchar((int)c);
14350Sstevel@tonic-gate 				else
14360Sstevel@tonic-gate 					if (isascii(c))
14370Sstevel@tonic-gate 						(void) fputs(unctrl(c), stdout);
14380Sstevel@tonic-gate 			}
14390Sstevel@tonic-gate 			(void) fclose(fp);
14400Sstevel@tonic-gate 		}
14410Sstevel@tonic-gate 	}
14420Sstevel@tonic-gate }
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate void
14460Sstevel@tonic-gate initscreening(void)
14470Sstevel@tonic-gate {
14480Sstevel@tonic-gate 	char *options, *value;
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 	if (defopen(defaultfile) == 0) {
14510Sstevel@tonic-gate 		char	*cp;
14520Sstevel@tonic-gate 		int	flags;
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 		/*
14550Sstevel@tonic-gate 		 * ignore case
14560Sstevel@tonic-gate 		 */
14570Sstevel@tonic-gate 		flags = defcntl(DC_GETFLAGS, 0);
14580Sstevel@tonic-gate 		TURNOFF(flags, DC_CASE);
1459*2923Sraf 		(void) defcntl(DC_SETFLAGS, flags);
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 		if (cp = defread(passvar)) {
14620Sstevel@tonic-gate 			options = cp;
14630Sstevel@tonic-gate 			while (*options != '\0')
14640Sstevel@tonic-gate 				switch (getsubopt(&options, termopts, &value)) {
14650Sstevel@tonic-gate 				case TERM_LOW:
14660Sstevel@tonic-gate 					termpass |= TS_LOW;
14670Sstevel@tonic-gate 					break;
14680Sstevel@tonic-gate 				case TERM_HIGH:
14690Sstevel@tonic-gate 					termpass |= TS_HIGH;
14700Sstevel@tonic-gate 					break;
14710Sstevel@tonic-gate 				}
14720Sstevel@tonic-gate 		}
14730Sstevel@tonic-gate 		(void) defopen(NULL);	/* close default file */
14740Sstevel@tonic-gate 	}
14750Sstevel@tonic-gate }
1476