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