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