xref: /freebsd/usr.bin/finger/lprint.c (revision 0afa8e065e14bb8fd338d75690e0238c00167d40)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #if 0
36 #ifndef lint
37 static char sccsid[] = "@(#)lprint.c	8.3 (Berkeley) 4/28/95";
38 #endif
39 #endif
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/stat.h>
47 #include <sys/time.h>
48 #include <ctype.h>
49 #include <db.h>
50 #include <err.h>
51 #include <fcntl.h>
52 #include <langinfo.h>
53 #include <paths.h>
54 #include <pwd.h>
55 #include <stdio.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <utmpx.h>
59 #include "finger.h"
60 #include "pathnames.h"
61 
62 #define	LINE_LEN	80
63 #define	TAB_LEN		8		/* 8 spaces between tabs */
64 
65 static int	demi_print(char *, int);
66 static void	lprint(PERSON *);
67 static void     vputc(unsigned char);
68 
69 void
70 lflag_print(void)
71 {
72 	PERSON *pn;
73 	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 
100 static void
101 lprint(PERSON *pn)
102 {
103 	struct tm *delta;
104 	WHERE *w;
105 	int cpr, len, maxlen;
106 	struct tm *tp;
107 	int oddfield;
108 	char t[80];
109 
110 	if (d_first < 0)
111 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
112 	/*
113 	 * long format --
114 	 *	login name
115 	 *	real name
116 	 *	home directory
117 	 *	shell
118 	 *	office, office phone, home phone if available
119 	 *	mail status
120 	 */
121 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
122 	    pn->name, pn->realname, pn->dir);
123 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
124 
125 	if (gflag)
126 		goto no_gecos;
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 no_gecos:
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),
180 			    d_first ? "%a %e %b %R (%Z)" : "%a %b %e %R (%Z)",
181 			    tp);
182 			cpr = printf("On since %s on %s", t, 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 (w->idletime != -1 && (delta->tm_yday ||
190 			    delta->tm_hour || delta->tm_min)) {
191 				cpr += printf("%-*s idle ",
192 				    maxlen - (int)strlen(w->tty) + 1, ",");
193 				if (delta->tm_yday > 0) {
194 					cpr += printf("%d day%s ",
195 					   delta->tm_yday,
196 					   delta->tm_yday == 1 ? "" : "s");
197 				}
198 				cpr += printf("%d:%02d",
199 				    delta->tm_hour, delta->tm_min);
200 				if (*w->host) {
201 					putchar(',');
202 					++cpr;
203 				}
204 			}
205 			if (!w->writable)
206 				cpr += printf(" (messages off)");
207 		} else if (w->loginat == 0) {
208 			cpr = printf("Never logged in.");
209 		} else {
210 			tp = localtime(&w->loginat);
211 			if (now - w->loginat > 86400 * 365 / 2) {
212 				strftime(t, sizeof(t),
213 					 d_first ? "%a %e %b %R %Y (%Z)" :
214 						   "%a %b %e %R %Y (%Z)",
215 					 tp);
216 			} else {
217 				strftime(t, sizeof(t),
218 					 d_first ? "%a %e %b %R (%Z)" :
219 						   "%a %b %e %R (%Z)",
220 					 tp);
221 			}
222 			cpr = printf("Last login %s on %s", t, w->tty);
223 		}
224 		if (*w->host) {
225 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
226 				(void)printf("\n   ");
227 			(void)printf(" from %s", w->host);
228 		}
229 		putchar('\n');
230 	}
231 	if (pn->mailrecv == -1)
232 		printf("No Mail.\n");
233 	else if (pn->mailrecv > pn->mailread) {
234 		tp = localtime(&pn->mailrecv);
235 		strftime(t, sizeof(t),
236 			 d_first ? "%a %e %b %R %Y (%Z)" :
237 				   "%a %b %e %R %Y (%Z)",
238 			 tp);
239 		printf("New mail received %s\n", t);
240 		tp = localtime(&pn->mailread);
241 		strftime(t, sizeof(t),
242 			 d_first ? "%a %e %b %R %Y (%Z)" :
243 				   "%a %b %e %R %Y (%Z)",
244 			 tp);
245 		printf("     Unread since %s\n", t);
246 	} else {
247 		tp = localtime(&pn->mailread);
248 		strftime(t, sizeof(t),
249 			 d_first ? "%a %e %b %R %Y (%Z)" :
250 				   "%a %b %e %R %Y (%Z)",
251 			 tp);
252 		printf("Mail last read %s\n", t);
253 	}
254 }
255 
256 static int
257 demi_print(char *str, 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 
294 int
295 show_text(const char *directory, const char *file_name, const char *header)
296 {
297 	struct stat sb;
298 	FILE *fp;
299 	int ch, cnt;
300 	char *p, lastc;
301 	int fd, nr;
302 
303 	lastc = '\0';
304 
305 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
306 	if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
307 	    sb.st_size == 0)
308 		return(0);
309 
310 	/* If short enough, and no newlines, show it on a single line.*/
311 	if (sb.st_size <= (off_t)(LINE_LEN - strlen(header) - 5)) {
312 		nr = read(fd, tbuf, sizeof(tbuf));
313 		if (nr <= 0) {
314 			(void)close(fd);
315 			return(0);
316 		}
317 		for (p = tbuf, cnt = nr; cnt--; ++p)
318 			if (*p == '\n')
319 				break;
320 		if (cnt <= 1) {
321 			if (*header != '\0')
322 				(void)printf("%s: ", header);
323 			for (p = tbuf, cnt = nr; cnt--; ++p)
324 				if (*p != '\r')
325 					vputc(lastc = *p);
326 			if (lastc != '\n')
327 				(void)putchar('\n');
328 			(void)close(fd);
329 			return(1);
330 		}
331 		else
332 			(void)lseek(fd, 0L, SEEK_SET);
333 	}
334 	if ((fp = fdopen(fd, "r")) == NULL)
335 		return(0);
336 	if (*header != '\0')
337 		(void)printf("%s:\n", header);
338 	while ((ch = getc(fp)) != EOF)
339 		if (ch != '\r')
340 			vputc(lastc = ch);
341 	if (lastc != '\n')
342 		(void)putchar('\n');
343 	(void)fclose(fp);
344 	return(1);
345 }
346 
347 static void
348 vputc(unsigned char ch)
349 {
350 	int meta;
351 
352 	if (!isprint(ch) && !isascii(ch)) {
353 		(void)putchar('M');
354 		(void)putchar('-');
355 		ch = toascii(ch);
356 		meta = 1;
357 	} else
358 		meta = 0;
359 	if (isprint(ch) || (!meta && (ch == ' ' || ch == '\t' || ch == '\n')))
360 		(void)putchar(ch);
361 	else {
362 		(void)putchar('^');
363 		(void)putchar(ch == '\177' ? '?' : ch | 0100);
364 	}
365 }
366