sprint.c revision 9993
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1989, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * This code is derived from software contributed to Berkeley by
61590Srgrimes * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
71590Srgrimes *
81590Srgrimes * Redistribution and use in source and binary forms, with or without
91590Srgrimes * modification, are permitted provided that the following conditions
101590Srgrimes * are met:
111590Srgrimes * 1. Redistributions of source code must retain the above copyright
121590Srgrimes *    notice, this list of conditions and the following disclaimer.
131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141590Srgrimes *    notice, this list of conditions and the following disclaimer in the
151590Srgrimes *    documentation and/or other materials provided with the distribution.
161590Srgrimes * 3. All advertising materials mentioning features or use of this software
171590Srgrimes *    must display the following acknowledgement:
181590Srgrimes *	This product includes software developed by the University of
191590Srgrimes *	California, Berkeley and its contributors.
201590Srgrimes * 4. Neither the name of the University nor the names of its contributors
211590Srgrimes *    may be used to endorse or promote products derived from this software
221590Srgrimes *    without specific prior written permission.
231590Srgrimes *
241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341590Srgrimes * SUCH DAMAGE.
351590Srgrimes */
361590Srgrimes
371590Srgrimes#ifndef lint
381590Srgrimesstatic char sccsid[] = "@(#)sprint.c	8.1 (Berkeley) 6/6/93";
391590Srgrimes#endif /* not lint */
401590Srgrimes
411590Srgrimes#include <sys/types.h>
421590Srgrimes#include <sys/time.h>
431590Srgrimes#include <time.h>
441590Srgrimes#include <db.h>
451590Srgrimes#include <pwd.h>
461590Srgrimes#include <errno.h>
471590Srgrimes#include <utmp.h>
481590Srgrimes#include <stdio.h>
491590Srgrimes#include <stdlib.h>
501590Srgrimes#include <string.h>
511590Srgrimes#include "finger.h"
521590Srgrimes
531590Srgrimesstatic void	  stimeprint __P((WHERE *));
541590Srgrimes
551590Srgrimesvoid
561590Srgrimessflag_print()
571590Srgrimes{
581590Srgrimes	extern time_t now;
592537Spst	extern int    oflag;
601590Srgrimes	register PERSON *pn;
611590Srgrimes	register WHERE *w;
621590Srgrimes	register int sflag, r;
639993Sache	char p[80];
641590Srgrimes	DBT data, key;
651590Srgrimes
661590Srgrimes	/*
671590Srgrimes	 * short format --
681590Srgrimes	 *	login name
691590Srgrimes	 *	real name
701590Srgrimes	 *	terminal name (the XX of ttyXX)
711590Srgrimes	 *	if terminal writeable (add an '*' to the terminal name
721590Srgrimes	 *		if not)
731590Srgrimes	 *	if logged in show idle time and day logged in, else
742537Spst	 *		show last login date and time.
752537Spst	 *		If > 6 months, show year instead of time.
762537Spst	 *	if (-o)
772537Spst	 *		office location
782537Spst	 *		office phone
792537Spst	 *	else
802537Spst	 *		remote host
811590Srgrimes	 */
821590Srgrimes#define	MAXREALNAME	20
832537Spst#define	MAXHOSTNAME	20
842537Spst	(void)printf("%-*s %-*s %s  %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
852537Spst	    "Name", "TTY  Idle  Login Time",
862537Spst	    oflag ? " Office     Office Phone" : " Where");
871590Srgrimes
881590Srgrimes	for (sflag = R_FIRST;; sflag = R_NEXT) {
891590Srgrimes		r = (*db->seq)(db, &key, &data, sflag);
901590Srgrimes		if (r == -1)
911590Srgrimes			err("db seq: %s", strerror(errno));
921590Srgrimes		if (r == 1)
931590Srgrimes			break;
941590Srgrimes		pn = *(PERSON **)data.data;
951590Srgrimes
961590Srgrimes		for (w = pn->whead; w != NULL; w = w->next) {
971590Srgrimes			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
981590Srgrimes			    pn->name, MAXREALNAME, MAXREALNAME,
991590Srgrimes			    pn->realname ? pn->realname : "");
1001590Srgrimes			if (!w->loginat) {
1011590Srgrimes				(void)printf("  *     *  No logins   ");
1021590Srgrimes				goto office;
1031590Srgrimes			}
1041590Srgrimes			(void)putchar(w->info == LOGGEDIN && !w->writable ?
1051590Srgrimes			    '*' : ' ');
1061590Srgrimes			if (*w->tty)
1071590Srgrimes				(void)printf("%-2.2s ",
1087371Sjoerg					     (strncmp(w->tty, "tty", 3)
1097371Sjoerg					      && strncmp(w->tty, "cua", 3))
1107371Sjoerg					     ? w->tty : w->tty + 3);
1111590Srgrimes			else
1121590Srgrimes				(void)printf("   ");
1131590Srgrimes			if (w->info == LOGGEDIN) {
1141590Srgrimes				stimeprint(w);
1151590Srgrimes				(void)printf("  ");
1161590Srgrimes			} else
1171590Srgrimes				(void)printf("    *  ");
1189993Sache			strftime(p, sizeof(p), "%c", localtime(&w->loginat));
1199987Swollman#define SECSPERDAY 86400
1209987Swollman#define DAYSPERWEEK 7
1219987Swollman#define DAYSPERNYEAR 365
1222537Spst			if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1))
1232537Spst				(void)printf("%.3s   ", p);
1242537Spst			else
1252537Spst				(void)printf("%.6s", p + 4);
1261590Srgrimes			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
1271590Srgrimes				(void)printf("  %.4s", p + 20);
1281590Srgrimes			else
1291590Srgrimes				(void)printf(" %.5s", p + 11);
1302537Spstoffice:			if (oflag) {
1312537Spst				if (pn->office)
1321590Srgrimes				(void)printf(" %-10.10s", pn->office);
1331590Srgrimes			else if (pn->officephone)
1341590Srgrimes				(void)printf(" %-10.10s", " ");
1351590Srgrimes			if (pn->officephone)
1361590Srgrimes				(void)printf(" %-.15s",
1371590Srgrimes				    prphone(pn->officephone));
1382537Spst			} else
1392537Spst				(void)printf(" %.*s", MAXHOSTNAME, w->host);
1401590Srgrimes			putchar('\n');
1411590Srgrimes		}
1421590Srgrimes	}
1431590Srgrimes}
1441590Srgrimes
1451590Srgrimesstatic void
1461590Srgrimesstimeprint(w)
1471590Srgrimes	WHERE *w;
1481590Srgrimes{
1491590Srgrimes	register struct tm *delta;
1501590Srgrimes
1511590Srgrimes	delta = gmtime(&w->idletime);
1521590Srgrimes	if (!delta->tm_yday)
1531590Srgrimes		if (!delta->tm_hour)
1541590Srgrimes			if (!delta->tm_min)
1551590Srgrimes				(void)printf("     ");
1561590Srgrimes			else
1571590Srgrimes				(void)printf("%5d", delta->tm_min);
1581590Srgrimes		else
1591590Srgrimes			(void)printf("%2d:%02d",
1601590Srgrimes			    delta->tm_hour, delta->tm_min);
1611590Srgrimes	else
1621590Srgrimes		(void)printf("%4dd", delta->tm_yday);
1631590Srgrimes}
164