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