lprint.c revision 1.6 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, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 /*static char sccsid[] = "from: @(#)lprint.c 5.13 (Berkeley) 10/31/90";*/
39 static char rcsid[] = "$Id: lprint.c,v 1.6 1996/11/22 05:34:06 lukem Exp $";
40 #endif /* not lint */
41
42 #include <sys/types.h>
43 #include <sys/file.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 #include <tzfile.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <time.h>
50 #include <ctype.h>
51 #include <paths.h>
52 #include <vis.h>
53 #include "finger.h"
54 #include "extern.h"
55
56 #define LINE_LEN 80
57 #define TAB_LEN 8 /* 8 spaces between tabs */
58 #define _PATH_PLAN ".plan"
59 #define _PATH_PROJECT ".project"
60
61 void
62 lflag_print()
63 {
64 PERSON *pn;
65
66 for (pn = phead;;) {
67 lprint(pn);
68 if (!pplan) {
69 (void)show_text(pn->dir, _PATH_PROJECT, "Project:");
70 if (!show_text(pn->dir, _PATH_PLAN, "Plan:"))
71 (void)printf("No Plan.\n");
72 }
73 if (!(pn = pn->next))
74 break;
75 putchar('\n');
76 }
77 }
78
79 void
80 lprint(pn)
81 PERSON *pn;
82 {
83 struct tm *delta;
84 WHERE *w;
85 int cpr, len, maxlen;
86 struct tm *tp;
87 int oddfield;
88 char *t, *tzn;
89
90 cpr = 0;
91 /*
92 * long format --
93 * login name
94 * real name
95 * home directory
96 * shell
97 * office, office phone, home phone if available
98 * mail status
99 */
100 (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
101 pn->name, pn->realname, pn->dir);
102 (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
103
104 /*
105 * try and print office, office phone, and home phone on one line;
106 * if that fails, do line filling so it looks nice.
107 */
108 #define OFFICE_TAG "Office"
109 #define OFFICE_PHONE_TAG "Office Phone"
110 oddfield = 0;
111 if (pn->office && pn->officephone &&
112 strlen(pn->office) + strlen(pn->officephone) +
113 sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
114 (void)snprintf(tbuf, sizeof(tbuf),
115 "%s: %s, %s", OFFICE_TAG, pn->office,
116 prphone(pn->officephone));
117 oddfield = demi_print(tbuf, oddfield);
118 } else {
119 if (pn->office) {
120 (void)snprintf(tbuf, sizeof(tbuf),
121 "%s: %s", OFFICE_TAG, pn->office);
122 oddfield = demi_print(tbuf, oddfield);
123 }
124 if (pn->officephone) {
125 (void)snprintf(tbuf, sizeof(tbuf),
126 "%s: %s", OFFICE_PHONE_TAG,
127 prphone(pn->officephone));
128 oddfield = demi_print(tbuf, oddfield);
129 }
130 }
131 if (pn->homephone) {
132 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
133 prphone(pn->homephone));
134 oddfield = demi_print(tbuf, oddfield);
135 }
136 if (oddfield)
137 putchar('\n');
138
139 /*
140 * long format con't:
141 * if logged in
142 * terminal
143 * idle time
144 * if messages allowed
145 * where logged in from
146 * if not logged in
147 * when last logged in
148 */
149 /* find out longest device name for this user for formatting */
150 for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
151 if ((len = strlen(w->tty)) > maxlen)
152 maxlen = len;
153 /* find rest of entries for user */
154 for (w = pn->whead; w != NULL; w = w->next) {
155 switch (w->info) {
156 case LOGGEDIN:
157 tp = localtime(&w->loginat);
158 t = asctime(tp);
159 tzn = tp->tm_zone;
160 cpr = printf("On since %.16s (%s) on %s",
161 t, tzn, w->tty);
162 /*
163 * idle time is tough; if have one, print a comma,
164 * then spaces to pad out the device name, then the
165 * idle time. Follow with a comma if a remote login.
166 */
167 delta = gmtime(&w->idletime);
168 if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
169 cpr += printf("%-*s idle ",
170 maxlen - strlen(w->tty) + 1, ",");
171 if (delta->tm_yday > 0) {
172 cpr += printf("%d day%s ",
173 delta->tm_yday,
174 delta->tm_yday == 1 ? "" : "s");
175 }
176 cpr += printf("%d:%02d",
177 delta->tm_hour, delta->tm_min);
178 if (*w->host) {
179 putchar(',');
180 ++cpr;
181 }
182 }
183 if (!w->writable)
184 cpr += printf(" (messages off)");
185 break;
186 case LASTLOG:
187 if (w->loginat == 0) {
188 (void)printf("Never logged in.");
189 break;
190 }
191 tp = localtime(&w->loginat);
192 t = asctime(tp);
193 tzn = tp->tm_zone;
194 if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
195 cpr =
196 printf("Last login %.16s %.4s (%s) on %s",
197 t, t + 20, tzn, w->tty);
198 else
199 cpr = printf("Last login %.16s (%s) on %s",
200 t, tzn, w->tty);
201 break;
202 }
203 if (*w->host) {
204 if (LINE_LEN < (cpr + 6 + strlen(w->host)))
205 (void)printf("\n ");
206 (void)printf(" from %s", w->host);
207 }
208 putchar('\n');
209 }
210 if (pn->mailrecv == -1)
211 printf("No Mail.\n");
212 else if (pn->mailrecv > pn->mailread) {
213 tp = localtime(&pn->mailrecv);
214 t = asctime(tp);
215 tzn = tp->tm_zone;
216 printf("New mail received %.16s %.4s (%s)\n", t, t + 20, tzn);
217 tp = localtime(&pn->mailread);
218 t = asctime(tp);
219 tzn = tp->tm_zone;
220 printf(" Unread since %.16s %.4s (%s)\n", t, t + 20, tzn);
221 } else {
222 tp = localtime(&pn->mailread);
223 t = asctime(tp);
224 tzn = tp->tm_zone;
225 printf("Mail last read %.16s %.4s (%s)\n", t, t + 20, tzn);
226 }
227 }
228
229 int
230 demi_print(str, oddfield)
231 char *str;
232 int oddfield;
233 {
234 static int lenlast;
235 int lenthis, maxlen;
236
237 lenthis = strlen(str);
238 if (oddfield) {
239 /*
240 * We left off on an odd number of fields. If we haven't
241 * crossed the midpoint of the screen, and we have room for
242 * the next field, print it on the same line; otherwise,
243 * print it on a new line.
244 *
245 * Note: we insist on having the right hand fields start
246 * no less than 5 tabs out.
247 */
248 maxlen = 5 * TAB_LEN;
249 if (maxlen < lenlast)
250 maxlen = lenlast;
251 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
252 lenthis) <= LINE_LEN) {
253 while(lenlast < (4 * TAB_LEN)) {
254 putchar('\t');
255 lenlast += TAB_LEN;
256 }
257 (void)printf("\t%s\n", str); /* force one tab */
258 } else {
259 (void)printf("\n%s", str); /* go to next line */
260 oddfield = !oddfield; /* this'll be undone below */
261 }
262 } else
263 (void)printf("%s", str);
264 oddfield = !oddfield; /* toggle odd/even marker */
265 lenlast = lenthis;
266 return(oddfield);
267 }
268
269 int
270 show_text(directory, file_name, header)
271 char *directory, *file_name, *header;
272 {
273 int ch, lastc;
274 FILE *fp;
275
276 lastc = 0;
277 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
278 if ((fp = fopen(tbuf, "r")) == NULL)
279 return(0);
280 (void)printf("%s\n", header);
281 while ((ch = getc(fp)) != EOF)
282 vputc(lastc = ch);
283 if (lastc != '\n')
284 (void)putchar('\n');
285 (void)fclose(fp);
286 return(1);
287 }
288
289 void
290 vputc(ch)
291 int ch;
292 {
293 char visout[5], *s2;
294
295 ch = toascii(ch);
296 vis(visout, ch, VIS_SAFE|VIS_NOSLASH, 0);
297 for (s2 = visout; *s2; s2++)
298 (void)putchar(*s2);
299 }
300