xref: /csrg-svn/usr.bin/finger/sprint.c (revision 38052)
137660Sbostic /*
237660Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337660Sbostic  * All rights reserved.
437660Sbostic  *
537660Sbostic  * Redistribution and use in source and binary forms are permitted
637660Sbostic  * provided that the above copyright notice and this paragraph are
737660Sbostic  * duplicated in all such forms and that any documentation,
837660Sbostic  * advertising materials, and other materials related to such
937660Sbostic  * distribution and use acknowledge that the software was developed
1037660Sbostic  * by the University of California, Berkeley.  The name of the
1137660Sbostic  * University may not be used to endorse or promote products derived
1237660Sbostic  * from this software without specific prior written permission.
1337660Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1437660Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1537660Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1637660Sbostic  */
1737660Sbostic 
1837660Sbostic #ifndef lint
19*38052Sbostic static char sccsid[] = "@(#)sprint.c	5.5 (Berkeley) 05/18/89";
2037660Sbostic #endif /* not lint */
2137660Sbostic 
2237660Sbostic #include <sys/types.h>
2337660Sbostic #include <sys/time.h>
2437660Sbostic #include <tzfile.h>
2537660Sbostic #include <stdio.h>
2637660Sbostic #include "finger.h"
2737660Sbostic 
2837660Sbostic extern int entries;
2937660Sbostic 
3037660Sbostic sflag_print()
3137660Sbostic {
3237660Sbostic 	extern time_t now;
3337660Sbostic 	register PERSON *pn;
3437664Sedward 	register WHERE *w;
3537660Sbostic 	register int cnt;
3637660Sbostic 	register char *p;
3737660Sbostic 	PERSON **list, **sort();
3837660Sbostic 	time_t time();
39*38052Sbostic 	char *ctime(), *prphone();
4037660Sbostic 
4137660Sbostic 	list = sort();
4237660Sbostic 	/*
4337660Sbostic 	 * short format --
4437660Sbostic 	 *	login name
4537660Sbostic 	 *	real name
4637660Sbostic 	 *	terminal name (the XX of ttyXX)
4737660Sbostic 	 *	if terminal writeable (add an '*' to the terminal name
4837660Sbostic 	 *		if not)
4937660Sbostic 	 *	if logged in show idle time and day logged in, else
5037660Sbostic 	 *		show last login date and time.  If > 6 moths,
5137660Sbostic 	 *		show year instead of time.
5237660Sbostic 	 *	office location
5337660Sbostic 	 *	office phone
5437660Sbostic 	 */
5537660Sbostic #define	MAXREALNAME	20
5637660Sbostic 	(void)printf("%-*s %-*s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
57*38052Sbostic 	    "Name", "Tty  Idle  Login        Office     Office Phone");
5837660Sbostic 	for (cnt = 0; cnt < entries; ++cnt) {
5937660Sbostic 		pn = list[cnt];
6037664Sedward 		for (w = pn->whead; w != NULL; w = w->next) {
6137664Sedward 			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
6237664Sedward 			    pn->name, MAXREALNAME, MAXREALNAME,
6337664Sedward 			    pn->realname ? pn->realname : "");
6437664Sedward 			if (!w->loginat) {
6537664Sedward 				(void)printf("  *     *  No logins   ");
6637664Sedward 				goto office;
6737664Sedward 			}
6837664Sedward 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
6937664Sedward 			    '*' : ' ');
7037664Sedward 			if (*w->tty)
7137664Sedward 				(void)printf("%-2.2s ",
7237664Sedward 				    w->tty[0] != 't' || w->tty[1] != 't' ||
7337664Sedward 				    w->tty[2] != 'y' ? w->tty : w->tty + 3);
7437664Sedward 			else
7537664Sedward 				(void)printf("   ");
7637664Sedward 			if (w->info == LOGGEDIN) {
7737664Sedward 				stimeprint(w);
7837664Sedward 				(void)printf("  ");
7937664Sedward 			} else
8037664Sedward 				(void)printf("    *  ");
8137664Sedward 			p = ctime(&w->loginat);
8237664Sedward 			(void)printf("%.6s", p + 4);
8337664Sedward 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
8437664Sedward 				(void)printf("  %.4s", p + 20);
8537664Sedward 			else
8637664Sedward 				(void)printf(" %.5s", p + 11);
8737664Sedward office:			if (pn->office)
88*38052Sbostic 				(void)printf(" %-10.10s", pn->office);
8937664Sedward 			else if (pn->officephone)
90*38052Sbostic 				(void)printf(" %-10.10s", " ");
9137664Sedward 			if (pn->officephone)
92*38052Sbostic 				(void)printf(" %-.15s",
93*38052Sbostic 				    prphone(pn->officephone));
9437664Sedward 			putchar('\n');
9537660Sbostic 		}
9637660Sbostic 	}
9737660Sbostic }
9837660Sbostic 
9937660Sbostic PERSON **
10037660Sbostic sort()
10137660Sbostic {
10237664Sedward 	register PERSON *pn, **lp;
10337660Sbostic 	PERSON **list;
10437660Sbostic 	int psort();
10537660Sbostic 	char *malloc();
10637660Sbostic 
10737660Sbostic 	if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *))))) {
10837660Sbostic 		(void)fprintf(stderr, "finger: out of space.\n");
10937660Sbostic 		exit(1);
11037660Sbostic 	}
11137664Sedward 	for (lp = list, pn = phead; pn != NULL; pn = pn->next)
11237664Sedward 		*lp++ = pn;
11337660Sbostic 	(void)qsort(list, entries, sizeof(PERSON *), psort);
11437660Sbostic 	return(list);
11537660Sbostic }
11637660Sbostic 
11737660Sbostic psort(p, t)
11837660Sbostic 	PERSON **p, **t;
11937660Sbostic {
12037660Sbostic 	return(strcmp((*p)->name, (*t)->name));
12137660Sbostic }
12237660Sbostic 
12337664Sedward stimeprint(w)
12437664Sedward 	WHERE *w;
12537660Sbostic {
12637660Sbostic 	register struct tm *delta;
12737660Sbostic 
12837664Sedward 	delta = gmtime(&w->idletime);
12937660Sbostic 	if (!delta->tm_yday)
13037660Sbostic 		if (!delta->tm_hour)
13137660Sbostic 			if (!delta->tm_min)
13237660Sbostic 				(void)printf("     ");
13337660Sbostic 			else
13437660Sbostic 				(void)printf("%5d", delta->tm_min);
13537660Sbostic 		else
13637660Sbostic 			(void)printf("%2d:%02d",
13737660Sbostic 			    delta->tm_hour, delta->tm_min);
13837660Sbostic 	else
13937660Sbostic 		(void)printf("%4dd", delta->tm_yday);
14037660Sbostic }
141