xref: /freebsd/usr.bin/finger/lprint.c (revision afe61c15161c324a7af299a9b8457aba5afc92db)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  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[] = "@(#)lprint.c	8.1 (Berkeley) 6/6/93";
39 #endif /* not lint */
40 
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <fcntl.h>
45 #include <time.h>
46 #include <tzfile.h>
47 #include <db.h>
48 #include <pwd.h>
49 #include <utmp.h>
50 #include <errno.h>
51 #include <unistd.h>
52 #include <stdio.h>
53 #include <ctype.h>
54 #include <string.h>
55 #include <paths.h>
56 #include "finger.h"
57 
58 #define	LINE_LEN	80
59 #define	TAB_LEN		8		/* 8 spaces between tabs */
60 #define	_PATH_FORWARD	".forward"
61 #define	_PATH_PLAN	".plan"
62 #define	_PATH_PROJECT	".project"
63 
64 static int	demi_print __P((char *, int));
65 static void	lprint __P((PERSON *));
66 static int	show_text __P((char *, char *, char *));
67 static void	vputc __P((int));
68 
69 void
70 lflag_print()
71 {
72 	extern int pplan;
73 	register PERSON *pn;
74 	register int sflag, r;
75 	DBT data, key;
76 
77 	for (sflag = R_FIRST;; sflag = R_NEXT) {
78 		r = (*db->seq)(db, &key, &data, sflag);
79 		if (r == -1)
80 			err("db seq: %s", strerror(errno));
81 		if (r == 1)
82 			break;
83 		pn = *(PERSON **)data.data;
84 		if (sflag != R_FIRST)
85 			putchar('\n');
86 		lprint(pn);
87 		if (!pplan) {
88 			(void)show_text(pn->dir,
89 			    _PATH_FORWARD, "Mail forwarded to");
90 			(void)show_text(pn->dir, _PATH_PROJECT, "Project");
91 			if (!show_text(pn->dir, _PATH_PLAN, "Plan"))
92 				(void)printf("No Plan.\n");
93 		}
94 	}
95 }
96 
97 static void
98 lprint(pn)
99 	register PERSON *pn;
100 {
101 	extern time_t now;
102 	register struct tm *delta;
103 	register WHERE *w;
104 	register int cpr, len, maxlen;
105 	struct tm *tp;
106 	int oddfield;
107 	char *t, *tzn;
108 
109 	/*
110 	 * long format --
111 	 *	login name
112 	 *	real name
113 	 *	home directory
114 	 *	shell
115 	 *	office, office phone, home phone if available
116 	 */
117 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
118 	    pn->name, pn->realname, pn->dir);
119 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
120 
121 	/*
122 	 * try and print office, office phone, and home phone on one line;
123 	 * if that fails, do line filling so it looks nice.
124 	 */
125 #define	OFFICE_TAG		"Office"
126 #define	OFFICE_PHONE_TAG	"Office Phone"
127 	oddfield = 0;
128 	if (pn->office && pn->officephone &&
129 	    strlen(pn->office) + strlen(pn->officephone) +
130 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
131 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
132 		    OFFICE_TAG, pn->office, prphone(pn->officephone));
133 		oddfield = demi_print(tbuf, oddfield);
134 	} else {
135 		if (pn->office) {
136 			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
137 			    OFFICE_TAG, pn->office);
138 			oddfield = demi_print(tbuf, oddfield);
139 		}
140 		if (pn->officephone) {
141 			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
142 			    OFFICE_PHONE_TAG, prphone(pn->officephone));
143 			oddfield = demi_print(tbuf, oddfield);
144 		}
145 	}
146 	if (pn->homephone) {
147 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
148 		    prphone(pn->homephone));
149 		oddfield = demi_print(tbuf, oddfield);
150 	}
151 	if (oddfield)
152 		putchar('\n');
153 
154 	/*
155 	 * long format con't: * if logged in
156 	 *	terminal
157 	 *	idle time
158 	 *	if messages allowed
159 	 *	where logged in from
160 	 * if not logged in
161 	 *	when last logged in
162 	 */
163 	/* find out longest device name for this user for formatting */
164 	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
165 		if ((len = strlen(w->tty)) > maxlen)
166 			maxlen = len;
167 	/* find rest of entries for user */
168 	for (w = pn->whead; w != NULL; w = w->next) {
169 		switch (w->info) {
170 		case LOGGEDIN:
171 			tp = localtime(&w->loginat);
172 			t = asctime(tp);
173 			tzn = tp->tm_zone;
174 			cpr = printf("On since %.16s (%s) on %s",
175 			    t, tzn, w->tty);
176 			/*
177 			 * idle time is tough; if have one, print a comma,
178 			 * then spaces to pad out the device name, then the
179 			 * idle time.  Follow with a comma if a remote login.
180 			 */
181 			delta = gmtime(&w->idletime);
182 			if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
183 				cpr += printf("%-*s idle ",
184 				    maxlen - strlen(w->tty) + 1, ",");
185 				if (delta->tm_yday > 0) {
186 					cpr += printf("%d day%s ",
187 					   delta->tm_yday,
188 					   delta->tm_yday == 1 ? "" : "s");
189 				}
190 				cpr += printf("%d:%02d",
191 				    delta->tm_hour, delta->tm_min);
192 				if (*w->host) {
193 					putchar(',');
194 					++cpr;
195 				}
196 			}
197 			if (!w->writable)
198 				cpr += printf(" (messages off)");
199 			break;
200 		case LASTLOG:
201 			if (w->loginat == 0) {
202 				(void)printf("Never logged in.");
203 				break;
204 			}
205 			tp = localtime(&w->loginat);
206 			t = asctime(tp);
207 			tzn = tp->tm_zone;
208 			if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
209 				cpr =
210 				    printf("Last login %.16s %.4s (%s) on %s",
211 				    t, t + 20, tzn, w->tty);
212 			else
213 				cpr = printf("Last login %.16s (%s) on %s",
214 				    t, tzn, w->tty);
215 			break;
216 		}
217 		if (*w->host) {
218 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
219 				(void)printf("\n   ");
220 			(void)printf(" from %s", w->host);
221 		}
222 		putchar('\n');
223 	}
224 }
225 
226 static int
227 demi_print(str, oddfield)
228 	char *str;
229 	int oddfield;
230 {
231 	static int lenlast;
232 	int lenthis, maxlen;
233 
234 	lenthis = strlen(str);
235 	if (oddfield) {
236 		/*
237 		 * We left off on an odd number of fields.  If we haven't
238 		 * crossed the midpoint of the screen, and we have room for
239 		 * the next field, print it on the same line; otherwise,
240 		 * print it on a new line.
241 		 *
242 		 * Note: we insist on having the right hand fields start
243 		 * no less than 5 tabs out.
244 		 */
245 		maxlen = 5 * TAB_LEN;
246 		if (maxlen < lenlast)
247 			maxlen = lenlast;
248 		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
249 		    lenthis) <= LINE_LEN) {
250 			while(lenlast < (4 * TAB_LEN)) {
251 				putchar('\t');
252 				lenlast += TAB_LEN;
253 			}
254 			(void)printf("\t%s\n", str);	/* force one tab */
255 		} else {
256 			(void)printf("\n%s", str);	/* go to next line */
257 			oddfield = !oddfield;	/* this'll be undone below */
258 		}
259 	} else
260 		(void)printf("%s", str);
261 	oddfield = !oddfield;			/* toggle odd/even marker */
262 	lenlast = lenthis;
263 	return(oddfield);
264 }
265 
266 static int
267 show_text(directory, file_name, header)
268 	char *directory, *file_name, *header;
269 {
270 	struct stat sb;
271 	register FILE *fp;
272 	register int ch, cnt, lastc;
273 	register char *p;
274 	int fd, nr;
275 
276 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
277 	if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
278 	    sb.st_size == 0)
279 		return(0);
280 
281 	/* If short enough, and no newlines, show it on a single line.*/
282 	if (sb.st_size <= LINE_LEN - strlen(header) - 5) {
283 		nr = read(fd, tbuf, sizeof(tbuf));
284 		if (nr <= 0) {
285 			(void)close(fd);
286 			return(0);
287 		}
288 		for (p = tbuf, cnt = nr; cnt--; ++p)
289 			if (*p == '\n')
290 				break;
291 		if (cnt <= 1) {
292 			(void)printf("%s: ", header);
293 			for (p = tbuf, cnt = nr; cnt--; ++p)
294 				vputc(lastc = *p);
295 			if (lastc != '\n')
296 				(void)putchar('\n');
297 			(void)close(fd);
298 			return(1);
299 		}
300 		else
301 			(void)lseek(fd, 0L, SEEK_SET);
302 	}
303 	if ((fp = fdopen(fd, "r")) == NULL)
304 		return(0);
305 	(void)printf("%s:\n", header);
306 	while ((ch = getc(fp)) != EOF)
307 		vputc(lastc = ch);
308 	if (lastc != '\n')
309 		(void)putchar('\n');
310 	(void)fclose(fp);
311 	return(1);
312 }
313 
314 static void
315 vputc(ch)
316 	register int ch;
317 {
318 	int meta;
319 
320 	if (!isascii(ch)) {
321 		(void)putchar('M');
322 		(void)putchar('-');
323 		ch = toascii(ch);
324 		meta = 1;
325 	} else
326 		meta = 0;
327 	if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n'))
328 		(void)putchar(ch);
329 	else {
330 		(void)putchar('^');
331 		(void)putchar(ch == '\177' ? '?' : ch | 0100);
332 	}
333 }
334