xref: /netbsd-src/usr.bin/finger/sprint.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
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[] = "from: @(#)sprint.c	5.8 (Berkeley) 12/4/90";*/
39 static char rcsid[] = "$Id: sprint.c,v 1.3 1993/10/26 20:54:41 mycroft Exp $";
40 #endif /* not lint */
41 
42 #include <sys/types.h>
43 #include <sys/time.h>
44 #include <tzfile.h>
45 #include <stdio.h>
46 #include "finger.h"
47 
48 extern int entries;
49 
50 sflag_print()
51 {
52 	extern time_t now;
53 	register PERSON *pn;
54 	register WHERE *w;
55 	register int cnt;
56 	register char *p;
57 	PERSON **list, **sort();
58 	time_t time();
59 	char *ctime(), *prphone();
60 
61 	list = sort();
62 	/*
63 	 * short format --
64 	 *	login name
65 	 *	real name
66 	 *	terminal name (the XX of ttyXX)
67 	 *	if terminal writeable (add an '*' to the terminal name
68 	 *		if not)
69 	 *	if logged in show idle time and day logged in, else
70 	 *		show last login date and time.  If > 6 moths,
71 	 *		show year instead of time.
72 	 *	office location
73 	 *	office phone
74 	 */
75 #define	MAXREALNAME	20
76 	(void)printf("%-*s %-*s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
77 	    "Name", "Tty  Idle  Login Time   Office     Office Phone");
78 	for (cnt = 0; cnt < entries; ++cnt) {
79 		pn = list[cnt];
80 		for (w = pn->whead; w != NULL; w = w->next) {
81 			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
82 			    pn->name, MAXREALNAME, MAXREALNAME,
83 			    pn->realname ? pn->realname : "");
84 			if (!w->loginat) {
85 				(void)printf("  *     *  No logins   ");
86 				goto office;
87 			}
88 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
89 			    '*' : ' ');
90 			if (*w->tty)
91 				(void)printf("%-2.2s ",
92 				    w->tty[0] != 't' || w->tty[1] != 't' ||
93 				    w->tty[2] != 'y' ? w->tty : w->tty + 3);
94 			else
95 				(void)printf("   ");
96 			if (w->info == LOGGEDIN) {
97 				stimeprint(w);
98 				(void)printf("  ");
99 			} else
100 				(void)printf("    *  ");
101 			p = ctime(&w->loginat);
102 			(void)printf("%.6s", p + 4);
103 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
104 				(void)printf("  %.4s", p + 20);
105 			else
106 				(void)printf(" %.5s", p + 11);
107 office:			if (pn->office)
108 				(void)printf(" %-10.10s", pn->office);
109 			else if (pn->officephone)
110 				(void)printf(" %-10.10s", " ");
111 			if (pn->officephone)
112 				(void)printf(" %-.15s",
113 				    prphone(pn->officephone));
114 			putchar('\n');
115 		}
116 	}
117 }
118 
119 PERSON **
120 sort()
121 {
122 	register PERSON *pn, **lp;
123 	PERSON **list;
124 	int psort();
125 	char *malloc();
126 
127 	if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *))))) {
128 		(void)fprintf(stderr, "finger: out of space.\n");
129 		exit(1);
130 	}
131 	for (lp = list, pn = phead; pn != NULL; pn = pn->next)
132 		*lp++ = pn;
133 	(void)qsort(list, entries, sizeof(PERSON *), psort);
134 	return(list);
135 }
136 
137 psort(p, t)
138 	PERSON **p, **t;
139 {
140 	return(strcmp((*p)->name, (*t)->name));
141 }
142 
143 stimeprint(w)
144 	WHERE *w;
145 {
146 	register struct tm *delta;
147 
148 	delta = gmtime(&w->idletime);
149 	if (!delta->tm_yday)
150 		if (!delta->tm_hour)
151 			if (!delta->tm_min)
152 				(void)printf("    -");
153 			else
154 				(void)printf("%5d", delta->tm_min);
155 		else
156 			(void)printf("%2d:%02d",
157 			    delta->tm_hour, delta->tm_min);
158 	else
159 		(void)printf("%4dd", delta->tm_yday);
160 }
161