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