xref: /freebsd/usr.bin/finger/lprint.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include <ctype.h>
40 #include <db.h>
41 #include <err.h>
42 #include <fcntl.h>
43 #include <langinfo.h>
44 #include <paths.h>
45 #include <pwd.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <utmpx.h>
50 #include "finger.h"
51 #include "pathnames.h"
52 
53 #define	LINE_LEN	80
54 #define	TAB_LEN		8		/* 8 spaces between tabs */
55 
56 static int	demi_print(char *, int);
57 static void	lprint(PERSON *);
58 static void     vputc(unsigned char);
59 
60 void
61 lflag_print(void)
62 {
63 	PERSON *pn;
64 	int sflag, r;
65 	PERSON *tmp;
66 	DBT data, key;
67 
68 	for (sflag = R_FIRST;; sflag = R_NEXT) {
69 		r = (*db->seq)(db, &key, &data, sflag);
70 		if (r == -1)
71 			err(1, "db seq");
72 		if (r == 1)
73 			break;
74 		memmove(&tmp, data.data, sizeof tmp);
75 		pn = tmp;
76 		if (sflag != R_FIRST)
77 			putchar('\n');
78 		lprint(pn);
79 		if (!pplan) {
80 			(void)show_text(pn->dir,
81 			    _PATH_FORWARD, "Mail forwarded to");
82 			(void)show_text(pn->dir, _PATH_PROJECT, "Project");
83 			if (!show_text(pn->dir, _PATH_PLAN, "Plan"))
84 				(void)printf("No Plan.\n");
85 			(void)show_text(pn->dir,
86 			    _PATH_PUBKEY, "Public key");
87 		}
88 	}
89 }
90 
91 static void
92 lprint(PERSON *pn)
93 {
94 	struct tm *delta;
95 	WHERE *w;
96 	int cpr, len, maxlen;
97 	struct tm *tp;
98 	int oddfield;
99 	char t[80];
100 
101 	if (d_first < 0)
102 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
103 	/*
104 	 * long format --
105 	 *	login name
106 	 *	real name
107 	 *	home directory
108 	 *	shell
109 	 *	office, office phone, home phone if available
110 	 *	mail status
111 	 */
112 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
113 	    pn->name, pn->realname, pn->dir);
114 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
115 
116 	if (gflag)
117 		goto no_gecos;
118 	/*
119 	 * try and print office, office phone, and home phone on one line;
120 	 * if that fails, do line filling so it looks nice.
121 	 */
122 #define	OFFICE_TAG		"Office"
123 #define	OFFICE_PHONE_TAG	"Office Phone"
124 	oddfield = 0;
125 	if (pn->office && pn->officephone &&
126 	    strlen(pn->office) + strlen(pn->officephone) +
127 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
128 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
129 		    OFFICE_TAG, pn->office, prphone(pn->officephone));
130 		oddfield = demi_print(tbuf, oddfield);
131 	} else {
132 		if (pn->office) {
133 			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
134 			    OFFICE_TAG, pn->office);
135 			oddfield = demi_print(tbuf, oddfield);
136 		}
137 		if (pn->officephone) {
138 			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
139 			    OFFICE_PHONE_TAG, prphone(pn->officephone));
140 			oddfield = demi_print(tbuf, oddfield);
141 		}
142 	}
143 	if (pn->homephone) {
144 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
145 		    prphone(pn->homephone));
146 		oddfield = demi_print(tbuf, oddfield);
147 	}
148 	if (oddfield)
149 		putchar('\n');
150 
151 no_gecos:
152 	/*
153 	 * long format con't:
154 	 * if logged in
155 	 *	terminal
156 	 *	idle time
157 	 *	if messages allowed
158 	 *	where logged in from
159 	 * if not logged in
160 	 *	when last logged in
161 	 */
162 	/* find out longest device name for this user for formatting */
163 	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
164 		if ((len = strlen(w->tty)) > maxlen)
165 			maxlen = len;
166 	/* find rest of entries for user */
167 	for (w = pn->whead; w != NULL; w = w->next) {
168 		if (w->info == LOGGEDIN) {
169 			tp = localtime(&w->loginat);
170 			strftime(t, sizeof(t),
171 			    d_first ? "%a %e %b %R (%Z)" : "%a %b %e %R (%Z)",
172 			    tp);
173 			cpr = printf("On since %s on %s", t, w->tty);
174 			/*
175 			 * idle time is tough; if have one, print a comma,
176 			 * then spaces to pad out the device name, then the
177 			 * idle time.  Follow with a comma if a remote login.
178 			 */
179 			delta = gmtime(&w->idletime);
180 			if (w->idletime != -1 && (delta->tm_yday ||
181 			    delta->tm_hour || delta->tm_min)) {
182 				cpr += printf("%-*s idle ",
183 				    maxlen - (int)strlen(w->tty) + 1, ",");
184 				if (delta->tm_yday > 0) {
185 					cpr += printf("%d day%s ",
186 					   delta->tm_yday,
187 					   delta->tm_yday == 1 ? "" : "s");
188 				}
189 				cpr += printf("%d:%02d",
190 				    delta->tm_hour, delta->tm_min);
191 				if (*w->host) {
192 					putchar(',');
193 					++cpr;
194 				}
195 			}
196 			if (!w->writable)
197 				cpr += printf(" (messages off)");
198 		} else if (w->loginat == 0) {
199 			cpr = printf("Never logged in.");
200 		} else {
201 			tp = localtime(&w->loginat);
202 			if (now - w->loginat > 86400 * 365 / 2) {
203 				strftime(t, sizeof(t),
204 					 d_first ? "%a %e %b %R %Y (%Z)" :
205 						   "%a %b %e %R %Y (%Z)",
206 					 tp);
207 			} else {
208 				strftime(t, sizeof(t),
209 					 d_first ? "%a %e %b %R (%Z)" :
210 						   "%a %b %e %R (%Z)",
211 					 tp);
212 			}
213 			cpr = printf("Last login %s on %s", t, w->tty);
214 		}
215 		if (*w->host) {
216 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
217 				(void)printf("\n   ");
218 			(void)printf(" from %s", w->host);
219 		}
220 		putchar('\n');
221 	}
222 	if (pn->mailrecv == -1)
223 		printf("No Mail.\n");
224 	else if (pn->mailrecv > pn->mailread) {
225 		tp = localtime(&pn->mailrecv);
226 		strftime(t, sizeof(t),
227 			 d_first ? "%a %e %b %R %Y (%Z)" :
228 				   "%a %b %e %R %Y (%Z)",
229 			 tp);
230 		printf("New mail received %s\n", t);
231 		tp = localtime(&pn->mailread);
232 		strftime(t, sizeof(t),
233 			 d_first ? "%a %e %b %R %Y (%Z)" :
234 				   "%a %b %e %R %Y (%Z)",
235 			 tp);
236 		printf("     Unread since %s\n", t);
237 	} else {
238 		tp = localtime(&pn->mailread);
239 		strftime(t, sizeof(t),
240 			 d_first ? "%a %e %b %R %Y (%Z)" :
241 				   "%a %b %e %R %Y (%Z)",
242 			 tp);
243 		printf("Mail last read %s\n", t);
244 	}
245 }
246 
247 static int
248 demi_print(char *str, 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 int
286 show_text(const char *directory, const char *file_name, const char *header)
287 {
288 	struct stat sb;
289 	FILE *fp;
290 	int ch, cnt;
291 	char *p, lastc;
292 	int fd, nr;
293 
294 	lastc = '\0';
295 
296 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
297 	if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
298 	    sb.st_size == 0)
299 		return(0);
300 
301 	/* If short enough, and no newlines, show it on a single line.*/
302 	if (sb.st_size <= (off_t)(LINE_LEN - strlen(header) - 5)) {
303 		nr = read(fd, tbuf, sizeof(tbuf));
304 		if (nr <= 0) {
305 			(void)close(fd);
306 			return(0);
307 		}
308 		for (p = tbuf, cnt = nr; cnt--; ++p)
309 			if (*p == '\n')
310 				break;
311 		if (cnt <= 1) {
312 			if (*header != '\0')
313 				(void)printf("%s: ", header);
314 			for (p = tbuf, cnt = nr; cnt--; ++p)
315 				if (*p != '\r')
316 					vputc(lastc = *p);
317 			if (lastc != '\n')
318 				(void)putchar('\n');
319 			(void)close(fd);
320 			return(1);
321 		}
322 		else
323 			(void)lseek(fd, 0L, SEEK_SET);
324 	}
325 	if ((fp = fdopen(fd, "r")) == NULL)
326 		return(0);
327 	if (*header != '\0')
328 		(void)printf("%s:\n", header);
329 	while ((ch = getc(fp)) != EOF)
330 		if (ch != '\r')
331 			vputc(lastc = ch);
332 	if (lastc != '\n')
333 		(void)putchar('\n');
334 	(void)fclose(fp);
335 	return(1);
336 }
337 
338 static void
339 vputc(unsigned char ch)
340 {
341 	int meta;
342 
343 	if (!isprint(ch) && !isascii(ch)) {
344 		(void)putchar('M');
345 		(void)putchar('-');
346 		ch = toascii(ch);
347 		meta = 1;
348 	} else
349 		meta = 0;
350 	if (isprint(ch) || (!meta && (ch == ' ' || ch == '\t' || ch == '\n')))
351 		(void)putchar(ch);
352 	else {
353 		(void)putchar('^');
354 		(void)putchar(ch == '\177' ? '?' : ch | 0100);
355 	}
356 }
357