xref: /freebsd/usr.bin/finger/lprint.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
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 	 *	mail status
117 	 */
118 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
119 	    pn->name, pn->realname, pn->dir);
120 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
121 
122 	/*
123 	 * try and print office, office phone, and home phone on one line;
124 	 * if that fails, do line filling so it looks nice.
125 	 */
126 #define	OFFICE_TAG		"Office"
127 #define	OFFICE_PHONE_TAG	"Office Phone"
128 	oddfield = 0;
129 	if (pn->office && pn->officephone &&
130 	    strlen(pn->office) + strlen(pn->officephone) +
131 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
132 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
133 		    OFFICE_TAG, pn->office, prphone(pn->officephone));
134 		oddfield = demi_print(tbuf, oddfield);
135 	} else {
136 		if (pn->office) {
137 			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
138 			    OFFICE_TAG, pn->office);
139 			oddfield = demi_print(tbuf, oddfield);
140 		}
141 		if (pn->officephone) {
142 			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
143 			    OFFICE_PHONE_TAG, prphone(pn->officephone));
144 			oddfield = demi_print(tbuf, oddfield);
145 		}
146 	}
147 	if (pn->homephone) {
148 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
149 		    prphone(pn->homephone));
150 		oddfield = demi_print(tbuf, oddfield);
151 	}
152 	if (oddfield)
153 		putchar('\n');
154 
155 	/*
156 	 * long format con't:
157 	 * if logged in
158 	 *	terminal
159 	 *	idle time
160 	 *	if messages allowed
161 	 *	where logged in from
162 	 * if not logged in
163 	 *	when last logged in
164 	 */
165 	/* find out longest device name for this user for formatting */
166 	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
167 		if ((len = strlen(w->tty)) > maxlen)
168 			maxlen = len;
169 	/* find rest of entries for user */
170 	for (w = pn->whead; w != NULL; w = w->next) {
171 		switch (w->info) {
172 		case LOGGEDIN:
173 			tp = localtime(&w->loginat);
174 			t = asctime(tp);
175 			tzn = tp->tm_zone;
176 			cpr = printf("On since %.16s (%s) on %s",
177 			    t, tzn, w->tty);
178 			/*
179 			 * idle time is tough; if have one, print a comma,
180 			 * then spaces to pad out the device name, then the
181 			 * idle time.  Follow with a comma if a remote login.
182 			 */
183 			delta = gmtime(&w->idletime);
184 			if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
185 				cpr += printf("%-*s idle ",
186 				    maxlen - strlen(w->tty) + 1, ",");
187 				if (delta->tm_yday > 0) {
188 					cpr += printf("%d day%s ",
189 					   delta->tm_yday,
190 					   delta->tm_yday == 1 ? "" : "s");
191 				}
192 				cpr += printf("%d:%02d",
193 				    delta->tm_hour, delta->tm_min);
194 				if (*w->host) {
195 					putchar(',');
196 					++cpr;
197 				}
198 			}
199 			if (!w->writable)
200 				cpr += printf(" (messages off)");
201 			break;
202 		case LASTLOG:
203 			if (w->loginat == 0) {
204 				(void)printf("Never logged in.");
205 				break;
206 			}
207 			tp = localtime(&w->loginat);
208 			t = asctime(tp);
209 			tzn = tp->tm_zone;
210 			if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
211 				cpr =
212 				    printf("Last login %.16s %.4s (%s) on %s",
213 				    t, t + 20, tzn, w->tty);
214 			else
215 				cpr = printf("Last login %.16s (%s) on %s",
216 				    t, tzn, w->tty);
217 			break;
218 		}
219 		if (*w->host) {
220 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
221 				(void)printf("\n   ");
222 			(void)printf(" from %s", w->host);
223 		}
224 		putchar('\n');
225 	}
226 	if (pn->mailrecv == -1)
227 		printf("No Mail.\n");
228 	else if (pn->mailrecv > pn->mailread) {
229 		tp = localtime(&pn->mailrecv);
230 		t = asctime(tp);
231 		tzn = tp->tm_zone;
232 		printf("New mail received %.16s %.4s (%s)\n", t, t + 20, tzn);
233 		tp = localtime(&pn->mailread);
234 		t = asctime(tp);
235 		tzn = tp->tm_zone;
236 		printf("     Unread since %.16s %.4s (%s)\n", t, t + 20, tzn);
237 	} else {
238 		tp = localtime(&pn->mailread);
239 		t = asctime(tp);
240 		tzn = tp->tm_zone;
241 		printf("Mail last read %.16s %.4s (%s)\n", t, t + 20, tzn);
242 	}
243 }
244 
245 static int
246 demi_print(str, oddfield)
247 	char *str;
248 	int oddfield;
249 {
250 	static int lenlast;
251 	int lenthis, maxlen;
252 
253 	lenthis = strlen(str);
254 	if (oddfield) {
255 		/*
256 		 * We left off on an odd number of fields.  If we haven't
257 		 * crossed the midpoint of the screen, and we have room for
258 		 * the next field, print it on the same line; otherwise,
259 		 * print it on a new line.
260 		 *
261 		 * Note: we insist on having the right hand fields start
262 		 * no less than 5 tabs out.
263 		 */
264 		maxlen = 5 * TAB_LEN;
265 		if (maxlen < lenlast)
266 			maxlen = lenlast;
267 		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
268 		    lenthis) <= LINE_LEN) {
269 			while(lenlast < (4 * TAB_LEN)) {
270 				putchar('\t');
271 				lenlast += TAB_LEN;
272 			}
273 			(void)printf("\t%s\n", str);	/* force one tab */
274 		} else {
275 			(void)printf("\n%s", str);	/* go to next line */
276 			oddfield = !oddfield;	/* this'll be undone below */
277 		}
278 	} else
279 		(void)printf("%s", str);
280 	oddfield = !oddfield;			/* toggle odd/even marker */
281 	lenlast = lenthis;
282 	return(oddfield);
283 }
284 
285 static int
286 show_text(directory, file_name, header)
287 	char *directory, *file_name, *header;
288 {
289 	struct stat sb;
290 	register FILE *fp;
291 	register int ch, cnt, lastc;
292 	register char *p;
293 	int fd, nr;
294 
295 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
296 	if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
297 	    sb.st_size == 0)
298 		return(0);
299 
300 	/* If short enough, and no newlines, show it on a single line.*/
301 	if (sb.st_size <= LINE_LEN - strlen(header) - 5) {
302 		nr = read(fd, tbuf, sizeof(tbuf));
303 		if (nr <= 0) {
304 			(void)close(fd);
305 			return(0);
306 		}
307 		for (p = tbuf, cnt = nr; cnt--; ++p)
308 			if (*p == '\n')
309 				break;
310 		if (cnt <= 1) {
311 			(void)printf("%s: ", header);
312 			for (p = tbuf, cnt = nr; cnt--; ++p)
313 				vputc(lastc = *p);
314 			if (lastc != '\n')
315 				(void)putchar('\n');
316 			(void)close(fd);
317 			return(1);
318 		}
319 		else
320 			(void)lseek(fd, 0L, SEEK_SET);
321 	}
322 	if ((fp = fdopen(fd, "r")) == NULL)
323 		return(0);
324 	(void)printf("%s:\n", header);
325 	while ((ch = getc(fp)) != EOF)
326 		vputc(lastc = ch);
327 	if (lastc != '\n')
328 		(void)putchar('\n');
329 	(void)fclose(fp);
330 	return(1);
331 }
332 
333 static void
334 vputc(ch)
335 	register int ch;
336 {
337 	int meta;
338 
339 	if (!isascii(ch)) {
340 		(void)putchar('M');
341 		(void)putchar('-');
342 		ch = toascii(ch);
343 		meta = 1;
344 	} else
345 		meta = 0;
346 	if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n'))
347 		(void)putchar(ch);
348 	else {
349 		(void)putchar('^');
350 		(void)putchar(ch == '\177' ? '?' : ch | 0100);
351 	}
352 }
353