xref: /freebsd/usr.bin/finger/lprint.c (revision 3bdf775801b218aa5a89564839405b122f4b233e)
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  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #if 0
34 #ifndef lint
35 static char sccsid[] = "@(#)lprint.c	8.3 (Berkeley) 4/28/95";
36 #endif
37 #endif
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 #include <ctype.h>
47 #include <db.h>
48 #include <err.h>
49 #include <fcntl.h>
50 #include <langinfo.h>
51 #include <paths.h>
52 #include <pwd.h>
53 #include <stdio.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <utmpx.h>
57 #include "finger.h"
58 #include "pathnames.h"
59 
60 #define	LINE_LEN	80
61 #define	TAB_LEN		8		/* 8 spaces between tabs */
62 
63 static int	demi_print(char *, int);
64 static void	lprint(PERSON *);
65 static void     vputc(unsigned char);
66 
67 void
68 lflag_print(void)
69 {
70 	PERSON *pn;
71 	int sflag, r;
72 	PERSON *tmp;
73 	DBT data, key;
74 
75 	for (sflag = R_FIRST;; sflag = R_NEXT) {
76 		r = (*db->seq)(db, &key, &data, sflag);
77 		if (r == -1)
78 			err(1, "db seq");
79 		if (r == 1)
80 			break;
81 		memmove(&tmp, data.data, sizeof tmp);
82 		pn = tmp;
83 		if (sflag != R_FIRST)
84 			putchar('\n');
85 		lprint(pn);
86 		if (!pplan) {
87 			(void)show_text(pn->dir,
88 			    _PATH_FORWARD, "Mail forwarded to");
89 			(void)show_text(pn->dir, _PATH_PROJECT, "Project");
90 			if (!show_text(pn->dir, _PATH_PLAN, "Plan"))
91 				(void)printf("No Plan.\n");
92 			(void)show_text(pn->dir,
93 			    _PATH_PUBKEY, "Public key");
94 		}
95 	}
96 }
97 
98 static void
99 lprint(PERSON *pn)
100 {
101 	struct tm *delta;
102 	WHERE *w;
103 	int cpr, len, maxlen;
104 	struct tm *tp;
105 	int oddfield;
106 	char t[80];
107 
108 	if (d_first < 0)
109 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
110 	/*
111 	 * long format --
112 	 *	login name
113 	 *	real name
114 	 *	home directory
115 	 *	shell
116 	 *	office, office phone, home phone if available
117 	 *	mail status
118 	 */
119 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
120 	    pn->name, pn->realname, pn->dir);
121 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
122 
123 	if (gflag)
124 		goto no_gecos;
125 	/*
126 	 * try and print office, office phone, and home phone on one line;
127 	 * if that fails, do line filling so it looks nice.
128 	 */
129 #define	OFFICE_TAG		"Office"
130 #define	OFFICE_PHONE_TAG	"Office Phone"
131 	oddfield = 0;
132 	if (pn->office && pn->officephone &&
133 	    strlen(pn->office) + strlen(pn->officephone) +
134 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
135 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
136 		    OFFICE_TAG, pn->office, prphone(pn->officephone));
137 		oddfield = demi_print(tbuf, oddfield);
138 	} else {
139 		if (pn->office) {
140 			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
141 			    OFFICE_TAG, pn->office);
142 			oddfield = demi_print(tbuf, oddfield);
143 		}
144 		if (pn->officephone) {
145 			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
146 			    OFFICE_PHONE_TAG, prphone(pn->officephone));
147 			oddfield = demi_print(tbuf, oddfield);
148 		}
149 	}
150 	if (pn->homephone) {
151 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
152 		    prphone(pn->homephone));
153 		oddfield = demi_print(tbuf, oddfield);
154 	}
155 	if (oddfield)
156 		putchar('\n');
157 
158 no_gecos:
159 	/*
160 	 * long format con't:
161 	 * if logged in
162 	 *	terminal
163 	 *	idle time
164 	 *	if messages allowed
165 	 *	where logged in from
166 	 * if not logged in
167 	 *	when last logged in
168 	 */
169 	/* find out longest device name for this user for formatting */
170 	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
171 		if ((len = strlen(w->tty)) > maxlen)
172 			maxlen = len;
173 	/* find rest of entries for user */
174 	for (w = pn->whead; w != NULL; w = w->next) {
175 		if (w->info == LOGGEDIN) {
176 			tp = localtime(&w->loginat);
177 			strftime(t, sizeof(t),
178 			    d_first ? "%a %e %b %R (%Z)" : "%a %b %e %R (%Z)",
179 			    tp);
180 			cpr = printf("On since %s on %s", t, w->tty);
181 			/*
182 			 * idle time is tough; if have one, print a comma,
183 			 * then spaces to pad out the device name, then the
184 			 * idle time.  Follow with a comma if a remote login.
185 			 */
186 			delta = gmtime(&w->idletime);
187 			if (w->idletime != -1 && (delta->tm_yday ||
188 			    delta->tm_hour || delta->tm_min)) {
189 				cpr += printf("%-*s idle ",
190 				    maxlen - (int)strlen(w->tty) + 1, ",");
191 				if (delta->tm_yday > 0) {
192 					cpr += printf("%d day%s ",
193 					   delta->tm_yday,
194 					   delta->tm_yday == 1 ? "" : "s");
195 				}
196 				cpr += printf("%d:%02d",
197 				    delta->tm_hour, delta->tm_min);
198 				if (*w->host) {
199 					putchar(',');
200 					++cpr;
201 				}
202 			}
203 			if (!w->writable)
204 				cpr += printf(" (messages off)");
205 		} else if (w->loginat == 0) {
206 			cpr = printf("Never logged in.");
207 		} else {
208 			tp = localtime(&w->loginat);
209 			if (now - w->loginat > 86400 * 365 / 2) {
210 				strftime(t, sizeof(t),
211 					 d_first ? "%a %e %b %R %Y (%Z)" :
212 						   "%a %b %e %R %Y (%Z)",
213 					 tp);
214 			} else {
215 				strftime(t, sizeof(t),
216 					 d_first ? "%a %e %b %R (%Z)" :
217 						   "%a %b %e %R (%Z)",
218 					 tp);
219 			}
220 			cpr = printf("Last login %s on %s", t, w->tty);
221 		}
222 		if (*w->host) {
223 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
224 				(void)printf("\n   ");
225 			(void)printf(" from %s", w->host);
226 		}
227 		putchar('\n');
228 	}
229 	if (pn->mailrecv == -1)
230 		printf("No Mail.\n");
231 	else if (pn->mailrecv > pn->mailread) {
232 		tp = localtime(&pn->mailrecv);
233 		strftime(t, sizeof(t),
234 			 d_first ? "%a %e %b %R %Y (%Z)" :
235 				   "%a %b %e %R %Y (%Z)",
236 			 tp);
237 		printf("New mail received %s\n", t);
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("     Unread since %s\n", t);
244 	} else {
245 		tp = localtime(&pn->mailread);
246 		strftime(t, sizeof(t),
247 			 d_first ? "%a %e %b %R %Y (%Z)" :
248 				   "%a %b %e %R %Y (%Z)",
249 			 tp);
250 		printf("Mail last read %s\n", t);
251 	}
252 }
253 
254 static int
255 demi_print(char *str, int oddfield)
256 {
257 	static int lenlast;
258 	int lenthis, maxlen;
259 
260 	lenthis = strlen(str);
261 	if (oddfield) {
262 		/*
263 		 * We left off on an odd number of fields.  If we haven't
264 		 * crossed the midpoint of the screen, and we have room for
265 		 * the next field, print it on the same line; otherwise,
266 		 * print it on a new line.
267 		 *
268 		 * Note: we insist on having the right hand fields start
269 		 * no less than 5 tabs out.
270 		 */
271 		maxlen = 5 * TAB_LEN;
272 		if (maxlen < lenlast)
273 			maxlen = lenlast;
274 		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
275 		    lenthis) <= LINE_LEN) {
276 			while(lenlast < (4 * TAB_LEN)) {
277 				putchar('\t');
278 				lenlast += TAB_LEN;
279 			}
280 			(void)printf("\t%s\n", str);	/* force one tab */
281 		} else {
282 			(void)printf("\n%s", str);	/* go to next line */
283 			oddfield = !oddfield;	/* this'll be undone below */
284 		}
285 	} else
286 		(void)printf("%s", str);
287 	oddfield = !oddfield;			/* toggle odd/even marker */
288 	lenlast = lenthis;
289 	return(oddfield);
290 }
291 
292 int
293 show_text(const char *directory, const char *file_name, const char *header)
294 {
295 	struct stat sb;
296 	FILE *fp;
297 	int ch, cnt;
298 	char *p, lastc;
299 	int fd, nr;
300 
301 	lastc = '\0';
302 
303 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
304 	if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
305 	    sb.st_size == 0)
306 		return(0);
307 
308 	/* If short enough, and no newlines, show it on a single line.*/
309 	if (sb.st_size <= (off_t)(LINE_LEN - strlen(header) - 5)) {
310 		nr = read(fd, tbuf, sizeof(tbuf));
311 		if (nr <= 0) {
312 			(void)close(fd);
313 			return(0);
314 		}
315 		for (p = tbuf, cnt = nr; cnt--; ++p)
316 			if (*p == '\n')
317 				break;
318 		if (cnt <= 1) {
319 			if (*header != '\0')
320 				(void)printf("%s: ", header);
321 			for (p = tbuf, cnt = nr; cnt--; ++p)
322 				if (*p != '\r')
323 					vputc(lastc = *p);
324 			if (lastc != '\n')
325 				(void)putchar('\n');
326 			(void)close(fd);
327 			return(1);
328 		}
329 		else
330 			(void)lseek(fd, 0L, SEEK_SET);
331 	}
332 	if ((fp = fdopen(fd, "r")) == NULL)
333 		return(0);
334 	if (*header != '\0')
335 		(void)printf("%s:\n", header);
336 	while ((ch = getc(fp)) != EOF)
337 		if (ch != '\r')
338 			vputc(lastc = ch);
339 	if (lastc != '\n')
340 		(void)putchar('\n');
341 	(void)fclose(fp);
342 	return(1);
343 }
344 
345 static void
346 vputc(unsigned char ch)
347 {
348 	int meta;
349 
350 	if (!isprint(ch) && !isascii(ch)) {
351 		(void)putchar('M');
352 		(void)putchar('-');
353 		ch = toascii(ch);
354 		meta = 1;
355 	} else
356 		meta = 0;
357 	if (isprint(ch) || (!meta && (ch == ' ' || ch == '\t' || ch == '\n')))
358 		(void)putchar(ch);
359 	else {
360 		(void)putchar('^');
361 		(void)putchar(ch == '\177' ? '?' : ch | 0100);
362 	}
363 }
364