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