1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. 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 * %sccs.include.redist.c%
9 */
10
11 #ifndef lint
12 static char sccsid[] = "@(#)lprint.c 8.3 (Berkeley) 04/28/95";
13 #endif /* not lint */
14
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/time.h>
18 #include <fcntl.h>
19 #include <time.h>
20 #include <tzfile.h>
21 #include <db.h>
22 #include <err.h>
23 #include <pwd.h>
24 #include <utmp.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <ctype.h>
29 #include <string.h>
30 #include <paths.h>
31 #include "finger.h"
32
33 #define LINE_LEN 80
34 #define TAB_LEN 8 /* 8 spaces between tabs */
35 #define _PATH_FORWARD ".forward"
36 #define _PATH_PLAN ".plan"
37 #define _PATH_PROJECT ".project"
38
39 static int demi_print __P((char *, int));
40 static void lprint __P((PERSON *));
41 static int show_text __P((char *, char *, char *));
42 static void vputc __P((int));
43
44 void
lflag_print()45 lflag_print()
46 {
47 extern int pplan;
48 register PERSON *pn;
49 register int sflag, r;
50 PERSON *tmp;
51 DBT data, key;
52
53 for (sflag = R_FIRST;; sflag = R_NEXT) {
54 r = (*db->seq)(db, &key, &data, sflag);
55 if (r == -1)
56 err(1, "db seq");
57 if (r == 1)
58 break;
59 memmove(&tmp, data.data, sizeof tmp);
60 pn = tmp;
61 if (sflag != R_FIRST)
62 putchar('\n');
63 lprint(pn);
64 if (!pplan) {
65 (void)show_text(pn->dir,
66 _PATH_FORWARD, "Mail forwarded to");
67 (void)show_text(pn->dir, _PATH_PROJECT, "Project");
68 if (!show_text(pn->dir, _PATH_PLAN, "Plan"))
69 (void)printf("No Plan.\n");
70 }
71 }
72 }
73
74 static void
lprint(pn)75 lprint(pn)
76 register PERSON *pn;
77 {
78 extern time_t now;
79 register struct tm *delta;
80 register WHERE *w;
81 register int cpr, len, maxlen;
82 struct tm *tp;
83 int oddfield;
84 char *t, *tzn;
85
86 /*
87 * long format --
88 * login name
89 * real name
90 * home directory
91 * shell
92 * office, office phone, home phone if available
93 */
94 (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
95 pn->name, pn->realname, pn->dir);
96 (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
97
98 /*
99 * try and print office, office phone, and home phone on one line;
100 * if that fails, do line filling so it looks nice.
101 */
102 #define OFFICE_TAG "Office"
103 #define OFFICE_PHONE_TAG "Office Phone"
104 oddfield = 0;
105 if (pn->office && pn->officephone &&
106 strlen(pn->office) + strlen(pn->officephone) +
107 sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
108 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
109 OFFICE_TAG, pn->office, prphone(pn->officephone));
110 oddfield = demi_print(tbuf, oddfield);
111 } else {
112 if (pn->office) {
113 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
114 OFFICE_TAG, pn->office);
115 oddfield = demi_print(tbuf, oddfield);
116 }
117 if (pn->officephone) {
118 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
119 OFFICE_PHONE_TAG, prphone(pn->officephone));
120 oddfield = demi_print(tbuf, oddfield);
121 }
122 }
123 if (pn->homephone) {
124 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
125 prphone(pn->homephone));
126 oddfield = demi_print(tbuf, oddfield);
127 }
128 if (oddfield)
129 putchar('\n');
130
131 /*
132 * long format con't: * if logged in
133 * terminal
134 * idle time
135 * if messages allowed
136 * where logged in from
137 * if not logged in
138 * when last logged in
139 */
140 /* find out longest device name for this user for formatting */
141 for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
142 if ((len = strlen(w->tty)) > maxlen)
143 maxlen = len;
144 /* find rest of entries for user */
145 for (w = pn->whead; w != NULL; w = w->next) {
146 switch (w->info) {
147 case LOGGEDIN:
148 tp = localtime(&w->loginat);
149 t = asctime(tp);
150 tzn = tp->tm_zone;
151 cpr = printf("On since %.16s (%s) on %s",
152 t, tzn, w->tty);
153 /*
154 * idle time is tough; if have one, print a comma,
155 * then spaces to pad out the device name, then the
156 * idle time. Follow with a comma if a remote login.
157 */
158 delta = gmtime(&w->idletime);
159 if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
160 cpr += printf("%-*s idle ",
161 maxlen - strlen(w->tty) + 1, ",");
162 if (delta->tm_yday > 0) {
163 cpr += printf("%d day%s ",
164 delta->tm_yday,
165 delta->tm_yday == 1 ? "" : "s");
166 }
167 cpr += printf("%d:%02d",
168 delta->tm_hour, delta->tm_min);
169 if (*w->host) {
170 putchar(',');
171 ++cpr;
172 }
173 }
174 if (!w->writable)
175 cpr += printf(" (messages off)");
176 break;
177 case LASTLOG:
178 if (w->loginat == 0) {
179 (void)printf("Never logged in.");
180 break;
181 }
182 tp = localtime(&w->loginat);
183 t = asctime(tp);
184 tzn = tp->tm_zone;
185 if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
186 cpr =
187 printf("Last login %.16s %.4s (%s) on %s",
188 t, t + 20, tzn, w->tty);
189 else
190 cpr = printf("Last login %.16s (%s) on %s",
191 t, tzn, w->tty);
192 break;
193 }
194 if (*w->host) {
195 if (LINE_LEN < (cpr + 6 + strlen(w->host)))
196 (void)printf("\n ");
197 (void)printf(" from %s", w->host);
198 }
199 putchar('\n');
200 }
201 }
202
203 static int
demi_print(str,oddfield)204 demi_print(str, oddfield)
205 char *str;
206 int oddfield;
207 {
208 static int lenlast;
209 int lenthis, maxlen;
210
211 lenthis = strlen(str);
212 if (oddfield) {
213 /*
214 * We left off on an odd number of fields. If we haven't
215 * crossed the midpoint of the screen, and we have room for
216 * the next field, print it on the same line; otherwise,
217 * print it on a new line.
218 *
219 * Note: we insist on having the right hand fields start
220 * no less than 5 tabs out.
221 */
222 maxlen = 5 * TAB_LEN;
223 if (maxlen < lenlast)
224 maxlen = lenlast;
225 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
226 lenthis) <= LINE_LEN) {
227 while(lenlast < (4 * TAB_LEN)) {
228 putchar('\t');
229 lenlast += TAB_LEN;
230 }
231 (void)printf("\t%s\n", str); /* force one tab */
232 } else {
233 (void)printf("\n%s", str); /* go to next line */
234 oddfield = !oddfield; /* this'll be undone below */
235 }
236 } else
237 (void)printf("%s", str);
238 oddfield = !oddfield; /* toggle odd/even marker */
239 lenlast = lenthis;
240 return(oddfield);
241 }
242
243 static int
show_text(directory,file_name,header)244 show_text(directory, file_name, header)
245 char *directory, *file_name, *header;
246 {
247 struct stat sb;
248 register FILE *fp;
249 register int ch, cnt, lastc;
250 register char *p;
251 int fd, nr;
252
253 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
254 if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
255 sb.st_size == 0)
256 return(0);
257
258 /* If short enough, and no newlines, show it on a single line.*/
259 if (sb.st_size <= LINE_LEN - strlen(header) - 5) {
260 nr = read(fd, tbuf, sizeof(tbuf));
261 if (nr <= 0) {
262 (void)close(fd);
263 return(0);
264 }
265 for (p = tbuf, cnt = nr; cnt--; ++p)
266 if (*p == '\n')
267 break;
268 if (cnt <= 1) {
269 (void)printf("%s: ", header);
270 for (p = tbuf, cnt = nr; cnt--; ++p)
271 vputc(lastc = *p);
272 if (lastc != '\n')
273 (void)putchar('\n');
274 (void)close(fd);
275 return(1);
276 }
277 else
278 (void)lseek(fd, 0L, SEEK_SET);
279 }
280 if ((fp = fdopen(fd, "r")) == NULL)
281 return(0);
282 (void)printf("%s:\n", header);
283 while ((ch = getc(fp)) != EOF)
284 vputc(lastc = ch);
285 if (lastc != '\n')
286 (void)putchar('\n');
287 (void)fclose(fp);
288 return(1);
289 }
290
291 static void
vputc(ch)292 vputc(ch)
293 register int ch;
294 {
295 int meta;
296
297 if (!isascii(ch)) {
298 (void)putchar('M');
299 (void)putchar('-');
300 ch = toascii(ch);
301 meta = 1;
302 } else
303 meta = 0;
304 if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n'))
305 (void)putchar(ch);
306 else {
307 (void)putchar('^');
308 (void)putchar(ch == '\177' ? '?' : ch | 0100);
309 }
310 }
311