sprint.c revision 1590
152419Sjulian/*
252419Sjulian * Copyright (c) 1989, 1993
379727Sschweikh *	The Regents of the University of California.  All rights reserved.
452419Sjulian *
552419Sjulian * This code is derived from software contributed to Berkeley by
652419Sjulian * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
752419Sjulian *
852419Sjulian * Redistribution and use in source and binary forms, with or without
952419Sjulian * modification, are permitted provided that the following conditions
1052419Sjulian * are met:
1152419Sjulian * 1. Redistributions of source code must retain the above copyright
1252419Sjulian *    notice, this list of conditions and the following disclaimer.
1352419Sjulian * 2. Redistributions in binary form must reproduce the above copyright
1479727Sschweikh *    notice, this list of conditions and the following disclaimer in the
1552419Sjulian *    documentation and/or other materials provided with the distribution.
1652419Sjulian * 3. All advertising materials mentioning features or use of this software
1752419Sjulian *    must display the following acknowledgement:
1852419Sjulian *	This product includes software developed by the University of
1952419Sjulian *	California, Berkeley and its contributors.
2052419Sjulian * 4. Neither the name of the University nor the names of its contributors
2152419Sjulian *    may be used to endorse or promote products derived from this software
2252419Sjulian *    without specific prior written permission.
2352419Sjulian *
2452419Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2552419Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2652419Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2752419Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2852419Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2952419Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3052419Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3152419Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3279727Sschweikh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3367627Sasmodai * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3452419Sjulian * SUCH DAMAGE.
3552419Sjulian */
3652419Sjulian
3752419Sjulian#ifndef lint
3852419Sjulianstatic char sccsid[] = "@(#)sprint.c	8.1 (Berkeley) 6/6/93";
3959982Sarchie#endif /* not lint */
4079538Sru
4152419Sjulian#include <sys/types.h>
4252419Sjulian#include <sys/time.h>
4352419Sjulian#include <time.h>
4452419Sjulian#include <tzfile.h>
4584306Sru#include <db.h>
4652419Sjulian#include <pwd.h>
4752419Sjulian#include <errno.h>
4852419Sjulian#include <utmp.h>
4952419Sjulian#include <stdio.h>
5052419Sjulian#include <stdlib.h>
5152419Sjulian#include <string.h>
5254927Sjulian#include "finger.h"
5354927Sjulian
5454927Sjulianstatic void	  stimeprint __P((WHERE *));
5552419Sjulian
5652419Sjulianvoid
5752419Sjuliansflag_print()
5852419Sjulian{
5952419Sjulian	extern time_t now;
6052419Sjulian	register PERSON *pn;
6152419Sjulian	register WHERE *w;
6252419Sjulian	register int sflag, r;
6352419Sjulian	register char *p;
6452419Sjulian	DBT data, key;
6559982Sarchie
6659982Sarchie	/*
6754927Sjulian	 * short format --
6854927Sjulian	 *	login name
6954927Sjulian	 *	real name
7054927Sjulian	 *	terminal name (the XX of ttyXX)
7154927Sjulian	 *	if terminal writeable (add an '*' to the terminal name
7254927Sjulian	 *		if not)
73267938Sbapt	 *	if logged in show idle time and day logged in, else
74	 *		show last login date and time.  If > 6 moths,
75	 *		show year instead of time.
76	 *	office location
77	 *	office phone
78	 */
79#define	MAXREALNAME	20
80	(void)printf("%-*s %-*s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
81	    "Name", "Tty  Idle  Login Time   Office     Office Phone");
82
83	for (sflag = R_FIRST;; sflag = R_NEXT) {
84		r = (*db->seq)(db, &key, &data, sflag);
85		if (r == -1)
86			err("db seq: %s", strerror(errno));
87		if (r == 1)
88			break;
89		pn = *(PERSON **)data.data;
90
91		for (w = pn->whead; w != NULL; w = w->next) {
92			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
93			    pn->name, MAXREALNAME, MAXREALNAME,
94			    pn->realname ? pn->realname : "");
95			if (!w->loginat) {
96				(void)printf("  *     *  No logins   ");
97				goto office;
98			}
99			(void)putchar(w->info == LOGGEDIN && !w->writable ?
100			    '*' : ' ');
101			if (*w->tty)
102				(void)printf("%-2.2s ",
103				    w->tty[0] != 't' || w->tty[1] != 't' ||
104				    w->tty[2] != 'y' ? w->tty : w->tty + 3);
105			else
106				(void)printf("   ");
107			if (w->info == LOGGEDIN) {
108				stimeprint(w);
109				(void)printf("  ");
110			} else
111				(void)printf("    *  ");
112			p = ctime(&w->loginat);
113			(void)printf("%.6s", p + 4);
114			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
115				(void)printf("  %.4s", p + 20);
116			else
117				(void)printf(" %.5s", p + 11);
118office:			if (pn->office)
119				(void)printf(" %-10.10s", pn->office);
120			else if (pn->officephone)
121				(void)printf(" %-10.10s", " ");
122			if (pn->officephone)
123				(void)printf(" %-.15s",
124				    prphone(pn->officephone));
125			putchar('\n');
126		}
127	}
128}
129
130static void
131stimeprint(w)
132	WHERE *w;
133{
134	register struct tm *delta;
135
136	delta = gmtime(&w->idletime);
137	if (!delta->tm_yday)
138		if (!delta->tm_hour)
139			if (!delta->tm_min)
140				(void)printf("     ");
141			else
142				(void)printf("%5d", delta->tm_min);
143		else
144			(void)printf("%2d:%02d",
145			    delta->tm_hour, delta->tm_min);
146	else
147		(void)printf("%4dd", delta->tm_yday);
148}
149