xref: /openbsd-src/usr.bin/finger/lprint.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: lprint.c,v 1.10 2009/10/27 23:59:38 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1989 The Regents of the University of California.
5  * 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/file.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include <tzfile.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <time.h>
43 #include <ctype.h>
44 #include <paths.h>
45 #include <vis.h>
46 #include "finger.h"
47 #include "extern.h"
48 
49 #define	LINE_LEN	80
50 #define	TAB_LEN		8		/* 8 spaces between tabs */
51 #define	_PATH_PLAN	".plan"
52 #define	_PATH_PROJECT	".project"
53 
54 void
55 lflag_print(void)
56 {
57 	PERSON *pn;
58 
59 	for (pn = phead;;) {
60 		lprint(pn);
61 		if (!pplan) {
62 			(void)show_text(pn->dir, _PATH_PROJECT, "Project:");
63 			if (!show_text(pn->dir, _PATH_PLAN, "Plan:"))
64 				(void)printf("No Plan.\n");
65 		}
66 		if (!(pn = pn->next))
67 			break;
68 		putchar('\n');
69 	}
70 }
71 
72 void
73 lprint(PERSON *pn)
74 {
75 	struct tm *delta;
76 	WHERE *w;
77 	int cpr, len, maxlen;
78 	struct tm *tp;
79 	int oddfield;
80 	char *t, *tzn;
81 	struct storage *mem = NULL;
82 
83 	cpr = 0;
84 	/*
85 	 * long format --
86 	 *	login name
87 	 *	real name
88 	 *	home directory
89 	 *	shell
90 	 *	office, office phone, home phone if available
91 	 *	mail status
92 	 */
93 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
94 	    vs(&mem, pn->name), vs(&mem, pn->realname), pn->dir);
95 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
96 
97 	/*
98 	 * try and print office, office phone, and home phone on one line;
99 	 * if that fails, do line filling so it looks nice.
100 	 */
101 #define	OFFICE_TAG		"Office"
102 #define	OFFICE_PHONE_TAG	"Office Phone"
103 	oddfield = 0;
104 	if (pn->office && pn->officephone &&
105 	    strlen(pn->office) + strlen(pn->officephone) +
106 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
107 		(void)snprintf(tbuf, sizeof(tbuf),
108 		    "%s: %s, %s", OFFICE_TAG, vs(&mem, pn->office),
109 		    vs(&mem, prphone(pn->officephone)));
110 		oddfield = demi_print(tbuf, oddfield);
111 	} else {
112 		if (pn->office) {
113 			(void)snprintf(tbuf, sizeof(tbuf),
114 			    "%s: %s", OFFICE_TAG, vs(&mem, pn->office));
115 			oddfield = demi_print(tbuf, oddfield);
116 		}
117 		if (pn->officephone) {
118 			(void)snprintf(tbuf, sizeof(tbuf),
119 			    "%s: %s", OFFICE_PHONE_TAG,
120 			    vs(&mem, prphone(pn->officephone)));
121 			oddfield = demi_print(tbuf, oddfield);
122 		}
123 	}
124 	if (pn->homephone) {
125 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
126 		    vs(&mem, prphone(pn->homephone)));
127 		oddfield = demi_print(tbuf, oddfield);
128 	}
129 	free_storage(mem);
130 	if (oddfield)
131 		putchar('\n');
132 
133 	/*
134 	 * long format con't:
135 	 * if logged in
136 	 *	terminal
137 	 *	idle time
138 	 *	if messages allowed
139 	 *	where logged in from
140 	 * if not logged in
141 	 *	when last logged in
142 	 */
143 	/* find out longest device name for this user for formatting */
144 	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
145 		if ((len = strlen(w->tty)) > maxlen)
146 			maxlen = len;
147 	/* find rest of entries for user */
148 	for (w = pn->whead; w != NULL; w = w->next) {
149 		switch (w->info) {
150 		case LOGGEDIN:
151 			tp = localtime(&w->loginat);
152 			t = asctime(tp);
153 			tzn = tp->tm_zone;
154 			cpr = printf("On since %.16s (%s) on %s",
155 			    t, tzn, w->tty);
156 			/*
157 			 * idle time is tough; if have one, print a comma,
158 			 * then spaces to pad out the device name, then the
159 			 * idle time.  Follow with a comma if a remote login.
160 			 */
161 			delta = gmtime(&w->idletime);
162 			if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
163 				cpr += printf("%-*s idle ",
164 				    (int)(maxlen - strlen(w->tty) + 1), ",");
165 				if (delta->tm_yday > 0) {
166 					cpr += printf("%d day%s ",
167 					   delta->tm_yday,
168 					   delta->tm_yday == 1 ? "" : "s");
169 				}
170 				cpr += printf("%d:%02d",
171 				    delta->tm_hour, delta->tm_min);
172 				if (*w->host) {
173 					putchar(',');
174 					++cpr;
175 				}
176 			}
177 			if (!w->writable)
178 				cpr += printf(" (messages off)");
179 			break;
180 		case LASTLOG:
181 			if (w->loginat == 0) {
182 				(void)printf("Never logged in.");
183 				break;
184 			}
185 			tp = localtime(&w->loginat);
186 			t = asctime(tp);
187 			tzn = tp->tm_zone;
188 			if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
189 				cpr =
190 				    printf("Last login %.16s %.4s (%s) on %s",
191 				    t, t + 20, tzn, w->tty);
192 			else
193 				cpr = printf("Last login %.16s (%s) on %s",
194 				    t, tzn, w->tty);
195 			break;
196 		}
197 		if (*w->host) {
198 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
199 				(void)printf("\n   ");
200 			(void)printf(" from %s", w->host);
201 		}
202 		putchar('\n');
203 	}
204 	if (pn->mailrecv == -1)
205 		printf("No Mail.\n");
206 	else if (pn->mailrecv > pn->mailread) {
207 		tp = localtime(&pn->mailrecv);
208 		t = asctime(tp);
209 		tzn = tp->tm_zone;
210 		printf("New mail received %.16s %.4s (%s)\n", t, t + 20, tzn);
211 		tp = localtime(&pn->mailread);
212 		t = asctime(tp);
213 		tzn = tp->tm_zone;
214 		printf("     Unread since %.16s %.4s (%s)\n", t, t + 20, tzn);
215 	} else {
216 		tp = localtime(&pn->mailread);
217 		t = asctime(tp);
218 		tzn = tp->tm_zone;
219 		printf("Mail last read %.16s %.4s (%s)\n", t, t + 20, tzn);
220 	}
221 }
222 
223 int
224 demi_print(char *str, int oddfield)
225 {
226 	static int lenlast;
227 	int lenthis, maxlen;
228 
229 	lenthis = strlen(str);
230 	if (oddfield) {
231 		/*
232 		 * We left off on an odd number of fields.  If we haven't
233 		 * crossed the midpoint of the screen, and we have room for
234 		 * the next field, print it on the same line; otherwise,
235 		 * print it on a new line.
236 		 *
237 		 * Note: we insist on having the right hand fields start
238 		 * no less than 5 tabs out.
239 		 */
240 		maxlen = 5 * TAB_LEN;
241 		if (maxlen < lenlast)
242 			maxlen = lenlast;
243 		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
244 		    lenthis) <= LINE_LEN) {
245 			while(lenlast < (4 * TAB_LEN)) {
246 				putchar('\t');
247 				lenlast += TAB_LEN;
248 			}
249 			(void)printf("\t%s\n", str);	/* force one tab */
250 		} else {
251 			(void)printf("\n%s", str);	/* go to next line */
252 			oddfield = !oddfield;	/* this'll be undone below */
253 		}
254 	} else
255 		(void)printf("%s", str);
256 	oddfield = !oddfield;			/* toggle odd/even marker */
257 	lenlast = lenthis;
258 	return (oddfield);
259 }
260 
261 int
262 show_text(char *directory, char *file_name, char *header)
263 {
264 	int ch, lastc;
265 	FILE *fp;
266 
267 	lastc = 0;
268 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
269 	if ((fp = fopen(tbuf, "r")) == NULL)
270 		return (0);
271 	(void)printf("%s\n", header);
272 	while ((ch = getc(fp)) != EOF)
273 		vputc(lastc = ch);
274 	if (lastc != '\n')
275 		(void)putchar('\n');
276 	(void)fclose(fp);
277 	return (1);
278 }
279 
280 void
281 vputc(int ch)
282 {
283 	char visout[5], *s2;
284 
285 	ch = toascii(ch);
286 	vis(visout, ch, VIS_SAFE|VIS_NOSLASH, 0);
287 	for (s2 = visout; *s2; s2++)
288 		(void)putchar(*s2);
289 }
290