xref: /csrg-svn/usr.bin/finger/sprint.c (revision 37664)
137660Sbostic /*
237660Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337660Sbostic  * All rights reserved.
437660Sbostic  *
537660Sbostic  * This code is derived from software contributed to Berkeley by
637660Sbostic  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
737660Sbostic  *
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*37664Sedward static char sccsid[] = "@(#)sprint.c	5.2 (Berkeley) 05/07/89";
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;
37*37664Sedward 	register WHERE *w;
3837660Sbostic 	register int cnt;
3937660Sbostic 	register char *p;
4037660Sbostic 	PERSON **list, **sort();
4137660Sbostic 	time_t time();
4237660Sbostic 	char *ctime();
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,
60*37664Sedward 	    "Name", "Tty  Idle  Login        Office      Office Phone");
6137660Sbostic 	for (cnt = 0; cnt < entries; ++cnt) {
6237660Sbostic 		pn = list[cnt];
63*37664Sedward 		for (w = pn->whead; w != NULL; w = w->next) {
64*37664Sedward 			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
65*37664Sedward 			    pn->name, MAXREALNAME, MAXREALNAME,
66*37664Sedward 			    pn->realname ? pn->realname : "");
67*37664Sedward 			if (!w->loginat) {
68*37664Sedward 				(void)printf("  *     *  No logins   ");
69*37664Sedward 				goto office;
70*37664Sedward 			}
71*37664Sedward 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
72*37664Sedward 			    '*' : ' ');
73*37664Sedward 			if (*w->tty)
74*37664Sedward 				(void)printf("%-2.2s ",
75*37664Sedward 				    w->tty[0] != 't' || w->tty[1] != 't' ||
76*37664Sedward 				    w->tty[2] != 'y' ? w->tty : w->tty + 3);
77*37664Sedward 			else
78*37664Sedward 				(void)printf("   ");
79*37664Sedward 			if (w->info == LOGGEDIN) {
80*37664Sedward 				stimeprint(w);
81*37664Sedward 				(void)printf("  ");
82*37664Sedward 			} else
83*37664Sedward 				(void)printf("    *  ");
84*37664Sedward 			p = ctime(&w->loginat);
85*37664Sedward 			(void)printf("%.6s", p + 4);
86*37664Sedward 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
87*37664Sedward 				(void)printf("  %.4s", p + 20);
88*37664Sedward 			else
89*37664Sedward 				(void)printf(" %.5s", p + 11);
90*37664Sedward office:			if (pn->office)
91*37664Sedward 				(void)printf(" %-11.11s", pn->office);
92*37664Sedward 			else if (pn->officephone)
93*37664Sedward 				(void)printf(" %-11.11s", " ");
94*37664Sedward 			if (pn->officephone)
95*37664Sedward 				(void)printf(" %s", pn->officephone);
96*37664Sedward 			putchar('\n');
9737660Sbostic 		}
9837660Sbostic 	}
9937660Sbostic }
10037660Sbostic 
10137660Sbostic PERSON **
10237660Sbostic sort()
10337660Sbostic {
104*37664Sedward 	register PERSON *pn, **lp;
10537660Sbostic 	PERSON **list;
10637660Sbostic 	int psort();
10737660Sbostic 	char *malloc();
10837660Sbostic 
10937660Sbostic 	if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *))))) {
11037660Sbostic 		(void)fprintf(stderr, "finger: out of space.\n");
11137660Sbostic 		exit(1);
11237660Sbostic 	}
113*37664Sedward 	for (lp = list, pn = phead; pn != NULL; pn = pn->next)
114*37664Sedward 		*lp++ = pn;
11537660Sbostic 	(void)qsort(list, entries, sizeof(PERSON *), psort);
11637660Sbostic 	return(list);
11737660Sbostic }
11837660Sbostic 
11937660Sbostic psort(p, t)
12037660Sbostic 	PERSON **p, **t;
12137660Sbostic {
12237660Sbostic 	return(strcmp((*p)->name, (*t)->name));
12337660Sbostic }
12437660Sbostic 
125*37664Sedward stimeprint(w)
126*37664Sedward 	WHERE *w;
12737660Sbostic {
12837660Sbostic 	register struct tm *delta;
12937660Sbostic 
130*37664Sedward 	delta = gmtime(&w->idletime);
13137660Sbostic 	if (!delta->tm_yday)
13237660Sbostic 		if (!delta->tm_hour)
13337660Sbostic 			if (!delta->tm_min)
13437660Sbostic 				(void)printf("     ");
13537660Sbostic 			else
13637660Sbostic 				(void)printf("%5d", delta->tm_min);
13737660Sbostic 		else
13837660Sbostic 			(void)printf("%2d:%02d",
13937660Sbostic 			    delta->tm_hour, delta->tm_min);
14037660Sbostic 	else
14137660Sbostic 		(void)printf("%4dd", delta->tm_yday);
14237660Sbostic }
143