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