xref: /csrg-svn/usr.bin/finger/sprint.c (revision 50596)
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*50596Sbostic static char sccsid[] = "@(#)sprint.c	5.9 (Berkeley) 07/27/91";
1337660Sbostic #endif /* not lint */
1437660Sbostic 
1537660Sbostic #include <sys/types.h>
1637660Sbostic #include <sys/time.h>
17*50596Sbostic #include <time.h>
1837660Sbostic #include <tzfile.h>
19*50596Sbostic #include <pwd.h>
20*50596Sbostic #include <utmp.h>
2137660Sbostic #include <stdio.h>
22*50596Sbostic #include <stdlib.h>
23*50596Sbostic #include <string.h>
2437660Sbostic #include "finger.h"
2537660Sbostic 
26*50596Sbostic static int	  psort __P((const void *, const void *));
27*50596Sbostic static PERSON	**sort __P((void));
28*50596Sbostic static void	  stimeprint __P((WHERE *));
2937660Sbostic 
30*50596Sbostic void
3137660Sbostic sflag_print()
3237660Sbostic {
3337660Sbostic 	extern time_t now;
3437660Sbostic 	register PERSON *pn;
3537664Sedward 	register WHERE *w;
3637660Sbostic 	register int cnt;
3737660Sbostic 	register char *p;
38*50596Sbostic 	PERSON **list;
3937660Sbostic 
4037660Sbostic 	list = sort();
4137660Sbostic 	/*
4237660Sbostic 	 * short format --
4337660Sbostic 	 *	login name
4437660Sbostic 	 *	real name
4537660Sbostic 	 *	terminal name (the XX of ttyXX)
4637660Sbostic 	 *	if terminal writeable (add an '*' to the terminal name
4737660Sbostic 	 *		if not)
4837660Sbostic 	 *	if logged in show idle time and day logged in, else
4937660Sbostic 	 *		show last login date and time.  If > 6 moths,
5037660Sbostic 	 *		show year instead of time.
5137660Sbostic 	 *	office location
5237660Sbostic 	 *	office phone
5337660Sbostic 	 */
5437660Sbostic #define	MAXREALNAME	20
5537660Sbostic 	(void)printf("%-*s %-*s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
5645687Sbostic 	    "Name", "Tty  Idle  Login Time   Office     Office Phone");
5737660Sbostic 	for (cnt = 0; cnt < entries; ++cnt) {
5837660Sbostic 		pn = list[cnt];
5937664Sedward 		for (w = pn->whead; w != NULL; w = w->next) {
6037664Sedward 			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
6137664Sedward 			    pn->name, MAXREALNAME, MAXREALNAME,
6237664Sedward 			    pn->realname ? pn->realname : "");
6337664Sedward 			if (!w->loginat) {
6437664Sedward 				(void)printf("  *     *  No logins   ");
6537664Sedward 				goto office;
6637664Sedward 			}
6737664Sedward 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
6837664Sedward 			    '*' : ' ');
6937664Sedward 			if (*w->tty)
7037664Sedward 				(void)printf("%-2.2s ",
7137664Sedward 				    w->tty[0] != 't' || w->tty[1] != 't' ||
7237664Sedward 				    w->tty[2] != 'y' ? w->tty : w->tty + 3);
7337664Sedward 			else
7437664Sedward 				(void)printf("   ");
7537664Sedward 			if (w->info == LOGGEDIN) {
7637664Sedward 				stimeprint(w);
7737664Sedward 				(void)printf("  ");
7837664Sedward 			} else
7937664Sedward 				(void)printf("    *  ");
8037664Sedward 			p = ctime(&w->loginat);
8137664Sedward 			(void)printf("%.6s", p + 4);
8237664Sedward 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
8337664Sedward 				(void)printf("  %.4s", p + 20);
8437664Sedward 			else
8537664Sedward 				(void)printf(" %.5s", p + 11);
8637664Sedward office:			if (pn->office)
8738052Sbostic 				(void)printf(" %-10.10s", pn->office);
8837664Sedward 			else if (pn->officephone)
8938052Sbostic 				(void)printf(" %-10.10s", " ");
9037664Sedward 			if (pn->officephone)
9138052Sbostic 				(void)printf(" %-.15s",
9238052Sbostic 				    prphone(pn->officephone));
9337664Sedward 			putchar('\n');
9437660Sbostic 		}
9537660Sbostic 	}
9637660Sbostic }
9737660Sbostic 
98*50596Sbostic static PERSON **
9937660Sbostic sort()
10037660Sbostic {
10137664Sedward 	register PERSON *pn, **lp;
10237660Sbostic 	PERSON **list;
10337660Sbostic 
10437660Sbostic 	if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *))))) {
10537660Sbostic 		(void)fprintf(stderr, "finger: out of space.\n");
10637660Sbostic 		exit(1);
10737660Sbostic 	}
10837664Sedward 	for (lp = list, pn = phead; pn != NULL; pn = pn->next)
10937664Sedward 		*lp++ = pn;
11037660Sbostic 	(void)qsort(list, entries, sizeof(PERSON *), psort);
11137660Sbostic 	return(list);
11237660Sbostic }
11337660Sbostic 
114*50596Sbostic 
115*50596Sbostic psort(a, b)
116*50596Sbostic 	const void *a, *b;
11737660Sbostic {
118*50596Sbostic 	return(strcmp(((PERSON *)a)->name, ((PERSON *)b)->name));
11937660Sbostic }
12037660Sbostic 
121*50596Sbostic static void
12237664Sedward stimeprint(w)
12337664Sedward 	WHERE *w;
12437660Sbostic {
12537660Sbostic 	register struct tm *delta;
12637660Sbostic 
12737664Sedward 	delta = gmtime(&w->idletime);
12837660Sbostic 	if (!delta->tm_yday)
12937660Sbostic 		if (!delta->tm_hour)
13037660Sbostic 			if (!delta->tm_min)
13137660Sbostic 				(void)printf("     ");
13237660Sbostic 			else
13337660Sbostic 				(void)printf("%5d", delta->tm_min);
13437660Sbostic 		else
13537660Sbostic 			(void)printf("%2d:%02d",
13637660Sbostic 			    delta->tm_hour, delta->tm_min);
13737660Sbostic 	else
13837660Sbostic 		(void)printf("%4dd", delta->tm_yday);
13937660Sbostic }
140