xref: /386bsd/usr/src/usr.bin/finger/sprint.c (revision a2142627)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 static char sccsid[] = "@(#)sprint.c	5.8 (Berkeley) 12/4/90";
39 #endif /* not lint */
40 
41 #include <sys/types.h>
42 #include <sys/time.h>
43 #include <tzfile.h>
44 #include <stdio.h>
45 #include "finger.h"
46 
47 extern int entries;
48 
sflag_print()49 sflag_print()
50 {
51 	extern time_t now;
52 	register PERSON *pn;
53 	register WHERE *w;
54 	register int cnt;
55 	register char *p;
56 	PERSON **list, **sort();
57 	time_t time();
58 	char *ctime(), *prphone();
59 
60 	list = sort();
61 	/*
62 	 * short format --
63 	 *	login name
64 	 *	real name
65 	 *	terminal name (the XX of ttyXX)
66 	 *	if terminal writeable (add an '*' to the terminal name
67 	 *		if not)
68 	 *	if logged in show idle time and day logged in, else
69 	 *		show last login date and time.  If > 6 moths,
70 	 *		show year instead of time.
71 	 *	office location
72 	 *	office phone
73 	 */
74 #define	MAXREALNAME	20
75 	(void)printf("%-*s %-*s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
76 	    "Name", "Tty  Idle  Login Time   Office     Office Phone");
77 	for (cnt = 0; cnt < entries; ++cnt) {
78 		pn = list[cnt];
79 		for (w = pn->whead; w != NULL; w = w->next) {
80 			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
81 			    pn->name, MAXREALNAME, MAXREALNAME,
82 			    pn->realname ? pn->realname : "");
83 			if (!w->loginat) {
84 				(void)printf("  *     *  No logins   ");
85 				goto office;
86 			}
87 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
88 			    '*' : ' ');
89 			if (*w->tty)
90 				(void)printf("%-2.2s ",
91 				    w->tty[0] != 't' || w->tty[1] != 't' ||
92 				    w->tty[2] != 'y' ? w->tty : w->tty + 3);
93 			else
94 				(void)printf("   ");
95 			if (w->info == LOGGEDIN) {
96 				stimeprint(w);
97 				(void)printf("  ");
98 			} else
99 				(void)printf("    *  ");
100 			p = ctime(&w->loginat);
101 			(void)printf("%.6s", p + 4);
102 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
103 				(void)printf("  %.4s", p + 20);
104 			else
105 				(void)printf(" %.5s", p + 11);
106 office:			if (pn->office)
107 				(void)printf(" %-10.10s", pn->office);
108 			else if (pn->officephone)
109 				(void)printf(" %-10.10s", " ");
110 			if (pn->officephone)
111 				(void)printf(" %-.15s",
112 				    prphone(pn->officephone));
113 			putchar('\n');
114 		}
115 	}
116 }
117 
118 PERSON **
sort()119 sort()
120 {
121 	register PERSON *pn, **lp;
122 	PERSON **list;
123 	int psort();
124 	char *malloc();
125 
126 	if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *))))) {
127 		(void)fprintf(stderr, "finger: out of space.\n");
128 		exit(1);
129 	}
130 	for (lp = list, pn = phead; pn != NULL; pn = pn->next)
131 		*lp++ = pn;
132 	(void)qsort(list, entries, sizeof(PERSON *), psort);
133 	return(list);
134 }
135 
psort(p,t)136 psort(p, t)
137 	PERSON **p, **t;
138 {
139 	return(strcmp((*p)->name, (*t)->name));
140 }
141 
stimeprint(w)142 stimeprint(w)
143 	WHERE *w;
144 {
145 	register struct tm *delta;
146 
147 	delta = gmtime(&w->idletime);
148 	if (!delta->tm_yday)
149 		if (!delta->tm_hour)
150 			if (!delta->tm_min)
151 				(void)printf("     ");
152 			else
153 				(void)printf("%5d", delta->tm_min);
154 		else
155 			(void)printf("%2d:%02d",
156 			    delta->tm_hour, delta->tm_min);
157 	else
158 		(void)printf("%4dd", delta->tm_yday);
159 }
160