xref: /original-bsd/usr.bin/finger/lprint.c (revision 419de4b1)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)lprint.c	5.6 (Berkeley) 09/01/89";
20 #endif /* not lint */
21 
22 #include <sys/types.h>
23 #include <sys/file.h>
24 #include <sys/stat.h>
25 #include <sys/time.h>
26 #include <tzfile.h>
27 #include <stdio.h>
28 #include <ctype.h>
29 #include "finger.h"
30 #include "pathnames.h"
31 
32 #define	LINE_LEN	80
33 #define	TAB_LEN		8		/* 8 spaces between tabs */
34 #define	_PATH_PLAN	".plan"
35 #define	_PATH_PROJECT	".project"
36 
37 lflag_print()
38 {
39 	extern int pplan;
40 	register PERSON *pn;
41 
42 	for (pn = phead;;) {
43 		lprint(pn);
44 		if (!pplan) {
45 			(void)show_text(pn->dir, _PATH_PROJECT, "Project:");
46 			if (!show_text(pn->dir, _PATH_PLAN, "Plan:"))
47 				(void)printf("No Plan.\n");
48 		}
49 		if (!(pn = pn->next))
50 			break;
51 		putchar('\n');
52 	}
53 }
54 
55 lprint(pn)
56 	register PERSON *pn;
57 {
58 	extern time_t now;
59 	register struct tm *delta;
60 	register WHERE *w;
61 	register int cpr, len, maxlen;
62 	int oddfield;
63 	time_t time();
64 	char *t, *ctime(), *prphone();
65 
66 	/*
67 	 * long format --
68 	 *	login name
69 	 *	real name
70 	 *	home directory
71 	 *	shell
72 	 *	office, office phone, home phone if available
73 	 */
74 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
75 	    pn->name, pn->realname, pn->dir);
76 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
77 
78 	/*
79 	 * try and print office, office phone, and home phone on one line;
80 	 * if that fails, do line filling so it looks nice.
81 	 */
82 #define	OFFICE_TAG		"Office"
83 #define	OFFICE_PHONE_TAG	"Office Phone"
84 	oddfield = 0;
85 	if (pn->office && pn->officephone &&
86 	    strlen(pn->office) + strlen(pn->officephone) +
87 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
88 		(void)sprintf(tbuf, "%s: %s, %s", OFFICE_TAG, pn->office,
89 		    prphone(pn->officephone));
90 		oddfield = demi_print(tbuf, oddfield);
91 	} else {
92 		if (pn->office) {
93 			(void)sprintf(tbuf, "%s: %s", OFFICE_TAG, pn->office);
94 			oddfield = demi_print(tbuf, oddfield);
95 		}
96 		if (pn->officephone) {
97 			(void)sprintf(tbuf, "%s: %s", OFFICE_PHONE_TAG,
98 			    prphone(pn->officephone));
99 			oddfield = demi_print(tbuf, oddfield);
100 		}
101 	}
102 	if (pn->homephone) {
103 		(void)sprintf(tbuf, "%s: %s", "Home Phone",
104 		    prphone(pn->homephone));
105 		oddfield = demi_print(tbuf, oddfield);
106 	}
107 	if (oddfield)
108 		putchar('\n');
109 
110 	/*
111 	 * long format con't: * if logged in
112 	 *	terminal
113 	 *	idle time
114 	 *	if messages allowed
115 	 *	where logged in from
116 	 * if not logged in
117 	 *	when last logged in
118 	 */
119 	/* find out longest device name for this user for formatting */
120 	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
121 		if ((len = strlen(w->tty)) > maxlen)
122 			maxlen = len;
123 	/* find rest of entries for user */
124 	for (w = pn->whead; w != NULL; w = w->next) {
125 		switch (w->info) {
126 		case LOGGEDIN:
127 			cpr = printf("On since %16.16s on %s",
128 			    ctime(&w->loginat), w->tty);
129 			/*
130 			 * idle time is tough; if have one, print a comma,
131 			 * then spaces to pad out the device name, then the
132 			 * idle time.  Follow with a comma if a remote login.
133 			 */
134 			delta = gmtime(&w->idletime);
135 			if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
136 				cpr += printf("%-*s idle ",
137 				    maxlen - strlen(w->tty) + 1, ",");
138 				if (delta->tm_yday > 0) {
139 					cpr += printf("%d day%s ",
140 					   delta->tm_yday,
141 					   delta->tm_yday == 1 ? "" : "s");
142 				}
143 				cpr += printf("%d:%02d",
144 				    delta->tm_hour, delta->tm_min);
145 				if (*w->host) {
146 					putchar(',');
147 					++cpr;
148 				}
149 			}
150 			if (!w->writable)
151 				cpr += printf(" (messages off)");
152 			break;
153 		case LASTLOG:
154 			if (w->loginat == 0) {
155 				(void)printf("Never logged in.");
156 				break;
157 			}
158 			t = ctime(&w->loginat);
159 			if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
160 				cpr = printf("Last login %10.10s, %4.4s on %s",
161 				    t, t + 20, w->tty);
162 			else
163 				cpr = printf("Last login %16.16s on %s",
164 					t, w->tty);
165 			break;
166 		}
167 		if (*w->host) {
168 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
169 				(void)printf("\n   ");
170 			(void)printf(" from %s", w->host);
171 		}
172 		putchar('\n');
173 	}
174 }
175 
176 demi_print(str, oddfield)
177 	char *str;
178 	int oddfield;
179 {
180 	static int lenlast;
181 	int lenthis, maxlen;
182 
183 	lenthis = strlen(str);
184 	if (oddfield) {
185 		/*
186 		 * We left off on an odd number of fields.  If we haven't
187 		 * crossed the midpoint of the screen, and we have room for
188 		 * the next field, print it on the same line; otherwise,
189 		 * print it on a new line.
190 		 *
191 		 * Note: we insist on having the right hand fields start
192 		 * no less than 5 tabs out.
193 		 */
194 		maxlen = 5 * TAB_LEN;
195 		if (maxlen < lenlast)
196 			maxlen = lenlast;
197 		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
198 		    lenthis) <= LINE_LEN) {
199 			while(lenlast < (4 * TAB_LEN)) {
200 				putchar('\t');
201 				lenlast += TAB_LEN;
202 			}
203 			(void)printf("\t%s\n", str);	/* force one tab */
204 		} else {
205 			(void)printf("\n%s", str);	/* go to next line */
206 			oddfield = !oddfield;	/* this'll be undone below */
207 		}
208 	} else
209 		(void)printf("%s", str);
210 	oddfield = !oddfield;			/* toggle odd/even marker */
211 	lenlast = lenthis;
212 	return(oddfield);
213 }
214 
215 show_text(directory, file_name, header)
216 	char *directory, *file_name, *header;
217 {
218 	register int ch;
219 
220 	(void)sprintf(tbuf, "%s/%s", directory, file_name);
221 	if (!freopen(tbuf, "r", stdin))
222 		return(0);
223 	(void)printf("%s\n", header);
224 	while ((ch = getchar(fp)) != EOF)
225 		vputc(ch);
226 	if (ch != '\n')
227 		(void)putchar('\n');
228 	return(1);
229 }
230 
231 vputc(ch)
232 	register int ch;
233 {
234 	int meta;
235 
236 	if (!isascii(ch)) {
237 		(void)putchar('M');
238 		(void)putchar('-');
239 		ch = toascii(ch);
240 		meta = 1;
241 	} else
242 		meta = 0;
243 	if (isprint(ch) || !meta && isspace(ch))
244 		(void)putchar(ch);
245 	else {
246 		(void)putchar('^');
247 		(void)putchar(ch == '\177' ? '?' : ch | 0100);
248 	}
249 }
250