sprint.c revision 1.8
1/*	$NetBSD: sprint.c,v 1.8 1997/10/19 08:13:46 mrg Exp $	*/
2
3/*
4 * Copyright (c) 1989, 1993
5 *	The Regents of the University of California.  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. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#include <sys/cdefs.h>
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)sprint.c	8.3 (Berkeley) 4/28/95";
43#else
44__RCSID("$NetBSD: sprint.c,v 1.8 1997/10/19 08:13:46 mrg Exp $");
45#endif
46#endif /* not lint */
47
48#include <sys/param.h>
49#include <sys/time.h>
50
51#include <time.h>
52#include <tzfile.h>
53#include <db.h>
54#include <err.h>
55#include <pwd.h>
56#include <errno.h>
57#include <utmp.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61
62#include "finger.h"
63#include "extern.h"
64
65static void	  stimeprint __P((WHERE *));
66
67void
68sflag_print()
69{
70	PERSON *pn;
71	WHERE *w;
72	int sflag, r;
73	char *p;
74	PERSON *tmp;
75	DBT data, key;
76
77	/*
78	 * short format --
79	 *	login name
80	 *	real name
81	 *	terminal name (the XX of ttyXX)
82	 *	if terminal writeable (add an '*' to the terminal name
83	 *		if not)
84	 *	if logged in show idle time and day logged in, else
85	 *		show last login date and time.  If > 6 months,
86	 *		show year instead of time.  If < 6 days,
87	 *		show day name instead of month & day.
88	 *	if -h given
89	 *		remote host
90	 *	else if -o given (overriding -h) (default)
91	 *		office location
92	 *		office phone
93	 */
94#define	MAXREALNAME	20
95	(void)printf("%-*s %-*s %s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
96	    "Name", "Tty  Idle  Login Time  ", (gflag) ? "" :
97	    (oflag) ? "Office     Office Phone" : "Where");
98
99	for (sflag = R_FIRST;; sflag = R_NEXT) {
100		r = (*db->seq)(db, &key, &data, sflag);
101		if (r == -1)
102			err(1, "db seq");
103		if (r == 1)
104			break;
105		memmove(&tmp, data.data, sizeof tmp);
106		pn = tmp;
107
108		for (w = pn->whead; w != NULL; w = w->next) {
109			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
110			    pn->name, MAXREALNAME, MAXREALNAME,
111			    pn->realname ? pn->realname : "");
112			if (!w->loginat) {
113				(void)printf("  *     *  No logins   ");
114				goto office;
115			}
116			(void)putchar(w->info == LOGGEDIN && !w->writable ?
117			    '*' : ' ');
118			if (*w->tty)
119				(void)printf("%-2.2s ",
120				    w->tty[0] != 't' || w->tty[1] != 't' ||
121				    w->tty[2] != 'y' ? 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			p = ctime(&w->loginat);
130			if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1))
131				(void)printf("   %.3s", p);
132			else
133				(void)printf("%.6s", p + 4);
134			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
135				(void)printf(" %.4s ", p + 20);
136			else
137				(void)printf(" %.5s", p + 11);
138office:
139			if (gflag)
140				goto no_gecos;
141			putchar(' ');
142			if (oflag) {
143				if (pn->office)
144					(void)printf("%-10.10s", pn->office);
145				else if (pn->officephone)
146					(void)printf("%-10.10s", " ");
147				if (pn->officephone)
148					(void)printf(" %-.15s",
149						    prphone(pn->officephone));
150			} else
151				(void)printf("%.*s", MAXHOSTNAMELEN, w->host);
152no_gecos:
153			putchar('\n');
154		}
155	}
156}
157
158static void
159stimeprint(w)
160	WHERE *w;
161{
162	struct tm *delta;
163
164	delta = gmtime(&w->idletime);
165	if (!delta->tm_yday)
166		if (!delta->tm_hour)
167			if (!delta->tm_min)
168				(void)printf("    -");
169			else
170				(void)printf("%5d", delta->tm_min);
171		else
172			(void)printf("%2d:%02d",
173			    delta->tm_hour, delta->tm_min);
174	else
175		(void)printf("%4dd", delta->tm_yday);
176}
177