sprint.c revision 7371
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#ifndef lint
38static char sccsid[] = "@(#)sprint.c	8.1 (Berkeley) 6/6/93";
39#endif /* not lint */
40
41#include <sys/types.h>
42#include <sys/time.h>
43#include <time.h>
44#include <tzfile.h>
45#include <db.h>
46#include <pwd.h>
47#include <errno.h>
48#include <utmp.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include "finger.h"
53
54static void	  stimeprint __P((WHERE *));
55
56void
57sflag_print()
58{
59	extern time_t now;
60	extern int    oflag;
61	register PERSON *pn;
62	register WHERE *w;
63	register int sflag, r;
64	register char *p;
65	DBT data, key;
66
67	/*
68	 * short format --
69	 *	login name
70	 *	real name
71	 *	terminal name (the XX of ttyXX)
72	 *	if terminal writeable (add an '*' to the terminal name
73	 *		if not)
74	 *	if logged in show idle time and day logged in, else
75	 *		show last login date and time.
76	 *		If > 6 months, show year instead of time.
77	 *	if (-o)
78	 *		office location
79	 *		office phone
80	 *	else
81	 *		remote host
82	 */
83#define	MAXREALNAME	20
84#define	MAXHOSTNAME	20
85	(void)printf("%-*s %-*s %s  %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
86	    "Name", "TTY  Idle  Login Time",
87	    oflag ? " Office     Office Phone" : " Where");
88
89	for (sflag = R_FIRST;; sflag = R_NEXT) {
90		r = (*db->seq)(db, &key, &data, sflag);
91		if (r == -1)
92			err("db seq: %s", strerror(errno));
93		if (r == 1)
94			break;
95		pn = *(PERSON **)data.data;
96
97		for (w = pn->whead; w != NULL; w = w->next) {
98			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
99			    pn->name, MAXREALNAME, MAXREALNAME,
100			    pn->realname ? pn->realname : "");
101			if (!w->loginat) {
102				(void)printf("  *     *  No logins   ");
103				goto office;
104			}
105			(void)putchar(w->info == LOGGEDIN && !w->writable ?
106			    '*' : ' ');
107			if (*w->tty)
108				(void)printf("%-2.2s ",
109					     (strncmp(w->tty, "tty", 3)
110					      && strncmp(w->tty, "cua", 3))
111					     ? w->tty : w->tty + 3);
112			else
113				(void)printf("   ");
114			if (w->info == LOGGEDIN) {
115				stimeprint(w);
116				(void)printf("  ");
117			} else
118				(void)printf("    *  ");
119			p = ctime(&w->loginat);
120			if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1))
121				(void)printf("%.3s   ", p);
122			else
123				(void)printf("%.6s", p + 4);
124			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
125				(void)printf("  %.4s", p + 20);
126			else
127				(void)printf(" %.5s", p + 11);
128office:			if (oflag) {
129				if (pn->office)
130				(void)printf(" %-10.10s", pn->office);
131			else if (pn->officephone)
132				(void)printf(" %-10.10s", " ");
133			if (pn->officephone)
134				(void)printf(" %-.15s",
135				    prphone(pn->officephone));
136			} else
137				(void)printf(" %.*s", MAXHOSTNAME, w->host);
138			putchar('\n');
139		}
140	}
141}
142
143static void
144stimeprint(w)
145	WHERE *w;
146{
147	register struct tm *delta;
148
149	delta = gmtime(&w->idletime);
150	if (!delta->tm_yday)
151		if (!delta->tm_hour)
152			if (!delta->tm_min)
153				(void)printf("     ");
154			else
155				(void)printf("%5d", delta->tm_min);
156		else
157			(void)printf("%2d:%02d",
158			    delta->tm_hour, delta->tm_min);
159	else
160		(void)printf("%4dd", delta->tm_yday);
161}
162