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