GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/finger/lprint.c Lines: 0 119 0.0 %
Date: 2017-11-07 Branches: 0 89 0.0 %

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