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

Line Branch Exec Source
1
/*	$OpenBSD: sprint.c,v 1.16 2015/08/20 22:32:41 deraadt 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/time.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <string.h>
40
#include <err.h>
41
#include "finger.h"
42
#include "extern.h"
43
44
void
45
sflag_print(void)
46
{
47
	PERSON *pn;
48
	WHERE *w;
49
	int cnt;
50
	char *p;
51
	PERSON **list;
52
53
	list = sort();
54
	/*
55
	 * short format --
56
	 *	login name
57
	 *	real name
58
	 *	terminal name (the XX of ttyXX)
59
	 *	if terminal writeable (add an '*' to the terminal name
60
	 *		if not)
61
	 *	if logged in show idle time and day logged in, else
62
	 *		show last login date and time.  If > 6 months,
63
	 *		show year instead of time.  If < 6 days,
64
	 *		show day name instead of month & day.
65
	 *	if -h given
66
	 *		remote host
67
	 *	else if -o given (overriding -h) (default)
68
	 *		office location
69
	 *		office phone
70
	 */
71
#define NAME_WIDTH	8
72
#define	MAXREALNAME	20
73
#define	MAXHOSTNAME	20
74
	(void)printf("%-*.*s %-*s %s %s\n",
75
	    NAME_WIDTH, UT_NAMESIZE, "Login", MAXREALNAME,
76
	    "Name", "Tty  Idle  Login Time  ",
77
	    (oflag) ? "Office     Office Phone" : "Where");
78
	for (cnt = 0; cnt < entries; ++cnt) {
79
		pn = list[cnt];
80
		for (w = pn->whead; w != NULL; w = w->next) {
81
			(void)printf("%-*.*s %-*.*s ",
82
			    NAME_WIDTH, UT_NAMESIZE, pn->name,
83
			    MAXREALNAME, MAXREALNAME,
84
			    pn->realname ? pn->realname : "");
85
			if (!w->loginat) {
86
				(void)printf("  *     *  No logins   ");
87
				goto office;
88
			}
89
			(void)putchar(w->info == LOGGEDIN && !w->writable ?
90
			    '*' : ' ');
91
			if (*w->tty)
92
				(void)printf("%-2.2s ",
93
				    w->tty[0] != 't' || w->tty[1] != 't' ||
94
				    w->tty[2] != 'y' ? w->tty : w->tty + 3);
95
			else
96
				(void)printf("   ");
97
			if (w->info == LOGGEDIN) {
98
				stimeprint(w);
99
				(void)printf("  ");
100
			} else
101
				(void)printf("    *  ");
102
			p = ctime(&w->loginat);
103
104
			if (now - w->loginat < SECSPERDAY * 6)
105
				(void)printf("   %.3s", p);
106
			else
107
				(void)printf("%.6s", p + 4);
108
			if (now - w->loginat >= SIXMONTHS)
109
				(void)printf(" %.4s ", p + 20);
110
			else
111
				(void)printf(" %.5s", p + 11);
112
office:
113
			putchar(' ');
114
			if (oflag) {
115
				if (pn->office)
116
					(void)printf("%-10.10s", pn->office);
117
				else if (pn->officephone)
118
					(void)printf("%-10.10s", " ");
119
				if (pn->officephone)
120
					(void)printf(" %-.15s",
121
					    prphone(pn->officephone));
122
			} else
123
				(void)printf("%.*s", MAXHOSTNAME, w->host);
124
			putchar('\n');
125
		}
126
	}
127
}
128
129
PERSON **
130
sort(void)
131
{
132
	PERSON *pn, **lp;
133
	PERSON **list;
134
135
	if (!(list = calloc((u_int)entries, sizeof(PERSON *))))
136
		err(1, "malloc");
137
	for (lp = list, pn = phead; pn != NULL; pn = pn->next)
138
		*lp++ = pn;
139
	(void)qsort(list, entries, sizeof(PERSON *), psort);
140
	return (list);
141
}
142
143
int
144
psort(const void *p, const void *t)
145
{
146
	return (strcmp((*(PERSON **)p)->name, (*(PERSON **)t)->name));
147
}
148
149
void
150
stimeprint(WHERE *w)
151
{
152
	struct tm *delta;
153
154
	delta = gmtime(&w->idletime);
155
	if (!delta->tm_yday) {
156
		if (!delta->tm_hour) {
157
			if (!delta->tm_min)
158
				(void)printf("    -");
159
			else
160
				(void)printf("%5d", delta->tm_min);
161
		 } else {
162
			(void)printf("%2d:%02d",
163
			    delta->tm_hour, delta->tm_min);
164
		 }
165
	} else
166
		(void)printf("%4dd", delta->tm_yday);
167
}