trivial pathname changes
[unix-history] / usr / src / usr.bin / finger / lprint.c
CommitLineData
6b8910b8
KB
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
f15db449 5 * %sccs.include.redist.c%
6b8910b8
KB
6 */
7
8#ifndef lint
f9b6496c 9static char sccsid[] = "@(#)lprint.c 5.11 (Berkeley) %G%";
6b8910b8
KB
10#endif /* not lint */
11
12#include <sys/types.h>
13#include <sys/file.h>
14#include <sys/stat.h>
15#include <sys/time.h>
16#include <tzfile.h>
17#include <stdio.h>
d65ceee1 18#include <ctype.h>
6b8910b8
KB
19#include "finger.h"
20#include "pathnames.h"
21
22#define LINE_LEN 80
23#define TAB_LEN 8 /* 8 spaces between tabs */
24#define _PATH_PLAN ".plan"
25#define _PATH_PROJECT ".project"
26
27lflag_print()
28{
6b8910b8
KB
29 extern int pplan;
30 register PERSON *pn;
31
7619ff47 32 for (pn = phead;;) {
6b8910b8
KB
33 lprint(pn);
34 if (!pplan) {
7619ff47 35 (void)show_text(pn->dir, _PATH_PROJECT, "Project:");
6b8910b8
KB
36 if (!show_text(pn->dir, _PATH_PLAN, "Plan:"))
37 (void)printf("No Plan.\n");
38 }
39 if (!(pn = pn->next))
40 break;
41 putchar('\n');
42 }
43}
44
45lprint(pn)
46 register PERSON *pn;
47{
48 extern time_t now;
49 register struct tm *delta;
7619ff47 50 register WHERE *w;
6b8910b8 51 register int cpr, len, maxlen;
f9b6496c 52 struct tm *tp;
6b8910b8
KB
53 int oddfield;
54 time_t time();
f9b6496c 55 char *t, *tzn, *prphone();
6b8910b8
KB
56
57 /*
58 * long format --
59 * login name
60 * real name
61 * home directory
62 * shell
63 * office, office phone, home phone if available
64 */
65 (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
66 pn->name, pn->realname, pn->dir);
67 (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
68
69 /*
70 * try and print office, office phone, and home phone on one line;
71 * if that fails, do line filling so it looks nice.
72 */
73#define OFFICE_TAG "Office"
74#define OFFICE_PHONE_TAG "Office Phone"
75 oddfield = 0;
76 if (pn->office && pn->officephone &&
77 strlen(pn->office) + strlen(pn->officephone) +
78 sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
79 (void)sprintf(tbuf, "%s: %s, %s", OFFICE_TAG, pn->office,
f156ea40 80 prphone(pn->officephone));
6b8910b8
KB
81 oddfield = demi_print(tbuf, oddfield);
82 } else {
83 if (pn->office) {
84 (void)sprintf(tbuf, "%s: %s", OFFICE_TAG, pn->office);
85 oddfield = demi_print(tbuf, oddfield);
86 }
87 if (pn->officephone) {
88 (void)sprintf(tbuf, "%s: %s", OFFICE_PHONE_TAG,
f156ea40 89 prphone(pn->officephone));
6b8910b8
KB
90 oddfield = demi_print(tbuf, oddfield);
91 }
92 }
93 if (pn->homephone) {
f156ea40
KB
94 (void)sprintf(tbuf, "%s: %s", "Home Phone",
95 prphone(pn->homephone));
6b8910b8
KB
96 oddfield = demi_print(tbuf, oddfield);
97 }
98 if (oddfield)
99 putchar('\n');
100
101 /*
7619ff47 102 * long format con't: * if logged in
6b8910b8
KB
103 * terminal
104 * idle time
105 * if messages allowed
106 * where logged in from
7619ff47
EW
107 * if not logged in
108 * when last logged in
6b8910b8 109 */
7619ff47 110 /* find out longest device name for this user for formatting */
3dc87a1e 111 for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
7619ff47
EW
112 if ((len = strlen(w->tty)) > maxlen)
113 maxlen = len;
114 /* find rest of entries for user */
115 for (w = pn->whead; w != NULL; w = w->next) {
116 switch (w->info) {
117 case LOGGEDIN:
f9b6496c
KB
118 tp = localtime(&w->loginat);
119 t = asctime(tp);
120 tzn = tp->tm_zone;
121 cpr = printf("On since %16.16s (%s) on %s",
122 t, tzn, w->tty);
6b8910b8
KB
123 /*
124 * idle time is tough; if have one, print a comma,
125 * then spaces to pad out the device name, then the
126 * idle time. Follow with a comma if a remote login.
127 */
7619ff47 128 delta = gmtime(&w->idletime);
6b8910b8
KB
129 if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
130 cpr += printf("%-*s idle ",
7619ff47 131 maxlen - strlen(w->tty) + 1, ",");
6b8910b8
KB
132 if (delta->tm_yday > 0) {
133 cpr += printf("%d day%s ",
134 delta->tm_yday,
135 delta->tm_yday == 1 ? "" : "s");
136 }
137 cpr += printf("%d:%02d",
138 delta->tm_hour, delta->tm_min);
7619ff47 139 if (*w->host) {
6b8910b8
KB
140 putchar(',');
141 ++cpr;
142 }
143 }
7619ff47 144 if (!w->writable)
6b8910b8 145 cpr += printf(" (messages off)");
7619ff47
EW
146 break;
147 case LASTLOG:
148 if (w->loginat == 0) {
149 (void)printf("Never logged in.");
150 break;
6b8910b8 151 }
f9b6496c
KB
152 tp = localtime(&w->loginat);
153 t = asctime(tp);
154 tzn = tp->tm_zone;
7619ff47 155 if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
f9b6496c
KB
156 cpr = printf("Last login %10.10s (%s), %4.4s on %s",
157 t, t + 20, tzn, w->tty);
7619ff47 158 else
f9b6496c
KB
159 cpr = printf("Last login %16.16s (%s) on %s",
160 t, tzn, w->tty);
7619ff47 161 break;
6b8910b8 162 }
7619ff47
EW
163 if (*w->host) {
164 if (LINE_LEN < (cpr + 6 + strlen(w->host)))
6b8910b8 165 (void)printf("\n ");
7619ff47 166 (void)printf(" from %s", w->host);
6b8910b8
KB
167 }
168 putchar('\n');
169 }
6b8910b8
KB
170}
171
172demi_print(str, oddfield)
173 char *str;
174 int oddfield;
175{
176 static int lenlast;
177 int lenthis, maxlen;
178
179 lenthis = strlen(str);
180 if (oddfield) {
181 /*
182 * We left off on an odd number of fields. If we haven't
183 * crossed the midpoint of the screen, and we have room for
184 * the next field, print it on the same line; otherwise,
185 * print it on a new line.
186 *
187 * Note: we insist on having the right hand fields start
188 * no less than 5 tabs out.
189 */
190 maxlen = 5 * TAB_LEN;
191 if (maxlen < lenlast)
192 maxlen = lenlast;
193 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
194 lenthis) <= LINE_LEN) {
195 while(lenlast < (4 * TAB_LEN)) {
196 putchar('\t');
197 lenlast += TAB_LEN;
198 }
199 (void)printf("\t%s\n", str); /* force one tab */
200 } else {
201 (void)printf("\n%s", str); /* go to next line */
202 oddfield = !oddfield; /* this'll be undone below */
203 }
204 } else
205 (void)printf("%s", str);
206 oddfield = !oddfield; /* toggle odd/even marker */
207 lenlast = lenthis;
208 return(oddfield);
209}
210
211show_text(directory, file_name, header)
212 char *directory, *file_name, *header;
213{
980f4f50
EW
214 register int ch, lastc;
215 register FILE *fp;
6b8910b8
KB
216
217 (void)sprintf(tbuf, "%s/%s", directory, file_name);
980f4f50 218 if ((fp = fopen(tbuf, "r")) == NULL)
6b8910b8
KB
219 return(0);
220 (void)printf("%s\n", header);
980f4f50
EW
221 while ((ch = getc(fp)) != EOF)
222 vputc(lastc = ch);
223 if (lastc != '\n')
d65ceee1 224 (void)putchar('\n');
980f4f50 225 (void)fclose(fp);
6b8910b8
KB
226 return(1);
227}
d65ceee1
KB
228
229vputc(ch)
230 register int ch;
231{
232 int meta;
233
234 if (!isascii(ch)) {
235 (void)putchar('M');
236 (void)putchar('-');
237 ch = toascii(ch);
238 meta = 1;
239 } else
240 meta = 0;
d5df533e 241 if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n'))
d65ceee1
KB
242 (void)putchar(ch);
243 else {
244 (void)putchar('^');
245 (void)putchar(ch == '\177' ? '?' : ch | 0100);
246 }
247}