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