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