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