sprint.c revision 92920
1239310Sdim/*
2239310Sdim * Copyright (c) 1989, 1993
3239310Sdim *	The Regents of the University of California.  All rights reserved.
4239310Sdim *
5239310Sdim * This code is derived from software contributed to Berkeley by
6239310Sdim * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
7239310Sdim *
8239310Sdim * Redistribution and use in source and binary forms, with or without
9239310Sdim * modification, are permitted provided that the following conditions
10239310Sdim * are met:
11239310Sdim * 1. Redistributions of source code must retain the above copyright
12239310Sdim *    notice, this list of conditions and the following disclaimer.
13239310Sdim * 2. Redistributions in binary form must reproduce the above copyright
14239310Sdim *    notice, this list of conditions and the following disclaimer in the
15239310Sdim *    documentation and/or other materials provided with the distribution.
16239310Sdim * 3. All advertising materials mentioning features or use of this software
17239310Sdim *    must display the following acknowledgement:
18239310Sdim *	This product includes software developed by the University of
19239310Sdim *	California, Berkeley and its contributors.
20239310Sdim * 4. Neither the name of the University nor the names of its contributors
21239310Sdim *    may be used to endorse or promote products derived from this software
22239310Sdim *    without specific prior written permission.
23239310Sdim *
24239310Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25239310Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26239310Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27239310Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28239310Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29239310Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30239310Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31239310Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32239310Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33239310Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34239310Sdim * SUCH DAMAGE.
35239310Sdim */
36239310Sdim
37239310Sdim#if 0
38239310Sdim#ifndef lint
39239310Sdimstatic char sccsid[] = "@(#)sprint.c	8.3 (Berkeley) 4/28/95";
40239310Sdim#endif
41239310Sdim#endif
42239310Sdim
43239310Sdim#include <sys/cdefs.h>
44239310Sdim__FBSDID("$FreeBSD: head/usr.bin/finger/sprint.c 92920 2002-03-22 01:22:50Z imp $");
45239310Sdim
46239310Sdim#include <db.h>
47239310Sdim#include <err.h>
48239310Sdim#include <langinfo.h>
49239310Sdim#include <pwd.h>
50239310Sdim#include <stdio.h>
51239310Sdim#include <string.h>
52239310Sdim#include <time.h>
53239310Sdim#include <utmp.h>
54239310Sdim#include "finger.h"
55239310Sdim
56239310Sdimstatic void	  stimeprint(WHERE *);
57239310Sdim
58239310Sdimvoid
59239310Sdimsflag_print()
60239310Sdim{
61239310Sdim	extern time_t now;
62239310Sdim	extern int    oflag;
63239310Sdim	PERSON *pn;
64239310Sdim	WHERE *w;
65239310Sdim	int sflag, r, namelen;
66239310Sdim	char p[80];
67239310Sdim	PERSON *tmp;
68239310Sdim	DBT data, key;
69239310Sdim	struct tm *lc;
70239310Sdim
71239310Sdim	if (d_first < 0)
72239310Sdim		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
73239310Sdim	/*
74239310Sdim	 * short format --
75239310Sdim	 *	login name
76239310Sdim	 *	real name
77239310Sdim	 *	terminal name (the XX of ttyXX)
78239310Sdim	 *	if terminal writeable (add an '*' to the terminal name
79239310Sdim	 *		if not)
80239310Sdim	 *	if logged in show idle time and day logged in, else
81239310Sdim	 *		show last login date and time.
82239310Sdim	 *		If > 6 months, show year instead of time.
83239310Sdim	 *	if (-o)
84239310Sdim	 *		office location
85239310Sdim	 *		office phone
86239310Sdim	 *	else
87	 *		remote host
88	 */
89#define	MAXREALNAME	20
90#define MAXHOSTNAME     17      /* in reality, hosts are never longer than 16 */
91	(void)printf("%-*s %-*s%s  %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
92	    "Name", " TTY  Idle  Login  Time",
93	    oflag ? "Office  Phone" : "Where");
94
95	for (sflag = R_FIRST;; sflag = R_NEXT) {
96		r = (*db->seq)(db, &key, &data, sflag);
97		if (r == -1)
98			err(1, "db seq");
99		if (r == 1)
100			break;
101		memmove(&tmp, data.data, sizeof tmp);
102		pn = tmp;
103
104		for (w = pn->whead; w != NULL; w = w->next) {
105			namelen = MAXREALNAME;
106			if (w->info == LOGGEDIN && !w->writable)
107				--namelen;	/* leave space before `*' */
108			(void)printf("%-*.*s %-*.*s", UT_NAMESIZE, UT_NAMESIZE,
109				pn->name, MAXREALNAME, namelen,
110				pn->realname ? pn->realname : "");
111			if (!w->loginat) {
112				(void)printf("  *     *   No logins   ");
113				goto office;
114			}
115			(void)putchar(w->info == LOGGEDIN && !w->writable ?
116			    '*' : ' ');
117			if (*w->tty)
118				(void)printf("%-3.3s ",
119					     (strncmp(w->tty, "tty", 3)
120					      && strncmp(w->tty, "cua", 3))
121					     ? w->tty : w->tty + 3);
122			else
123				(void)printf("    ");
124			if (w->info == LOGGEDIN) {
125				stimeprint(w);
126				(void)printf("  ");
127			} else
128				(void)printf("    *  ");
129			lc = localtime(&w->loginat);
130#define SECSPERDAY 86400
131#define DAYSPERWEEK 7
132#define DAYSPERNYEAR 365
133			if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1)) {
134				(void)strftime(p, sizeof(p), "%a", lc);
135			} else {
136				(void)strftime(p, sizeof(p),
137					     d_first ? "%e %b" : "%b %e", lc);
138			}
139			(void)printf("%-6.6s", p);
140			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2) {
141				(void)strftime(p, sizeof(p), "%Y", lc);
142			} else {
143				(void)strftime(p, sizeof(p), "%R", lc);
144			}
145			(void)printf(" %-5.5s", p);
146office:			if (oflag) {
147				if (pn->office)
148					(void)printf(" %-7.7s", pn->office);
149				else if (pn->officephone)
150					(void)printf(" %-7.7s", " ");
151				if (pn->officephone)
152					(void)printf(" %-.9s",
153					    prphone(pn->officephone));
154			} else
155				(void)printf(" %.*s", MAXHOSTNAME, w->host);
156			putchar('\n');
157		}
158	}
159}
160
161static void
162stimeprint(w)
163	WHERE *w;
164{
165	struct tm *delta;
166
167	delta = gmtime(&w->idletime);
168	if (!delta->tm_yday)
169		if (!delta->tm_hour)
170			if (!delta->tm_min)
171				(void)printf("     ");
172			else
173				(void)printf("%5d", delta->tm_min);
174		else
175			(void)printf("%2d:%02d",
176			    delta->tm_hour, delta->tm_min);
177	else
178		(void)printf("%4dd", delta->tm_yday);
179}
180