xref: /csrg-svn/usr.bin/finger/sprint.c (revision 40027)
137660Sbostic /*
237660Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337660Sbostic  * All rights reserved.
437660Sbostic  *
5*40027Sbostic  * This code is derived from software contributed to Berkeley by
6*40027Sbostic  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
7*40027Sbostic  *
837660Sbostic  * Redistribution and use in source and binary forms are permitted
937660Sbostic  * provided that the above copyright notice and this paragraph are
1037660Sbostic  * duplicated in all such forms and that any documentation,
1137660Sbostic  * advertising materials, and other materials related to such
1237660Sbostic  * distribution and use acknowledge that the software was developed
1337660Sbostic  * by the University of California, Berkeley.  The name of the
1437660Sbostic  * University may not be used to endorse or promote products derived
1537660Sbostic  * from this software without specific prior written permission.
1637660Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1737660Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1837660Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1937660Sbostic  */
2037660Sbostic 
2137660Sbostic #ifndef lint
22*40027Sbostic static char sccsid[] = "@(#)sprint.c	5.6 (Berkeley) 02/07/90";
2337660Sbostic #endif /* not lint */
2437660Sbostic 
2537660Sbostic #include <sys/types.h>
2637660Sbostic #include <sys/time.h>
2737660Sbostic #include <tzfile.h>
2837660Sbostic #include <stdio.h>
2937660Sbostic #include "finger.h"
3037660Sbostic 
3137660Sbostic extern int entries;
3237660Sbostic 
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;
4037660Sbostic 	PERSON **list, **sort();
4137660Sbostic 	time_t time();
4238052Sbostic 	char *ctime(), *prphone();
4337660Sbostic 
4437660Sbostic 	list = sort();
4537660Sbostic 	/*
4637660Sbostic 	 * short format --
4737660Sbostic 	 *	login name
4837660Sbostic 	 *	real name
4937660Sbostic 	 *	terminal name (the XX of ttyXX)
5037660Sbostic 	 *	if terminal writeable (add an '*' to the terminal name
5137660Sbostic 	 *		if not)
5237660Sbostic 	 *	if logged in show idle time and day logged in, else
5337660Sbostic 	 *		show last login date and time.  If > 6 moths,
5437660Sbostic 	 *		show year instead of time.
5537660Sbostic 	 *	office location
5637660Sbostic 	 *	office phone
5737660Sbostic 	 */
5837660Sbostic #define	MAXREALNAME	20
5937660Sbostic 	(void)printf("%-*s %-*s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
6038052Sbostic 	    "Name", "Tty  Idle  Login        Office     Office Phone");
6137660Sbostic 	for (cnt = 0; cnt < entries; ++cnt) {
6237660Sbostic 		pn = list[cnt];
6337664Sedward 		for (w = pn->whead; w != NULL; w = w->next) {
6437664Sedward 			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
6537664Sedward 			    pn->name, MAXREALNAME, MAXREALNAME,
6637664Sedward 			    pn->realname ? pn->realname : "");
6737664Sedward 			if (!w->loginat) {
6837664Sedward 				(void)printf("  *     *  No logins   ");
6937664Sedward 				goto office;
7037664Sedward 			}
7137664Sedward 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
7237664Sedward 			    '*' : ' ');
7337664Sedward 			if (*w->tty)
7437664Sedward 				(void)printf("%-2.2s ",
7537664Sedward 				    w->tty[0] != 't' || w->tty[1] != 't' ||
7637664Sedward 				    w->tty[2] != 'y' ? w->tty : w->tty + 3);
7737664Sedward 			else
7837664Sedward 				(void)printf("   ");
7937664Sedward 			if (w->info == LOGGEDIN) {
8037664Sedward 				stimeprint(w);
8137664Sedward 				(void)printf("  ");
8237664Sedward 			} else
8337664Sedward 				(void)printf("    *  ");
8437664Sedward 			p = ctime(&w->loginat);
8537664Sedward 			(void)printf("%.6s", p + 4);
8637664Sedward 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
8737664Sedward 				(void)printf("  %.4s", p + 20);
8837664Sedward 			else
8937664Sedward 				(void)printf(" %.5s", p + 11);
9037664Sedward office:			if (pn->office)
9138052Sbostic 				(void)printf(" %-10.10s", pn->office);
9237664Sedward 			else if (pn->officephone)
9338052Sbostic 				(void)printf(" %-10.10s", " ");
9437664Sedward 			if (pn->officephone)
9538052Sbostic 				(void)printf(" %-.15s",
9638052Sbostic 				    prphone(pn->officephone));
9737664Sedward 			putchar('\n');
9837660Sbostic 		}
9937660Sbostic 	}
10037660Sbostic }
10137660Sbostic 
10237660Sbostic PERSON **
10337660Sbostic sort()
10437660Sbostic {
10537664Sedward 	register PERSON *pn, **lp;
10637660Sbostic 	PERSON **list;
10737660Sbostic 	int psort();
10837660Sbostic 	char *malloc();
10937660Sbostic 
11037660Sbostic 	if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *))))) {
11137660Sbostic 		(void)fprintf(stderr, "finger: out of space.\n");
11237660Sbostic 		exit(1);
11337660Sbostic 	}
11437664Sedward 	for (lp = list, pn = phead; pn != NULL; pn = pn->next)
11537664Sedward 		*lp++ = pn;
11637660Sbostic 	(void)qsort(list, entries, sizeof(PERSON *), psort);
11737660Sbostic 	return(list);
11837660Sbostic }
11937660Sbostic 
12037660Sbostic psort(p, t)
12137660Sbostic 	PERSON **p, **t;
12237660Sbostic {
12337660Sbostic 	return(strcmp((*p)->name, (*t)->name));
12437660Sbostic }
12537660Sbostic 
12637664Sedward stimeprint(w)
12737664Sedward 	WHERE *w;
12837660Sbostic {
12937660Sbostic 	register struct tm *delta;
13037660Sbostic 
13137664Sedward 	delta = gmtime(&w->idletime);
13237660Sbostic 	if (!delta->tm_yday)
13337660Sbostic 		if (!delta->tm_hour)
13437660Sbostic 			if (!delta->tm_min)
13537660Sbostic 				(void)printf("     ");
13637660Sbostic 			else
13737660Sbostic 				(void)printf("%5d", delta->tm_min);
13837660Sbostic 		else
13937660Sbostic 			(void)printf("%2d:%02d",
14037660Sbostic 			    delta->tm_hour, delta->tm_min);
14137660Sbostic 	else
14237660Sbostic 		(void)printf("%4dd", delta->tm_yday);
14337660Sbostic }
144