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