Annotation of src/usr.bin/finger/lprint.c, Revision 1.6
1.6 ! millert 1: /* $OpenBSD: lprint.c,v 1.5 2001/07/12 05:17:04 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.
1.6 ! millert 18: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 19: * may be used to endorse or promote products derived from this software
20: * without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32: * SUCH DAMAGE.
33: */
34:
35: #ifndef lint
36: /*static char sccsid[] = "from: @(#)lprint.c 5.13 (Berkeley) 10/31/90";*/
1.6 ! millert 37: static char rcsid[] = "$OpenBSD: lprint.c,v 1.5 2001/07/12 05:17:04 deraadt Exp $";
1.1 deraadt 38: #endif /* not lint */
39:
40: #include <sys/types.h>
41: #include <sys/file.h>
42: #include <sys/stat.h>
43: #include <sys/time.h>
44: #include <tzfile.h>
45: #include <stdio.h>
46: #include <string.h>
1.4 kstailey 47: #include <time.h>
1.1 deraadt 48: #include <ctype.h>
49: #include <paths.h>
1.3 deraadt 50: #include <vis.h>
1.1 deraadt 51: #include "finger.h"
1.4 kstailey 52: #include "extern.h"
1.1 deraadt 53:
54: #define LINE_LEN 80
55: #define TAB_LEN 8 /* 8 spaces between tabs */
56: #define _PATH_PLAN ".plan"
57: #define _PATH_PROJECT ".project"
58:
1.4 kstailey 59: void
1.1 deraadt 60: lflag_print()
61: {
1.4 kstailey 62: PERSON *pn;
1.1 deraadt 63:
64: for (pn = phead;;) {
65: lprint(pn);
66: if (!pplan) {
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: if (!(pn = pn->next))
72: break;
73: putchar('\n');
74: }
75: }
76:
1.4 kstailey 77: void
1.1 deraadt 78: lprint(pn)
1.4 kstailey 79: PERSON *pn;
1.1 deraadt 80: {
1.4 kstailey 81: struct tm *delta;
82: WHERE *w;
83: int cpr, len, maxlen;
1.1 deraadt 84: struct tm *tp;
85: int oddfield;
1.4 kstailey 86: char *t, *tzn;
1.1 deraadt 87:
1.4 kstailey 88: cpr = 0;
1.1 deraadt 89: /*
90: * long format --
91: * login name
92: * real name
93: * home directory
94: * shell
95: * office, office phone, home phone if available
1.4 kstailey 96: * mail status
1.1 deraadt 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) {
1.4 kstailey 112: (void)snprintf(tbuf, sizeof(tbuf),
113: "%s: %s, %s", OFFICE_TAG, pn->office,
1.1 deraadt 114: prphone(pn->officephone));
115: oddfield = demi_print(tbuf, oddfield);
116: } else {
117: if (pn->office) {
1.4 kstailey 118: (void)snprintf(tbuf, sizeof(tbuf),
119: "%s: %s", OFFICE_TAG, pn->office);
1.1 deraadt 120: oddfield = demi_print(tbuf, oddfield);
121: }
122: if (pn->officephone) {
1.4 kstailey 123: (void)snprintf(tbuf, sizeof(tbuf),
124: "%s: %s", OFFICE_PHONE_TAG,
1.1 deraadt 125: prphone(pn->officephone));
126: oddfield = demi_print(tbuf, oddfield);
127: }
128: }
129: if (pn->homephone) {
1.4 kstailey 130: (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
1.1 deraadt 131: prphone(pn->homephone));
132: oddfield = demi_print(tbuf, oddfield);
133: }
134: if (oddfield)
135: putchar('\n');
136:
137: /*
1.4 kstailey 138: * long format con't:
139: * if logged in
1.1 deraadt 140: * terminal
141: * idle time
142: * if messages allowed
143: * where logged in from
144: * if not logged in
145: * when last logged in
146: */
147: /* find out longest device name for this user for formatting */
148: for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
149: if ((len = strlen(w->tty)) > maxlen)
150: maxlen = len;
151: /* find rest of entries for user */
152: for (w = pn->whead; w != NULL; w = w->next) {
153: switch (w->info) {
154: case LOGGEDIN:
155: tp = localtime(&w->loginat);
156: t = asctime(tp);
157: tzn = tp->tm_zone;
158: cpr = printf("On since %.16s (%s) on %s",
159: t, tzn, w->tty);
160: /*
161: * idle time is tough; if have one, print a comma,
162: * then spaces to pad out the device name, then the
163: * idle time. Follow with a comma if a remote login.
164: */
165: delta = gmtime(&w->idletime);
166: if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
167: cpr += printf("%-*s idle ",
1.5 deraadt 168: (int)(maxlen - strlen(w->tty) + 1), ",");
1.1 deraadt 169: if (delta->tm_yday > 0) {
170: cpr += printf("%d day%s ",
171: delta->tm_yday,
172: delta->tm_yday == 1 ? "" : "s");
173: }
174: cpr += printf("%d:%02d",
175: delta->tm_hour, delta->tm_min);
176: if (*w->host) {
177: putchar(',');
178: ++cpr;
179: }
180: }
181: if (!w->writable)
182: cpr += printf(" (messages off)");
183: break;
184: case LASTLOG:
185: if (w->loginat == 0) {
186: (void)printf("Never logged in.");
187: break;
188: }
189: tp = localtime(&w->loginat);
190: t = asctime(tp);
191: tzn = tp->tm_zone;
192: if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
193: cpr =
194: printf("Last login %.16s %.4s (%s) on %s",
195: t, t + 20, tzn, w->tty);
196: else
197: cpr = printf("Last login %.16s (%s) on %s",
198: t, tzn, w->tty);
199: break;
200: }
201: if (*w->host) {
202: if (LINE_LEN < (cpr + 6 + strlen(w->host)))
203: (void)printf("\n ");
204: (void)printf(" from %s", w->host);
205: }
206: putchar('\n');
207: }
208: if (pn->mailrecv == -1)
209: printf("No Mail.\n");
210: else if (pn->mailrecv > pn->mailread) {
211: tp = localtime(&pn->mailrecv);
212: t = asctime(tp);
213: tzn = tp->tm_zone;
214: printf("New mail received %.16s %.4s (%s)\n", t, t + 20, tzn);
215: tp = localtime(&pn->mailread);
216: t = asctime(tp);
217: tzn = tp->tm_zone;
218: printf(" Unread since %.16s %.4s (%s)\n", t, t + 20, tzn);
219: } else {
220: tp = localtime(&pn->mailread);
221: t = asctime(tp);
222: tzn = tp->tm_zone;
223: printf("Mail last read %.16s %.4s (%s)\n", t, t + 20, tzn);
224: }
225: }
226:
1.4 kstailey 227: int
1.1 deraadt 228: demi_print(str, oddfield)
229: char *str;
230: int oddfield;
231: {
232: static int lenlast;
233: int lenthis, maxlen;
234:
235: lenthis = strlen(str);
236: if (oddfield) {
237: /*
238: * We left off on an odd number of fields. If we haven't
239: * crossed the midpoint of the screen, and we have room for
240: * the next field, print it on the same line; otherwise,
241: * print it on a new line.
242: *
243: * Note: we insist on having the right hand fields start
244: * no less than 5 tabs out.
245: */
246: maxlen = 5 * TAB_LEN;
247: if (maxlen < lenlast)
248: maxlen = lenlast;
249: if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
250: lenthis) <= LINE_LEN) {
251: while(lenlast < (4 * TAB_LEN)) {
252: putchar('\t');
253: lenlast += TAB_LEN;
254: }
255: (void)printf("\t%s\n", str); /* force one tab */
256: } else {
257: (void)printf("\n%s", str); /* go to next line */
258: oddfield = !oddfield; /* this'll be undone below */
259: }
260: } else
261: (void)printf("%s", str);
262: oddfield = !oddfield; /* toggle odd/even marker */
263: lenlast = lenthis;
264: return(oddfield);
265: }
266:
1.4 kstailey 267: int
1.1 deraadt 268: show_text(directory, file_name, header)
269: char *directory, *file_name, *header;
270: {
1.4 kstailey 271: int ch, lastc;
272: FILE *fp;
1.1 deraadt 273:
1.4 kstailey 274: lastc = 0;
275: (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
1.1 deraadt 276: if ((fp = fopen(tbuf, "r")) == NULL)
277: return(0);
278: (void)printf("%s\n", header);
279: while ((ch = getc(fp)) != EOF)
280: vputc(lastc = ch);
281: if (lastc != '\n')
282: (void)putchar('\n');
283: (void)fclose(fp);
284: return(1);
285: }
286:
1.4 kstailey 287: void
1.1 deraadt 288: vputc(ch)
1.4 kstailey 289: int ch;
1.1 deraadt 290: {
1.4 kstailey 291: char visout[5], *s2;
1.1 deraadt 292:
1.3 deraadt 293: ch = toascii(ch);
294: vis(visout, ch, VIS_SAFE|VIS_NOSLASH, 0);
295: for (s2 = visout; *s2; s2++)
296: (void)putchar(*s2);
1.1 deraadt 297: }