sprint.c revision 1.9
1/*	$NetBSD: sprint.c,v 1.9 1998/09/09 17:22:31 tron 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.9 1998/09/09 17:22:31 tron 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 %-8.5s", p, p + 11);
132			else if (now - w->loginat
133			      < SECSPERDAY * DAYSPERNYEAR / 2)
134				(void)printf("%.6s %-5.5s", p + 4, p + 11);
135			else
136				(void)printf("%.6s %-5.4s", p + 4, p + 20);
137office:
138			if (gflag)
139				goto no_gecos;
140			putchar(' ');
141			if (oflag) {
142				if (pn->office)
143					(void)printf("%-10.10s", pn->office);
144				else if (pn->officephone)
145					(void)printf("%-10.10s", " ");
146				if (pn->officephone)
147					(void)printf(" %-.15s",
148						    prphone(pn->officephone));
149			} else
150				(void)printf("%.*s", MAXHOSTNAMELEN, w->host);
151no_gecos:
152			putchar('\n');
153		}
154	}
155}
156
157static void
158stimeprint(w)
159	WHERE *w;
160{
161	struct tm *delta;
162
163	delta = gmtime(&w->idletime);
164	if (!delta->tm_yday)
165		if (!delta->tm_hour)
166			if (!delta->tm_min)
167				(void)printf("    -");
168			else
169				(void)printf("%5d", delta->tm_min);
170		else
171			(void)printf("%2d:%02d",
172			    delta->tm_hour, delta->tm_min);
173	else
174		(void)printf("%4dd", delta->tm_yday);
175}
176