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