sprint.c revision 74586
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
3827169Scharnier#if 0
3923693Speterstatic char sccsid[] = "@(#)sprint.c	8.3 (Berkeley) 4/28/95";
4072109Scharnier#endif
4127169Scharnierstatic const char rcsid[] =
4250477Speter  "$FreeBSD: head/usr.bin/finger/sprint.c 74586 2001-03-21 18:43:49Z ache $";
431590Srgrimes#endif /* not lint */
441590Srgrimes
451590Srgrimes#include <db.h>
4623693Speter#include <err.h>
4774586Sache#include <langinfo.h>
481590Srgrimes#include <pwd.h>
491590Srgrimes#include <stdio.h>
501590Srgrimes#include <string.h>
5172109Scharnier#include <time.h>
5272109Scharnier#include <utmp.h>
531590Srgrimes#include "finger.h"
541590Srgrimes
551590Srgrimesstatic void	  stimeprint __P((WHERE *));
561590Srgrimes
571590Srgrimesvoid
581590Srgrimessflag_print()
591590Srgrimes{
601590Srgrimes	extern time_t now;
612537Spst	extern int    oflag;
621590Srgrimes	register PERSON *pn;
631590Srgrimes	register WHERE *w;
6411386Speter	register int sflag, r, namelen;
659993Sache	char p[80];
6623693Speter	PERSON *tmp;
671590Srgrimes	DBT data, key;
6874586Sache	struct tm *lc;
691590Srgrimes
7074586Sache	if (d_first < 0)
7174586Sache		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
721590Srgrimes	/*
731590Srgrimes	 * short format --
741590Srgrimes	 *	login name
751590Srgrimes	 *	real name
761590Srgrimes	 *	terminal name (the XX of ttyXX)
771590Srgrimes	 *	if terminal writeable (add an '*' to the terminal name
781590Srgrimes	 *		if not)
791590Srgrimes	 *	if logged in show idle time and day logged in, else
802537Spst	 *		show last login date and time.
812537Spst	 *		If > 6 months, show year instead of time.
822537Spst	 *	if (-o)
832537Spst	 *		office location
842537Spst	 *		office phone
852537Spst	 *	else
862537Spst	 *		remote host
871590Srgrimes	 */
881590Srgrimes#define	MAXREALNAME	20
8920157Sache#define MAXHOSTNAME     17      /* in reality, hosts are never longer than 16 */
9011386Speter	(void)printf("%-*s %-*s%s  %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
9174586Sache	    "Name", " TTY  Idle  Login  Time",
9274586Sache	    oflag ? "Office  Phone" : "Where");
931590Srgrimes
941590Srgrimes	for (sflag = R_FIRST;; sflag = R_NEXT) {
951590Srgrimes		r = (*db->seq)(db, &key, &data, sflag);
961590Srgrimes		if (r == -1)
9723693Speter			err(1, "db seq");
981590Srgrimes		if (r == 1)
991590Srgrimes			break;
10023693Speter		memmove(&tmp, data.data, sizeof tmp);
10123693Speter		pn = tmp;
1021590Srgrimes
1031590Srgrimes		for (w = pn->whead; w != NULL; w = w->next) {
10411386Speter			namelen = MAXREALNAME;
10511386Speter			if (w->info == LOGGEDIN && !w->writable)
10611386Speter				--namelen;	/* leave space before `*' */
10711386Speter			(void)printf("%-*.*s %-*.*s", UT_NAMESIZE, UT_NAMESIZE,
10811386Speter				pn->name, MAXREALNAME, namelen,
10911386Speter				pn->realname ? pn->realname : "");
1101590Srgrimes			if (!w->loginat) {
11163007Sasmodai				(void)printf("  *     *   No logins   ");
1121590Srgrimes				goto office;
1131590Srgrimes			}
1141590Srgrimes			(void)putchar(w->info == LOGGEDIN && !w->writable ?
1151590Srgrimes			    '*' : ' ');
1161590Srgrimes			if (*w->tty)
11710553Speter				(void)printf("%-3.3s ",
1187371Sjoerg					     (strncmp(w->tty, "tty", 3)
1197371Sjoerg					      && strncmp(w->tty, "cua", 3))
1207371Sjoerg					     ? w->tty : w->tty + 3);
1211590Srgrimes			else
12210553Speter				(void)printf("    ");
1231590Srgrimes			if (w->info == LOGGEDIN) {
1241590Srgrimes				stimeprint(w);
1251590Srgrimes				(void)printf("  ");
1261590Srgrimes			} else
1271590Srgrimes				(void)printf("    *  ");
12874586Sache			lc = localtime(&w->loginat);
1299987Swollman#define SECSPERDAY 86400
1309987Swollman#define DAYSPERWEEK 7
1319987Swollman#define DAYSPERNYEAR 365
13274586Sache			if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1)) {
13374586Sache				(void)strftime(p, sizeof(p), "%a", lc);
13474586Sache			} else {
13574586Sache				(void)strftime(p, sizeof(p),
13674586Sache					     d_first ? "%e %b" : "%b %e", lc);
13774586Sache			}
13874586Sache			(void)printf("%-6.6s", p);
13974586Sache			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2) {
14074586Sache				(void)strftime(p, sizeof(p), "%Y", lc);
14174586Sache			} else {
14274586Sache				(void)strftime(p, sizeof(p), "%R", lc);
14374586Sache			}
14474586Sache			(void)printf(" %-5.5s", p);
1452537Spstoffice:			if (oflag) {
1462537Spst				if (pn->office)
14720157Sache					(void)printf(" %-7.7s", pn->office);
14820157Sache				else if (pn->officephone)
14920157Sache					(void)printf(" %-7.7s", " ");
15020157Sache				if (pn->officephone)
15120157Sache					(void)printf(" %-.9s",
15220157Sache					    prphone(pn->officephone));
1532537Spst			} else
1542537Spst				(void)printf(" %.*s", MAXHOSTNAME, w->host);
1551590Srgrimes			putchar('\n');
1561590Srgrimes		}
1571590Srgrimes	}
1581590Srgrimes}
1591590Srgrimes
1601590Srgrimesstatic void
1611590Srgrimesstimeprint(w)
1621590Srgrimes	WHERE *w;
1631590Srgrimes{
1641590Srgrimes	register struct tm *delta;
1651590Srgrimes
1661590Srgrimes	delta = gmtime(&w->idletime);
1671590Srgrimes	if (!delta->tm_yday)
1681590Srgrimes		if (!delta->tm_hour)
1691590Srgrimes			if (!delta->tm_min)
1701590Srgrimes				(void)printf("     ");
1711590Srgrimes			else
1721590Srgrimes				(void)printf("%5d", delta->tm_min);
1731590Srgrimes		else
1741590Srgrimes			(void)printf("%2d:%02d",
1751590Srgrimes			    delta->tm_hour, delta->tm_min);
1761590Srgrimes	else
1771590Srgrimes		(void)printf("%4dd", delta->tm_yday);
1781590Srgrimes}
179