sprint.c revision 23693
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
3823693Speterstatic char sccsid[] = "@(#)sprint.c	8.3 (Berkeley) 4/28/95";
391590Srgrimes#endif /* not lint */
401590Srgrimes
411590Srgrimes#include <sys/types.h>
421590Srgrimes#include <sys/time.h>
431590Srgrimes#include <time.h>
441590Srgrimes#include <db.h>
4523693Speter#include <err.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;
6311386Speter	register int sflag, r, namelen;
649993Sache	char p[80];
6523693Speter	PERSON *tmp;
661590Srgrimes	DBT data, key;
671590Srgrimes
681590Srgrimes	/*
691590Srgrimes	 * short format --
701590Srgrimes	 *	login name
711590Srgrimes	 *	real name
721590Srgrimes	 *	terminal name (the XX of ttyXX)
731590Srgrimes	 *	if terminal writeable (add an '*' to the terminal name
741590Srgrimes	 *		if not)
751590Srgrimes	 *	if logged in show idle time and day logged in, else
762537Spst	 *		show last login date and time.
772537Spst	 *		If > 6 months, show year instead of time.
782537Spst	 *	if (-o)
792537Spst	 *		office location
802537Spst	 *		office phone
812537Spst	 *	else
822537Spst	 *		remote host
831590Srgrimes	 */
841590Srgrimes#define	MAXREALNAME	20
8520157Sache#define MAXHOSTNAME     17      /* in reality, hosts are never longer than 16 */
8611386Speter	(void)printf("%-*s %-*s%s  %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
8711386Speter	    "Name", " TTY  Idle  Login Time",
8820157Sache	    oflag ? " Office  Phone" : " Where");
891590Srgrimes
901590Srgrimes	for (sflag = R_FIRST;; sflag = R_NEXT) {
911590Srgrimes		r = (*db->seq)(db, &key, &data, sflag);
921590Srgrimes		if (r == -1)
9323693Speter			err(1, "db seq");
941590Srgrimes		if (r == 1)
951590Srgrimes			break;
9623693Speter		memmove(&tmp, data.data, sizeof tmp);
9723693Speter		pn = tmp;
981590Srgrimes
991590Srgrimes		for (w = pn->whead; w != NULL; w = w->next) {
10011386Speter			namelen = MAXREALNAME;
10111386Speter			if (w->info == LOGGEDIN && !w->writable)
10211386Speter				--namelen;	/* leave space before `*' */
10311386Speter			(void)printf("%-*.*s %-*.*s", UT_NAMESIZE, UT_NAMESIZE,
10411386Speter				pn->name, MAXREALNAME, namelen,
10511386Speter				pn->realname ? pn->realname : "");
1061590Srgrimes			if (!w->loginat) {
1071590Srgrimes				(void)printf("  *     *  No logins   ");
1081590Srgrimes				goto office;
1091590Srgrimes			}
1101590Srgrimes			(void)putchar(w->info == LOGGEDIN && !w->writable ?
1111590Srgrimes			    '*' : ' ');
1121590Srgrimes			if (*w->tty)
11310553Speter				(void)printf("%-3.3s ",
1147371Sjoerg					     (strncmp(w->tty, "tty", 3)
1157371Sjoerg					      && strncmp(w->tty, "cua", 3))
1167371Sjoerg					     ? w->tty : w->tty + 3);
1171590Srgrimes			else
11810553Speter				(void)printf("    ");
1191590Srgrimes			if (w->info == LOGGEDIN) {
1201590Srgrimes				stimeprint(w);
1211590Srgrimes				(void)printf("  ");
1221590Srgrimes			} else
1231590Srgrimes				(void)printf("    *  ");
1249993Sache			strftime(p, sizeof(p), "%c", localtime(&w->loginat));
1259987Swollman#define SECSPERDAY 86400
1269987Swollman#define DAYSPERWEEK 7
1279987Swollman#define DAYSPERNYEAR 365
1282537Spst			if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1))
1292537Spst				(void)printf("%.3s   ", p);
1302537Spst			else
1312537Spst				(void)printf("%.6s", p + 4);
1321590Srgrimes			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
1331590Srgrimes				(void)printf("  %.4s", p + 20);
1341590Srgrimes			else
1351590Srgrimes				(void)printf(" %.5s", p + 11);
1362537Spstoffice:			if (oflag) {
1372537Spst				if (pn->office)
13820157Sache					(void)printf(" %-7.7s", pn->office);
13920157Sache				else if (pn->officephone)
14020157Sache					(void)printf(" %-7.7s", " ");
14120157Sache				if (pn->officephone)
14220157Sache					(void)printf(" %-.9s",
14320157Sache					    prphone(pn->officephone));
1442537Spst			} else
1452537Spst				(void)printf(" %.*s", MAXHOSTNAME, w->host);
1461590Srgrimes			putchar('\n');
1471590Srgrimes		}
1481590Srgrimes	}
1491590Srgrimes}
1501590Srgrimes
1511590Srgrimesstatic void
1521590Srgrimesstimeprint(w)
1531590Srgrimes	WHERE *w;
1541590Srgrimes{
1551590Srgrimes	register struct tm *delta;
1561590Srgrimes
1571590Srgrimes	delta = gmtime(&w->idletime);
1581590Srgrimes	if (!delta->tm_yday)
1591590Srgrimes		if (!delta->tm_hour)
1601590Srgrimes			if (!delta->tm_min)
1611590Srgrimes				(void)printf("     ");
1621590Srgrimes			else
1631590Srgrimes				(void)printf("%5d", delta->tm_min);
1641590Srgrimes		else
1651590Srgrimes			(void)printf("%2d:%02d",
1661590Srgrimes			    delta->tm_hour, delta->tm_min);
1671590Srgrimes	else
1681590Srgrimes		(void)printf("%4dd", delta->tm_yday);
1691590Srgrimes}
170