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