xref: /csrg-svn/usr.bin/finger/sprint.c (revision 50613)
137660Sbostic /*
237660Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337660Sbostic  * All rights reserved.
437660Sbostic  *
540027Sbostic  * This code is derived from software contributed to Berkeley by
640027Sbostic  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
740027Sbostic  *
842731Sbostic  * %sccs.include.redist.c%
937660Sbostic  */
1037660Sbostic 
1137660Sbostic #ifndef lint
12*50613Sbostic static char sccsid[] = "@(#)sprint.c	5.10 (Berkeley) 07/27/91";
1337660Sbostic #endif /* not lint */
1437660Sbostic 
1537660Sbostic #include <sys/types.h>
1637660Sbostic #include <sys/time.h>
1750596Sbostic #include <time.h>
1837660Sbostic #include <tzfile.h>
19*50613Sbostic #include <db.h>
2050596Sbostic #include <pwd.h>
21*50613Sbostic #include <errno.h>
2250596Sbostic #include <utmp.h>
2337660Sbostic #include <stdio.h>
2450596Sbostic #include <stdlib.h>
2550596Sbostic #include <string.h>
2637660Sbostic #include "finger.h"
2737660Sbostic 
2850596Sbostic static int	  psort __P((const void *, const void *));
2950596Sbostic static PERSON	**sort __P((void));
3050596Sbostic static void	  stimeprint __P((WHERE *));
3137660Sbostic 
3250596Sbostic void
3337660Sbostic sflag_print()
3437660Sbostic {
3537660Sbostic 	extern time_t now;
3637660Sbostic 	register PERSON *pn;
3737664Sedward 	register WHERE *w;
3837660Sbostic 	register int cnt;
3937660Sbostic 	register char *p;
4050596Sbostic 	PERSON **list;
4137660Sbostic 
4237660Sbostic 	list = sort();
4337660Sbostic 	/*
4437660Sbostic 	 * short format --
4537660Sbostic 	 *	login name
4637660Sbostic 	 *	real name
4737660Sbostic 	 *	terminal name (the XX of ttyXX)
4837660Sbostic 	 *	if terminal writeable (add an '*' to the terminal name
4937660Sbostic 	 *		if not)
5037660Sbostic 	 *	if logged in show idle time and day logged in, else
5137660Sbostic 	 *		show last login date and time.  If > 6 moths,
5237660Sbostic 	 *		show year instead of time.
5337660Sbostic 	 *	office location
5437660Sbostic 	 *	office phone
5537660Sbostic 	 */
5637660Sbostic #define	MAXREALNAME	20
5737660Sbostic 	(void)printf("%-*s %-*s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
5845687Sbostic 	    "Name", "Tty  Idle  Login Time   Office     Office Phone");
5937660Sbostic 	for (cnt = 0; cnt < entries; ++cnt) {
6037660Sbostic 		pn = list[cnt];
6137664Sedward 		for (w = pn->whead; w != NULL; w = w->next) {
6237664Sedward 			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
6337664Sedward 			    pn->name, MAXREALNAME, MAXREALNAME,
6437664Sedward 			    pn->realname ? pn->realname : "");
6537664Sedward 			if (!w->loginat) {
6637664Sedward 				(void)printf("  *     *  No logins   ");
6737664Sedward 				goto office;
6837664Sedward 			}
6937664Sedward 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
7037664Sedward 			    '*' : ' ');
7137664Sedward 			if (*w->tty)
7237664Sedward 				(void)printf("%-2.2s ",
7337664Sedward 				    w->tty[0] != 't' || w->tty[1] != 't' ||
7437664Sedward 				    w->tty[2] != 'y' ? w->tty : w->tty + 3);
7537664Sedward 			else
7637664Sedward 				(void)printf("   ");
7737664Sedward 			if (w->info == LOGGEDIN) {
7837664Sedward 				stimeprint(w);
7937664Sedward 				(void)printf("  ");
8037664Sedward 			} else
8137664Sedward 				(void)printf("    *  ");
8237664Sedward 			p = ctime(&w->loginat);
8337664Sedward 			(void)printf("%.6s", p + 4);
8437664Sedward 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
8537664Sedward 				(void)printf("  %.4s", p + 20);
8637664Sedward 			else
8737664Sedward 				(void)printf(" %.5s", p + 11);
8837664Sedward office:			if (pn->office)
8938052Sbostic 				(void)printf(" %-10.10s", pn->office);
9037664Sedward 			else if (pn->officephone)
9138052Sbostic 				(void)printf(" %-10.10s", " ");
9237664Sedward 			if (pn->officephone)
9338052Sbostic 				(void)printf(" %-.15s",
9438052Sbostic 				    prphone(pn->officephone));
9537664Sedward 			putchar('\n');
9637660Sbostic 		}
9737660Sbostic 	}
9837660Sbostic }
9937660Sbostic 
10050596Sbostic static PERSON **
10137660Sbostic sort()
10237660Sbostic {
103*50613Sbostic 	register PERSON **lp;
104*50613Sbostic 	register int sflag, r;
10537660Sbostic 	PERSON **list;
106*50613Sbostic 	DBT data, key;
10737660Sbostic 
108*50613Sbostic 	if ((list = malloc((u_int)(entries * sizeof(PERSON *)))) == NULL)
109*50613Sbostic 		err("%s", strerror(errno));
110*50613Sbostic 	for (sflag = R_FIRST, lp = list;; sflag = R_NEXT) {
111*50613Sbostic 		r = (*db->seq)(db, &key, &data, sflag);
112*50613Sbostic 		if (r == -1)
113*50613Sbostic 			err("db seq: %s", strerror(errno));
114*50613Sbostic 		if (r == 1)
115*50613Sbostic 			break;
116*50613Sbostic 		*lp++ = *(PERSON **)data.data;
11737660Sbostic 	}
11837660Sbostic 	(void)qsort(list, entries, sizeof(PERSON *), psort);
11937660Sbostic 	return(list);
12037660Sbostic }
12137660Sbostic 
12250596Sbostic 
12350596Sbostic psort(a, b)
12450596Sbostic 	const void *a, *b;
12537660Sbostic {
126*50613Sbostic 	return(strcmp((*(PERSON **)a)->name, (*(PERSON **)b)->name));
12737660Sbostic }
12837660Sbostic 
12950596Sbostic static void
13037664Sedward stimeprint(w)
13137664Sedward 	WHERE *w;
13237660Sbostic {
13337660Sbostic 	register struct tm *delta;
13437660Sbostic 
13537664Sedward 	delta = gmtime(&w->idletime);
13637660Sbostic 	if (!delta->tm_yday)
13737660Sbostic 		if (!delta->tm_hour)
13837660Sbostic 			if (!delta->tm_min)
13937660Sbostic 				(void)printf("     ");
14037660Sbostic 			else
14137660Sbostic 				(void)printf("%5d", delta->tm_min);
14237660Sbostic 		else
14337660Sbostic 			(void)printf("%2d:%02d",
14437660Sbostic 			    delta->tm_hour, delta->tm_min);
14537660Sbostic 	else
14637660Sbostic 		(void)printf("%4dd", delta->tm_yday);
14737660Sbostic }
148