sprint.c revision 87229
1/*
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#include <sys/cdefs.h>
38
39__FBSDID("$FreeBSD: head/usr.bin/finger/sprint.c 87229 2001-12-02 20:13:42Z markm $");
40
41#ifndef lint
42static const char sccsid[] = "@(#)sprint.c	8.3 (Berkeley) 4/28/95";
43#endif
44
45#include <db.h>
46#include <err.h>
47#include <langinfo.h>
48#include <pwd.h>
49#include <stdio.h>
50#include <string.h>
51#include <time.h>
52#include <utmp.h>
53#include "finger.h"
54
55static void	  stimeprint __P((WHERE *));
56
57void
58sflag_print()
59{
60	extern time_t now;
61	extern int    oflag;
62	PERSON *pn;
63	WHERE *w;
64	int sflag, r, namelen;
65	char p[80];
66	PERSON *tmp;
67	DBT data, key;
68	struct tm *lc;
69
70	if (d_first < 0)
71		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
72	/*
73	 * short format --
74	 *	login name
75	 *	real name
76	 *	terminal name (the XX of ttyXX)
77	 *	if terminal writeable (add an '*' to the terminal name
78	 *		if not)
79	 *	if logged in show idle time and day logged in, else
80	 *		show last login date and time.
81	 *		If > 6 months, show year instead of time.
82	 *	if (-o)
83	 *		office location
84	 *		office phone
85	 *	else
86	 *		remote host
87	 */
88#define	MAXREALNAME	20
89#define MAXHOSTNAME     17      /* in reality, hosts are never longer than 16 */
90	(void)printf("%-*s %-*s%s  %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
91	    "Name", " TTY  Idle  Login  Time",
92	    oflag ? "Office  Phone" : "Where");
93
94	for (sflag = R_FIRST;; sflag = R_NEXT) {
95		r = (*db->seq)(db, &key, &data, sflag);
96		if (r == -1)
97			err(1, "db seq");
98		if (r == 1)
99			break;
100		memmove(&tmp, data.data, sizeof tmp);
101		pn = tmp;
102
103		for (w = pn->whead; w != NULL; w = w->next) {
104			namelen = MAXREALNAME;
105			if (w->info == LOGGEDIN && !w->writable)
106				--namelen;	/* leave space before `*' */
107			(void)printf("%-*.*s %-*.*s", UT_NAMESIZE, UT_NAMESIZE,
108				pn->name, MAXREALNAME, namelen,
109				pn->realname ? pn->realname : "");
110			if (!w->loginat) {
111				(void)printf("  *     *   No logins   ");
112				goto office;
113			}
114			(void)putchar(w->info == LOGGEDIN && !w->writable ?
115			    '*' : ' ');
116			if (*w->tty)
117				(void)printf("%-3.3s ",
118					     (strncmp(w->tty, "tty", 3)
119					      && strncmp(w->tty, "cua", 3))
120					     ? w->tty : w->tty + 3);
121			else
122				(void)printf("    ");
123			if (w->info == LOGGEDIN) {
124				stimeprint(w);
125				(void)printf("  ");
126			} else
127				(void)printf("    *  ");
128			lc = localtime(&w->loginat);
129#define SECSPERDAY 86400
130#define DAYSPERWEEK 7
131#define DAYSPERNYEAR 365
132			if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1)) {
133				(void)strftime(p, sizeof(p), "%a", lc);
134			} else {
135				(void)strftime(p, sizeof(p),
136					     d_first ? "%e %b" : "%b %e", lc);
137			}
138			(void)printf("%-6.6s", p);
139			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2) {
140				(void)strftime(p, sizeof(p), "%Y", lc);
141			} else {
142				(void)strftime(p, sizeof(p), "%R", lc);
143			}
144			(void)printf(" %-5.5s", p);
145office:			if (oflag) {
146				if (pn->office)
147					(void)printf(" %-7.7s", pn->office);
148				else if (pn->officephone)
149					(void)printf(" %-7.7s", " ");
150				if (pn->officephone)
151					(void)printf(" %-.9s",
152					    prphone(pn->officephone));
153			} else
154				(void)printf(" %.*s", MAXHOSTNAME, w->host);
155			putchar('\n');
156		}
157	}
158}
159
160static void
161stimeprint(w)
162	WHERE *w;
163{
164	struct tm *delta;
165
166	delta = gmtime(&w->idletime);
167	if (!delta->tm_yday)
168		if (!delta->tm_hour)
169			if (!delta->tm_min)
170				(void)printf("     ");
171			else
172				(void)printf("%5d", delta->tm_min);
173		else
174			(void)printf("%2d:%02d",
175			    delta->tm_hour, delta->tm_min);
176	else
177		(void)printf("%4dd", delta->tm_yday);
178}
179