xref: /csrg-svn/usr.bin/finger/sprint.c (revision 61995)
137660Sbostic /*
2*61995Sbostic  * Copyright (c) 1989, 1993
3*61995Sbostic  *	The Regents of the University of California.  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*61995Sbostic static char sccsid[] = "@(#)sprint.c	8.1 (Berkeley) 06/06/93";
1337660Sbostic #endif /* not lint */
1437660Sbostic 
1537660Sbostic #include <sys/types.h>
1637660Sbostic #include <sys/time.h>
1750596Sbostic #include <time.h>
1837660Sbostic #include <tzfile.h>
1950613Sbostic #include <db.h>
2050596Sbostic #include <pwd.h>
2150613Sbostic #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 void	  stimeprint __P((WHERE *));
2937660Sbostic 
3050596Sbostic void
3137660Sbostic sflag_print()
3237660Sbostic {
3337660Sbostic 	extern time_t now;
3437660Sbostic 	register PERSON *pn;
3537664Sedward 	register WHERE *w;
3651423Sbostic 	register int sflag, r;
3737660Sbostic 	register char *p;
3851423Sbostic 	DBT data, key;
3937660Sbostic 
4037660Sbostic 	/*
4137660Sbostic 	 * short format --
4237660Sbostic 	 *	login name
4337660Sbostic 	 *	real name
4437660Sbostic 	 *	terminal name (the XX of ttyXX)
4537660Sbostic 	 *	if terminal writeable (add an '*' to the terminal name
4637660Sbostic 	 *		if not)
4737660Sbostic 	 *	if logged in show idle time and day logged in, else
4837660Sbostic 	 *		show last login date and time.  If > 6 moths,
4937660Sbostic 	 *		show year instead of time.
5037660Sbostic 	 *	office location
5137660Sbostic 	 *	office phone
5237660Sbostic 	 */
5337660Sbostic #define	MAXREALNAME	20
5437660Sbostic 	(void)printf("%-*s %-*s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
5545687Sbostic 	    "Name", "Tty  Idle  Login Time   Office     Office Phone");
5651423Sbostic 
5751423Sbostic 	for (sflag = R_FIRST;; sflag = R_NEXT) {
5851423Sbostic 		r = (*db->seq)(db, &key, &data, sflag);
5951423Sbostic 		if (r == -1)
6051423Sbostic 			err("db seq: %s", strerror(errno));
6151423Sbostic 		if (r == 1)
6251423Sbostic 			break;
6351423Sbostic 		pn = *(PERSON **)data.data;
6451423Sbostic 
6537664Sedward 		for (w = pn->whead; w != NULL; w = w->next) {
6637664Sedward 			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
6737664Sedward 			    pn->name, MAXREALNAME, MAXREALNAME,
6837664Sedward 			    pn->realname ? pn->realname : "");
6937664Sedward 			if (!w->loginat) {
7037664Sedward 				(void)printf("  *     *  No logins   ");
7137664Sedward 				goto office;
7237664Sedward 			}
7337664Sedward 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
7437664Sedward 			    '*' : ' ');
7537664Sedward 			if (*w->tty)
7637664Sedward 				(void)printf("%-2.2s ",
7737664Sedward 				    w->tty[0] != 't' || w->tty[1] != 't' ||
7837664Sedward 				    w->tty[2] != 'y' ? w->tty : w->tty + 3);
7937664Sedward 			else
8037664Sedward 				(void)printf("   ");
8137664Sedward 			if (w->info == LOGGEDIN) {
8237664Sedward 				stimeprint(w);
8337664Sedward 				(void)printf("  ");
8437664Sedward 			} else
8537664Sedward 				(void)printf("    *  ");
8637664Sedward 			p = ctime(&w->loginat);
8737664Sedward 			(void)printf("%.6s", p + 4);
8837664Sedward 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
8937664Sedward 				(void)printf("  %.4s", p + 20);
9037664Sedward 			else
9137664Sedward 				(void)printf(" %.5s", p + 11);
9237664Sedward office:			if (pn->office)
9338052Sbostic 				(void)printf(" %-10.10s", pn->office);
9437664Sedward 			else if (pn->officephone)
9538052Sbostic 				(void)printf(" %-10.10s", " ");
9637664Sedward 			if (pn->officephone)
9738052Sbostic 				(void)printf(" %-.15s",
9838052Sbostic 				    prphone(pn->officephone));
9937664Sedward 			putchar('\n');
10037660Sbostic 		}
10137660Sbostic 	}
10237660Sbostic }
10337660Sbostic 
10450596Sbostic static void
10537664Sedward stimeprint(w)
10637664Sedward 	WHERE *w;
10737660Sbostic {
10837660Sbostic 	register struct tm *delta;
10937660Sbostic 
11037664Sedward 	delta = gmtime(&w->idletime);
11137660Sbostic 	if (!delta->tm_yday)
11237660Sbostic 		if (!delta->tm_hour)
11337660Sbostic 			if (!delta->tm_min)
11437660Sbostic 				(void)printf("     ");
11537660Sbostic 			else
11637660Sbostic 				(void)printf("%5d", delta->tm_min);
11737660Sbostic 		else
11837660Sbostic 			(void)printf("%2d:%02d",
11937660Sbostic 			    delta->tm_hour, delta->tm_min);
12037660Sbostic 	else
12137660Sbostic 		(void)printf("%4dd", delta->tm_yday);
12237660Sbostic }
123