Annotation of src/usr.bin/finger/lprint.c, Revision 1.9
1.9 ! espie 1: /* $OpenBSD: lprint.c,v 1.8 2004/03/15 02:50:29 tedu 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.9 ! espie 37: static const char rcsid[] = "$OpenBSD: lprint.c,v 1.8 2004/03/15 02:50:29 tedu 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.7 deraadt 60: lflag_print(void)
1.1 deraadt 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.7 deraadt 78: lprint(PERSON *pn)
1.1 deraadt 79: {
1.4 kstailey 80: struct tm *delta;
81: WHERE *w;
82: int cpr, len, maxlen;
1.1 deraadt 83: struct tm *tp;
84: int oddfield;
1.4 kstailey 85: char *t, *tzn;
1.9 ! espie 86: struct storage *mem = NULL;
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",
1.9 ! espie 99: vs(&mem, pn->name), vs(&mem, pn->realname), pn->dir);
1.1 deraadt 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),
1.9 ! espie 113: "%s: %s, %s", OFFICE_TAG, vs(&mem, pn->office),
! 114: vs(&mem, prphone(pn->officephone)));
1.1 deraadt 115: oddfield = demi_print(tbuf, oddfield);
116: } else {
117: if (pn->office) {
1.4 kstailey 118: (void)snprintf(tbuf, sizeof(tbuf),
1.9 ! espie 119: "%s: %s", OFFICE_TAG, vs(&mem, 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.9 ! espie 125: vs(&mem, prphone(pn->officephone)));
1.1 deraadt 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.9 ! espie 131: vs(&mem, prphone(pn->homephone)));
1.1 deraadt 132: oddfield = demi_print(tbuf, oddfield);
133: }
1.9 ! espie 134: free_storage(mem);
1.1 deraadt 135: if (oddfield)
136: putchar('\n');
137:
138: /*
1.4 kstailey 139: * long format con't:
140: * if logged in
1.1 deraadt 141: * terminal
142: * idle time
143: * if messages allowed
144: * where logged in from
145: * if not logged in
146: * when last logged in
147: */
148: /* find out longest device name for this user for formatting */
149: for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
150: if ((len = strlen(w->tty)) > maxlen)
151: maxlen = len;
152: /* find rest of entries for user */
153: for (w = pn->whead; w != NULL; w = w->next) {
154: switch (w->info) {
155: case LOGGEDIN:
156: tp = localtime(&w->loginat);
157: t = asctime(tp);
158: tzn = tp->tm_zone;
159: cpr = printf("On since %.16s (%s) on %s",
160: t, tzn, w->tty);
161: /*
162: * idle time is tough; if have one, print a comma,
163: * then spaces to pad out the device name, then the
164: * idle time. Follow with a comma if a remote login.
165: */
166: delta = gmtime(&w->idletime);
167: if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
168: cpr += printf("%-*s idle ",
1.5 deraadt 169: (int)(maxlen - strlen(w->tty) + 1), ",");
1.1 deraadt 170: if (delta->tm_yday > 0) {
171: cpr += printf("%d day%s ",
172: delta->tm_yday,
173: delta->tm_yday == 1 ? "" : "s");
174: }
175: cpr += printf("%d:%02d",
176: delta->tm_hour, delta->tm_min);
177: if (*w->host) {
178: putchar(',');
179: ++cpr;
180: }
181: }
182: if (!w->writable)
183: cpr += printf(" (messages off)");
184: break;
185: case LASTLOG:
186: if (w->loginat == 0) {
187: (void)printf("Never logged in.");
188: break;
189: }
190: tp = localtime(&w->loginat);
191: t = asctime(tp);
192: tzn = tp->tm_zone;
193: if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
194: cpr =
195: printf("Last login %.16s %.4s (%s) on %s",
196: t, t + 20, tzn, w->tty);
197: else
198: cpr = printf("Last login %.16s (%s) on %s",
199: t, tzn, w->tty);
200: break;
201: }
202: if (*w->host) {
203: if (LINE_LEN < (cpr + 6 + strlen(w->host)))
204: (void)printf("\n ");
205: (void)printf(" from %s", w->host);
206: }
207: putchar('\n');
208: }
209: if (pn->mailrecv == -1)
210: printf("No Mail.\n");
211: else if (pn->mailrecv > pn->mailread) {
212: tp = localtime(&pn->mailrecv);
213: t = asctime(tp);
214: tzn = tp->tm_zone;
215: printf("New mail received %.16s %.4s (%s)\n", t, t + 20, tzn);
216: tp = localtime(&pn->mailread);
217: t = asctime(tp);
218: tzn = tp->tm_zone;
219: printf(" Unread since %.16s %.4s (%s)\n", t, t + 20, tzn);
220: } else {
221: tp = localtime(&pn->mailread);
222: t = asctime(tp);
223: tzn = tp->tm_zone;
224: printf("Mail last read %.16s %.4s (%s)\n", t, t + 20, tzn);
225: }
226: }
227:
1.4 kstailey 228: int
1.7 deraadt 229: demi_print(char *str, int oddfield)
1.1 deraadt 230: {
231: static int lenlast;
232: int lenthis, maxlen;
233:
234: lenthis = strlen(str);
235: if (oddfield) {
236: /*
237: * We left off on an odd number of fields. If we haven't
238: * crossed the midpoint of the screen, and we have room for
239: * the next field, print it on the same line; otherwise,
240: * print it on a new line.
241: *
242: * Note: we insist on having the right hand fields start
243: * no less than 5 tabs out.
244: */
245: maxlen = 5 * TAB_LEN;
246: if (maxlen < lenlast)
247: maxlen = lenlast;
248: if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
249: lenthis) <= LINE_LEN) {
250: while(lenlast < (4 * TAB_LEN)) {
251: putchar('\t');
252: lenlast += TAB_LEN;
253: }
254: (void)printf("\t%s\n", str); /* force one tab */
255: } else {
256: (void)printf("\n%s", str); /* go to next line */
257: oddfield = !oddfield; /* this'll be undone below */
258: }
259: } else
260: (void)printf("%s", str);
261: oddfield = !oddfield; /* toggle odd/even marker */
262: lenlast = lenthis;
1.8 tedu 263: return (oddfield);
1.1 deraadt 264: }
265:
1.4 kstailey 266: int
1.7 deraadt 267: show_text(char *directory, char *file_name, char *header)
1.1 deraadt 268: {
1.4 kstailey 269: int ch, lastc;
270: FILE *fp;
1.1 deraadt 271:
1.4 kstailey 272: lastc = 0;
273: (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
1.1 deraadt 274: if ((fp = fopen(tbuf, "r")) == NULL)
1.8 tedu 275: return (0);
1.1 deraadt 276: (void)printf("%s\n", header);
277: while ((ch = getc(fp)) != EOF)
278: vputc(lastc = ch);
279: if (lastc != '\n')
280: (void)putchar('\n');
281: (void)fclose(fp);
1.8 tedu 282: return (1);
1.1 deraadt 283: }
284:
1.4 kstailey 285: void
1.7 deraadt 286: vputc(int ch)
1.1 deraadt 287: {
1.4 kstailey 288: char visout[5], *s2;
1.1 deraadt 289:
1.3 deraadt 290: ch = toascii(ch);
291: vis(visout, ch, VIS_SAFE|VIS_NOSLASH, 0);
292: for (s2 = visout; *s2; s2++)
293: (void)putchar(*s2);
1.1 deraadt 294: }