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