sprint.c revision 2537
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 <tzfile.h>
451590Srgrimes#include <db.h>
461590Srgrimes#include <pwd.h>
471590Srgrimes#include <errno.h>
481590Srgrimes#include <utmp.h>
491590Srgrimes#include <stdio.h>
501590Srgrimes#include <stdlib.h>
511590Srgrimes#include <string.h>
521590Srgrimes#include "finger.h"
531590Srgrimes
541590Srgrimesstatic void	  stimeprint __P((WHERE *));
551590Srgrimes
561590Srgrimesvoid
571590Srgrimessflag_print()
581590Srgrimes{
591590Srgrimes	extern time_t now;
602537Spst	extern int    oflag;
611590Srgrimes	register PERSON *pn;
621590Srgrimes	register WHERE *w;
631590Srgrimes	register int sflag, r;
641590Srgrimes	register char *p;
651590Srgrimes	DBT data, key;
661590Srgrimes
671590Srgrimes	/*
681590Srgrimes	 * short format --
691590Srgrimes	 *	login name
701590Srgrimes	 *	real name
711590Srgrimes	 *	terminal name (the XX of ttyXX)
721590Srgrimes	 *	if terminal writeable (add an '*' to the terminal name
731590Srgrimes	 *		if not)
741590Srgrimes	 *	if logged in show idle time and day logged in, else
752537Spst	 *		show last login date and time.
762537Spst	 *		If > 6 months, show year instead of time.
772537Spst	 *	if (-o)
782537Spst	 *		office location
792537Spst	 *		office phone
802537Spst	 *	else
812537Spst	 *		remote host
821590Srgrimes	 */
831590Srgrimes#define	MAXREALNAME	20
842537Spst#define	MAXHOSTNAME	20
852537Spst	(void)printf("%-*s %-*s %s  %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
862537Spst	    "Name", "TTY  Idle  Login Time",
872537Spst	    oflag ? " Office     Office Phone" : " Where");
881590Srgrimes
891590Srgrimes	for (sflag = R_FIRST;; sflag = R_NEXT) {
901590Srgrimes		r = (*db->seq)(db, &key, &data, sflag);
911590Srgrimes		if (r == -1)
921590Srgrimes			err("db seq: %s", strerror(errno));
931590Srgrimes		if (r == 1)
941590Srgrimes			break;
951590Srgrimes		pn = *(PERSON **)data.data;
961590Srgrimes
971590Srgrimes		for (w = pn->whead; w != NULL; w = w->next) {
981590Srgrimes			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
991590Srgrimes			    pn->name, MAXREALNAME, MAXREALNAME,
1001590Srgrimes			    pn->realname ? pn->realname : "");
1011590Srgrimes			if (!w->loginat) {
1021590Srgrimes				(void)printf("  *     *  No logins   ");
1031590Srgrimes				goto office;
1041590Srgrimes			}
1051590Srgrimes			(void)putchar(w->info == LOGGEDIN && !w->writable ?
1061590Srgrimes			    '*' : ' ');
1071590Srgrimes			if (*w->tty)
1081590Srgrimes				(void)printf("%-2.2s ",
1091590Srgrimes				    w->tty[0] != 't' || w->tty[1] != 't' ||
1101590Srgrimes				    w->tty[2] != 'y' ? 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("    *  ");
1181590Srgrimes			p = ctime(&w->loginat);
1192537Spst			if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1))
1202537Spst				(void)printf("%.3s   ", p);
1212537Spst			else
1222537Spst				(void)printf("%.6s", p + 4);
1231590Srgrimes			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
1241590Srgrimes				(void)printf("  %.4s", p + 20);
1251590Srgrimes			else
1261590Srgrimes				(void)printf(" %.5s", p + 11);
1272537Spstoffice:			if (oflag) {
1282537Spst				if (pn->office)
1291590Srgrimes				(void)printf(" %-10.10s", pn->office);
1301590Srgrimes			else if (pn->officephone)
1311590Srgrimes				(void)printf(" %-10.10s", " ");
1321590Srgrimes			if (pn->officephone)
1331590Srgrimes				(void)printf(" %-.15s",
1341590Srgrimes				    prphone(pn->officephone));
1352537Spst			} else
1362537Spst				(void)printf(" %.*s", MAXHOSTNAME, w->host);
1371590Srgrimes			putchar('\n');
1381590Srgrimes		}
1391590Srgrimes	}
1401590Srgrimes}
1411590Srgrimes
1421590Srgrimesstatic void
1431590Srgrimesstimeprint(w)
1441590Srgrimes	WHERE *w;
1451590Srgrimes{
1461590Srgrimes	register struct tm *delta;
1471590Srgrimes
1481590Srgrimes	delta = gmtime(&w->idletime);
1491590Srgrimes	if (!delta->tm_yday)
1501590Srgrimes		if (!delta->tm_hour)
1511590Srgrimes			if (!delta->tm_min)
1521590Srgrimes				(void)printf("     ");
1531590Srgrimes			else
1541590Srgrimes				(void)printf("%5d", delta->tm_min);
1551590Srgrimes		else
1561590Srgrimes			(void)printf("%2d:%02d",
1571590Srgrimes			    delta->tm_hour, delta->tm_min);
1581590Srgrimes	else
1591590Srgrimes		(void)printf("%4dd", delta->tm_yday);
1601590Srgrimes}
161