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