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