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