lprint.c revision 99249
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1989, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * This code is derived from software contributed to Berkeley by
61590Srgrimes * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
71590Srgrimes *
81590Srgrimes * Redistribution and use in source and binary forms, with or without
91590Srgrimes * modification, are permitted provided that the following conditions
101590Srgrimes * are met:
111590Srgrimes * 1. Redistributions of source code must retain the above copyright
121590Srgrimes *    notice, this list of conditions and the following disclaimer.
131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141590Srgrimes *    notice, this list of conditions and the following disclaimer in the
151590Srgrimes *    documentation and/or other materials provided with the distribution.
161590Srgrimes * 3. All advertising materials mentioning features or use of this software
171590Srgrimes *    must display the following acknowledgement:
181590Srgrimes *	This product includes software developed by the University of
191590Srgrimes *	California, Berkeley and its contributors.
201590Srgrimes * 4. Neither the name of the University nor the names of its contributors
211590Srgrimes *    may be used to endorse or promote products derived from this software
221590Srgrimes *    without specific prior written permission.
231590Srgrimes *
241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341590Srgrimes * SUCH DAMAGE.
351590Srgrimes */
361590Srgrimes
3787628Sdwmalone#if 0
3887628Sdwmalone#ifndef lint
3987628Sdwmalonestatic char sccsid[] = "@(#)lprint.c	8.3 (Berkeley) 4/28/95";
4087628Sdwmalone#endif
4187628Sdwmalone#endif
4287628Sdwmalone
4387229Smarkm#include <sys/cdefs.h>
4487229Smarkm__FBSDID("$FreeBSD: head/usr.bin/finger/lprint.c 99249 2002-07-02 08:00:20Z mini $");
4587229Smarkm
4691226Sbde#include <sys/types.h>
471590Srgrimes#include <sys/stat.h>
4891226Sbde#include <sys/time.h>
4972109Scharnier#include <ctype.h>
501590Srgrimes#include <db.h>
5123693Speter#include <err.h>
5272109Scharnier#include <fcntl.h>
5374586Sache#include <langinfo.h>
5472109Scharnier#include <paths.h>
551590Srgrimes#include <pwd.h>
561590Srgrimes#include <stdio.h>
571590Srgrimes#include <string.h>
5872109Scharnier#include <unistd.h>
5972109Scharnier#include <utmp.h>
601590Srgrimes#include "finger.h"
6165064Sbrian#include "pathnames.h"
6265064Sbrian#include "extern.h"
631590Srgrimes
641590Srgrimes#define	LINE_LEN	80
651590Srgrimes#define	TAB_LEN		8		/* 8 spaces between tabs */
661590Srgrimes
6792920Simpstatic int	demi_print(char *, int);
6892920Simpstatic void	lprint(PERSON *);
6992920Simpstatic void     vputc(unsigned char);
701590Srgrimes
711590Srgrimesvoid
721590Srgrimeslflag_print()
731590Srgrimes{
741590Srgrimes	extern int pplan;
7587229Smarkm	PERSON *pn;
7687229Smarkm	int sflag, r;
7723693Speter	PERSON *tmp;
781590Srgrimes	DBT data, key;
791590Srgrimes
801590Srgrimes	for (sflag = R_FIRST;; sflag = R_NEXT) {
811590Srgrimes		r = (*db->seq)(db, &key, &data, sflag);
821590Srgrimes		if (r == -1)
8323693Speter			err(1, "db seq");
841590Srgrimes		if (r == 1)
851590Srgrimes			break;
8623693Speter		memmove(&tmp, data.data, sizeof tmp);
8723693Speter		pn = tmp;
881590Srgrimes		if (sflag != R_FIRST)
891590Srgrimes			putchar('\n');
901590Srgrimes		lprint(pn);
911590Srgrimes		if (!pplan) {
921590Srgrimes			(void)show_text(pn->dir,
931590Srgrimes			    _PATH_FORWARD, "Mail forwarded to");
941590Srgrimes			(void)show_text(pn->dir, _PATH_PROJECT, "Project");
951590Srgrimes			if (!show_text(pn->dir, _PATH_PLAN, "Plan"))
961590Srgrimes				(void)printf("No Plan.\n");
9770474Sdes			(void)show_text(pn->dir,
9870655Sdes			    _PATH_PUBKEY, "Public key");
991590Srgrimes		}
1001590Srgrimes	}
1011590Srgrimes}
1021590Srgrimes
1031590Srgrimesstatic void
1041590Srgrimeslprint(pn)
10587229Smarkm	PERSON *pn;
1061590Srgrimes{
1071590Srgrimes	extern time_t now;
10887229Smarkm	struct tm *delta;
10987229Smarkm	WHERE *w;
11087229Smarkm	int cpr, len, maxlen;
1111590Srgrimes	struct tm *tp;
1121590Srgrimes	int oddfield;
1139993Sache	char t[80];
1141590Srgrimes
11574586Sache	if (d_first < 0)
11674586Sache		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
1171590Srgrimes	/*
1181590Srgrimes	 * long format --
1191590Srgrimes	 *	login name
1201590Srgrimes	 *	real name
1211590Srgrimes	 *	home directory
1221590Srgrimes	 *	shell
1231590Srgrimes	 *	office, office phone, home phone if available
1242537Spst	 *	mail status
1251590Srgrimes	 */
1261590Srgrimes	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
1271590Srgrimes	    pn->name, pn->realname, pn->dir);
1281590Srgrimes	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
1291590Srgrimes
13099249Smini	if (gflag)
13199249Smini		goto no_gecos;
1321590Srgrimes	/*
1331590Srgrimes	 * try and print office, office phone, and home phone on one line;
1341590Srgrimes	 * if that fails, do line filling so it looks nice.
1351590Srgrimes	 */
1361590Srgrimes#define	OFFICE_TAG		"Office"
1371590Srgrimes#define	OFFICE_PHONE_TAG	"Office Phone"
1381590Srgrimes	oddfield = 0;
1391590Srgrimes	if (pn->office && pn->officephone &&
1401590Srgrimes	    strlen(pn->office) + strlen(pn->officephone) +
1411590Srgrimes	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
1421590Srgrimes		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
1431590Srgrimes		    OFFICE_TAG, pn->office, prphone(pn->officephone));
1441590Srgrimes		oddfield = demi_print(tbuf, oddfield);
1451590Srgrimes	} else {
1461590Srgrimes		if (pn->office) {
1471590Srgrimes			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
1481590Srgrimes			    OFFICE_TAG, pn->office);
1491590Srgrimes			oddfield = demi_print(tbuf, oddfield);
1501590Srgrimes		}
1511590Srgrimes		if (pn->officephone) {
1521590Srgrimes			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
1531590Srgrimes			    OFFICE_PHONE_TAG, prphone(pn->officephone));
1541590Srgrimes			oddfield = demi_print(tbuf, oddfield);
1551590Srgrimes		}
1561590Srgrimes	}
1571590Srgrimes	if (pn->homephone) {
1581590Srgrimes		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
1591590Srgrimes		    prphone(pn->homephone));
1601590Srgrimes		oddfield = demi_print(tbuf, oddfield);
1611590Srgrimes	}
1621590Srgrimes	if (oddfield)
1631590Srgrimes		putchar('\n');
1641590Srgrimes
16599249Sminino_gecos:
1661590Srgrimes	/*
1672537Spst	 * long format con't:
1682537Spst	 * if logged in
1691590Srgrimes	 *	terminal
1701590Srgrimes	 *	idle time
1711590Srgrimes	 *	if messages allowed
1721590Srgrimes	 *	where logged in from
1731590Srgrimes	 * if not logged in
1741590Srgrimes	 *	when last logged in
1751590Srgrimes	 */
1761590Srgrimes	/* find out longest device name for this user for formatting */
1771590Srgrimes	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
1781590Srgrimes		if ((len = strlen(w->tty)) > maxlen)
1791590Srgrimes			maxlen = len;
1801590Srgrimes	/* find rest of entries for user */
1811590Srgrimes	for (w = pn->whead; w != NULL; w = w->next) {
18267467Sru		if (w->info == LOGGEDIN) {
1831590Srgrimes			tp = localtime(&w->loginat);
18474586Sache			strftime(t, sizeof(t),
18574586Sache			    d_first ? "%a %e %b %R (%Z)" : "%a %b %e %R (%Z)",
18674586Sache			    tp);
18774586Sache			cpr = printf("On since %s on %s", t, w->tty);
1881590Srgrimes			/*
1891590Srgrimes			 * idle time is tough; if have one, print a comma,
1901590Srgrimes			 * then spaces to pad out the device name, then the
1911590Srgrimes			 * idle time.  Follow with a comma if a remote login.
1921590Srgrimes			 */
1931590Srgrimes			delta = gmtime(&w->idletime);
1941590Srgrimes			if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
1951590Srgrimes				cpr += printf("%-*s idle ",
19690390Speter				    maxlen - (int)strlen(w->tty) + 1, ",");
1971590Srgrimes				if (delta->tm_yday > 0) {
1981590Srgrimes					cpr += printf("%d day%s ",
1991590Srgrimes					   delta->tm_yday,
2001590Srgrimes					   delta->tm_yday == 1 ? "" : "s");
2011590Srgrimes				}
2021590Srgrimes				cpr += printf("%d:%02d",
2031590Srgrimes				    delta->tm_hour, delta->tm_min);
2041590Srgrimes				if (*w->host) {
2051590Srgrimes					putchar(',');
2061590Srgrimes					++cpr;
2071590Srgrimes				}
2081590Srgrimes			}
2091590Srgrimes			if (!w->writable)
2101590Srgrimes				cpr += printf(" (messages off)");
21167467Sru		} else if (w->loginat == 0) {
21267467Sru			cpr = printf("Never logged in.");
21367467Sru		} else {
2141590Srgrimes			tp = localtime(&w->loginat);
21574586Sache			if (now - w->loginat > 86400 * 365 / 2) {
21674586Sache				strftime(t, sizeof(t),
21774586Sache					 d_first ? "%a %e %b %R %Y (%Z)" :
21874586Sache						   "%a %b %e %R %Y (%Z)",
21974586Sache					 tp);
22074586Sache			} else {
22174586Sache				strftime(t, sizeof(t),
22274586Sache					 d_first ? "%a %e %b %R (%Z)" :
22374586Sache						   "%a %b %e %R (%Z)",
22474586Sache					 tp);
22574586Sache			}
22674586Sache			cpr = printf("Last login %s on %s", t, w->tty);
2271590Srgrimes		}
2281590Srgrimes		if (*w->host) {
2291590Srgrimes			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
2301590Srgrimes				(void)printf("\n   ");
2311590Srgrimes			(void)printf(" from %s", w->host);
2321590Srgrimes		}
2331590Srgrimes		putchar('\n');
2341590Srgrimes	}
2352537Spst	if (pn->mailrecv == -1)
2362537Spst		printf("No Mail.\n");
2372537Spst	else if (pn->mailrecv > pn->mailread) {
2382537Spst		tp = localtime(&pn->mailrecv);
23974586Sache		strftime(t, sizeof(t),
24074586Sache			 d_first ? "%a %e %b %R %Y (%Z)" :
24174586Sache				   "%a %b %e %R %Y (%Z)",
24274586Sache			 tp);
24374586Sache		printf("New mail received %s\n", t);
2442537Spst		tp = localtime(&pn->mailread);
24574586Sache		strftime(t, sizeof(t),
24674586Sache			 d_first ? "%a %e %b %R %Y (%Z)" :
24774586Sache				   "%a %b %e %R %Y (%Z)",
24874586Sache			 tp);
24974586Sache		printf("     Unread since %s\n", t);
2502537Spst	} else {
2512537Spst		tp = localtime(&pn->mailread);
25274586Sache		strftime(t, sizeof(t),
25374586Sache			 d_first ? "%a %e %b %R %Y (%Z)" :
25474586Sache				   "%a %b %e %R %Y (%Z)",
25574586Sache			 tp);
25674586Sache		printf("Mail last read %s\n", t);
2572537Spst	}
2581590Srgrimes}
2591590Srgrimes
2601590Srgrimesstatic int
2611590Srgrimesdemi_print(str, oddfield)
2621590Srgrimes	char *str;
2631590Srgrimes	int oddfield;
2641590Srgrimes{
2651590Srgrimes	static int lenlast;
2661590Srgrimes	int lenthis, maxlen;
2671590Srgrimes
2681590Srgrimes	lenthis = strlen(str);
2691590Srgrimes	if (oddfield) {
2701590Srgrimes		/*
2711590Srgrimes		 * We left off on an odd number of fields.  If we haven't
2721590Srgrimes		 * crossed the midpoint of the screen, and we have room for
2731590Srgrimes		 * the next field, print it on the same line; otherwise,
2741590Srgrimes		 * print it on a new line.
2751590Srgrimes		 *
2761590Srgrimes		 * Note: we insist on having the right hand fields start
2771590Srgrimes		 * no less than 5 tabs out.
2781590Srgrimes		 */
2791590Srgrimes		maxlen = 5 * TAB_LEN;
2801590Srgrimes		if (maxlen < lenlast)
2811590Srgrimes			maxlen = lenlast;
2821590Srgrimes		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
2831590Srgrimes		    lenthis) <= LINE_LEN) {
2841590Srgrimes			while(lenlast < (4 * TAB_LEN)) {
2851590Srgrimes				putchar('\t');
2861590Srgrimes				lenlast += TAB_LEN;
2871590Srgrimes			}
2881590Srgrimes			(void)printf("\t%s\n", str);	/* force one tab */
2891590Srgrimes		} else {
2901590Srgrimes			(void)printf("\n%s", str);	/* go to next line */
2911590Srgrimes			oddfield = !oddfield;	/* this'll be undone below */
2921590Srgrimes		}
2931590Srgrimes	} else
2941590Srgrimes		(void)printf("%s", str);
2951590Srgrimes	oddfield = !oddfield;			/* toggle odd/even marker */
2961590Srgrimes	lenlast = lenthis;
2971590Srgrimes	return(oddfield);
2981590Srgrimes}
2991590Srgrimes
30065064Sbrianint
3011590Srgrimesshow_text(directory, file_name, header)
30287229Smarkm	const char *directory, *file_name, *header;
3031590Srgrimes{
3041590Srgrimes	struct stat sb;
30587229Smarkm	FILE *fp;
30687229Smarkm	int ch, cnt;
30787229Smarkm	char *p, lastc;
3081590Srgrimes	int fd, nr;
3091590Srgrimes
31067467Sru	lastc = '\0';
31167467Sru
3121590Srgrimes	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
3131590Srgrimes	if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
3141590Srgrimes	    sb.st_size == 0)
3151590Srgrimes		return(0);
3161590Srgrimes
3171590Srgrimes	/* If short enough, and no newlines, show it on a single line.*/
3181590Srgrimes	if (sb.st_size <= LINE_LEN - strlen(header) - 5) {
3191590Srgrimes		nr = read(fd, tbuf, sizeof(tbuf));
3201590Srgrimes		if (nr <= 0) {
3211590Srgrimes			(void)close(fd);
3221590Srgrimes			return(0);
3231590Srgrimes		}
3241590Srgrimes		for (p = tbuf, cnt = nr; cnt--; ++p)
3251590Srgrimes			if (*p == '\n')
3261590Srgrimes				break;
3271590Srgrimes		if (cnt <= 1) {
32865064Sbrian			if (*header != '\0')
32965064Sbrian				(void)printf("%s: ", header);
3301590Srgrimes			for (p = tbuf, cnt = nr; cnt--; ++p)
33123971Sache				if (*p != '\r')
33223971Sache					vputc(lastc = *p);
3331590Srgrimes			if (lastc != '\n')
3341590Srgrimes				(void)putchar('\n');
3351590Srgrimes			(void)close(fd);
3361590Srgrimes			return(1);
3371590Srgrimes		}
3381590Srgrimes		else
3391590Srgrimes			(void)lseek(fd, 0L, SEEK_SET);
3401590Srgrimes	}
3411590Srgrimes	if ((fp = fdopen(fd, "r")) == NULL)
3421590Srgrimes		return(0);
34365064Sbrian	if (*header != '\0')
34465064Sbrian		(void)printf("%s:\n", header);
3451590Srgrimes	while ((ch = getc(fp)) != EOF)
34623971Sache		if (ch != '\r')
34723971Sache			vputc(lastc = ch);
3481590Srgrimes	if (lastc != '\n')
3491590Srgrimes		(void)putchar('\n');
3501590Srgrimes	(void)fclose(fp);
3511590Srgrimes	return(1);
3521590Srgrimes}
3531590Srgrimes
3541590Srgrimesstatic void
3551590Srgrimesvputc(ch)
35687229Smarkm	unsigned char ch;
3571590Srgrimes{
3581590Srgrimes	int meta;
3591590Srgrimes
3609994Sache	if (!isprint(ch) && !isascii(ch)) {
3611590Srgrimes		(void)putchar('M');
3621590Srgrimes		(void)putchar('-');
3631590Srgrimes		ch = toascii(ch);
3641590Srgrimes		meta = 1;
3651590Srgrimes	} else
3661590Srgrimes		meta = 0;
36767467Sru	if (isprint(ch) || (!meta && (ch == ' ' || ch == '\t' || ch == '\n')))
3681590Srgrimes		(void)putchar(ch);
3691590Srgrimes	else {
3701590Srgrimes		(void)putchar('^');
3711590Srgrimes		(void)putchar(ch == '\177' ? '?' : ch | 0100);
3721590Srgrimes	}
3731590Srgrimes}
374