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