xref: /csrg-svn/usr.bin/finger/sprint.c (revision 37660)
1*37660Sbostic /*
2*37660Sbostic  * Copyright (c) 1989 The Regents of the University of California.
3*37660Sbostic  * All rights reserved.
4*37660Sbostic  *
5*37660Sbostic  * This code is derived from software contributed to Berkeley by
6*37660Sbostic  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
7*37660Sbostic  *
8*37660Sbostic  * Redistribution and use in source and binary forms are permitted
9*37660Sbostic  * provided that the above copyright notice and this paragraph are
10*37660Sbostic  * duplicated in all such forms and that any documentation,
11*37660Sbostic  * advertising materials, and other materials related to such
12*37660Sbostic  * distribution and use acknowledge that the software was developed
13*37660Sbostic  * by the University of California, Berkeley.  The name of the
14*37660Sbostic  * University may not be used to endorse or promote products derived
15*37660Sbostic  * from this software without specific prior written permission.
16*37660Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17*37660Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18*37660Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19*37660Sbostic  */
20*37660Sbostic 
21*37660Sbostic #ifndef lint
22*37660Sbostic static char sccsid[] = "@(#)sprint.c	5.1 (Berkeley) 05/06/89";
23*37660Sbostic #endif /* not lint */
24*37660Sbostic 
25*37660Sbostic #include <sys/types.h>
26*37660Sbostic #include <sys/time.h>
27*37660Sbostic #include <tzfile.h>
28*37660Sbostic #include <stdio.h>
29*37660Sbostic #include "finger.h"
30*37660Sbostic 
31*37660Sbostic extern int entries;
32*37660Sbostic 
33*37660Sbostic sflag_print()
34*37660Sbostic {
35*37660Sbostic 	extern time_t now;
36*37660Sbostic 	register PERSON *pn;
37*37660Sbostic 	register int cnt;
38*37660Sbostic 	register char *p;
39*37660Sbostic 	PERSON **list, **sort();
40*37660Sbostic 	time_t time();
41*37660Sbostic 	char *ctime();
42*37660Sbostic 
43*37660Sbostic 	list = sort();
44*37660Sbostic 	/*
45*37660Sbostic 	 * short format --
46*37660Sbostic 	 *	login name
47*37660Sbostic 	 *	real name
48*37660Sbostic 	 *	terminal name (the XX of ttyXX)
49*37660Sbostic 	 *	if terminal writeable (add an '*' to the terminal name
50*37660Sbostic 	 *		if not)
51*37660Sbostic 	 *	if logged in show idle time and day logged in, else
52*37660Sbostic 	 *		show last login date and time.  If > 6 moths,
53*37660Sbostic 	 *		show year instead of time.
54*37660Sbostic 	 *	office location
55*37660Sbostic 	 *	office phone
56*37660Sbostic 	 */
57*37660Sbostic #define	MAXREALNAME	20
58*37660Sbostic 	(void)printf("%-*s %-*s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
59*37660Sbostic 	    "Name", "Tty  Idle Login        Office      Office Phone");
60*37660Sbostic 	for (cnt = 0; cnt < entries; ++cnt) {
61*37660Sbostic 		pn = list[cnt];
62*37660Sbostic 		 (void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
63*37660Sbostic 		    pn->name, MAXREALNAME, MAXREALNAME,
64*37660Sbostic 		    pn->realname ? pn->realname : "");
65*37660Sbostic 		if (!pn->loginat) {
66*37660Sbostic 			(void)printf("          Never logged in\n");
67*37660Sbostic 			continue;
68*37660Sbostic 		}
69*37660Sbostic 		(void)printf(pn->info == LOGGEDIN &&
70*37660Sbostic 		    !pn->writable ? "*" : " ");
71*37660Sbostic 		if (*pn->tty)
72*37660Sbostic 			(void)printf("%-2.2s ",
73*37660Sbostic 			    pn->tty[0] != 't' || pn->tty[1] != 't' ||
74*37660Sbostic 			    pn->tty[2] != 'y' ? pn->tty : pn->tty + 3);
75*37660Sbostic 		else
76*37660Sbostic 			(void)printf("   ");
77*37660Sbostic 		stimeprint(pn);
78*37660Sbostic 		p = ctime(&pn->loginat);
79*37660Sbostic 		(void)printf(" %.6s", p + 4);
80*37660Sbostic 		if (now - pn->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
81*37660Sbostic 			(void)printf(" %.4s ", p + 20);
82*37660Sbostic 		else
83*37660Sbostic 			(void)printf(" %.5s", p + 11);
84*37660Sbostic 		if (pn->office)
85*37660Sbostic 			(void)printf(" %-11.11s", pn->office);
86*37660Sbostic 		else if (pn->officephone)
87*37660Sbostic 			(void)printf(" %-11.11s", " ");
88*37660Sbostic 		if (pn->officephone)
89*37660Sbostic 			(void)printf(" %s", pn->officephone);
90*37660Sbostic 		putchar('\n');
91*37660Sbostic 	}
92*37660Sbostic }
93*37660Sbostic 
94*37660Sbostic PERSON **
95*37660Sbostic sort()
96*37660Sbostic {
97*37660Sbostic 	extern PERSON *head;
98*37660Sbostic 	register PERSON *pn;
99*37660Sbostic 	register int cnt;
100*37660Sbostic 	PERSON **list;
101*37660Sbostic 	int psort();
102*37660Sbostic 	char *malloc();
103*37660Sbostic 
104*37660Sbostic 	if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *))))) {
105*37660Sbostic 		(void)fprintf(stderr, "finger: out of space.\n");
106*37660Sbostic 		exit(1);
107*37660Sbostic 	}
108*37660Sbostic 	for (pn = head, cnt = 0; cnt < entries; pn = pn->next)
109*37660Sbostic 		list[cnt++] = pn;
110*37660Sbostic 	(void)qsort(list, entries, sizeof(PERSON *), psort);
111*37660Sbostic 	return(list);
112*37660Sbostic }
113*37660Sbostic 
114*37660Sbostic psort(p, t)
115*37660Sbostic 	PERSON **p, **t;
116*37660Sbostic {
117*37660Sbostic 	return(strcmp((*p)->name, (*t)->name));
118*37660Sbostic }
119*37660Sbostic 
120*37660Sbostic stimeprint(pn)
121*37660Sbostic 	PERSON *pn;
122*37660Sbostic {
123*37660Sbostic 	register struct tm *delta;
124*37660Sbostic 
125*37660Sbostic 	if (pn->info != LOGGEDIN) {
126*37660Sbostic 		(void)printf("     ");
127*37660Sbostic 		return;
128*37660Sbostic 	}
129*37660Sbostic 	delta = gmtime(&pn->idletime);
130*37660Sbostic 	if (!delta->tm_yday)
131*37660Sbostic 		if (!delta->tm_hour)
132*37660Sbostic 			if (!delta->tm_min)
133*37660Sbostic 				(void)printf("     ");
134*37660Sbostic 			else
135*37660Sbostic 				(void)printf("%5d", delta->tm_min);
136*37660Sbostic 		else
137*37660Sbostic 			(void)printf("%2d:%02d",
138*37660Sbostic 			    delta->tm_hour, delta->tm_min);
139*37660Sbostic 	else
140*37660Sbostic 		(void)printf("%4dd", delta->tm_yday);
141*37660Sbostic }
142