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