1331722Seadler/*
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 * 4. Neither the name of the University nor the names of its contributors
171590Srgrimes *    may be used to endorse or promote products derived from this software
181590Srgrimes *    without specific prior written permission.
191590Srgrimes *
201590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301590Srgrimes * SUCH DAMAGE.
311590Srgrimes */
321590Srgrimes
3387628Sdwmalone#if 0
3487628Sdwmalone#ifndef lint
3587628Sdwmalonestatic char sccsid[] = "@(#)sprint.c	8.3 (Berkeley) 4/28/95";
3687628Sdwmalone#endif
3787628Sdwmalone#endif
3887628Sdwmalone
3987229Smarkm#include <sys/cdefs.h>
4087229Smarkm__FBSDID("$FreeBSD$");
4187229Smarkm
42201140Sed#include <sys/param.h>
43102944Sdwmalone#include <sys/types.h>
44102944Sdwmalone#include <sys/socket.h>
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>
52202191Sed#include <utmpx.h>
531590Srgrimes#include "finger.h"
541590Srgrimes
5592920Simpstatic void	  stimeprint(WHERE *);
561590Srgrimes
571590Srgrimesvoid
58102944Sdwmalonesflag_print(void)
591590Srgrimes{
6087229Smarkm	PERSON *pn;
6187229Smarkm	WHERE *w;
6287229Smarkm	int sflag, r, namelen;
639993Sache	char p[80];
6423693Speter	PERSON *tmp;
651590Srgrimes	DBT data, key;
6674586Sache	struct tm *lc;
671590Srgrimes
6874586Sache	if (d_first < 0)
6974586Sache		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
701590Srgrimes	/*
711590Srgrimes	 * short format --
721590Srgrimes	 *	login name
731590Srgrimes	 *	real name
741590Srgrimes	 *	terminal name (the XX of ttyXX)
751590Srgrimes	 *	if terminal writeable (add an '*' to the terminal name
761590Srgrimes	 *		if not)
771590Srgrimes	 *	if logged in show idle time and day logged in, else
782537Spst	 *		show last login date and time.
792537Spst	 *		If > 6 months, show year instead of time.
802537Spst	 *	if (-o)
812537Spst	 *		office location
822537Spst	 *		office phone
832537Spst	 *	else
842537Spst	 *		remote host
851590Srgrimes	 */
86156282Srwatson#define	MAXREALNAME	16
8720157Sache#define MAXHOSTNAME     17      /* in reality, hosts are never longer than 16 */
88201140Sed	(void)printf("%-*s %-*s%s %s\n", MAXLOGNAME, "Login", MAXREALNAME,
89156282Srwatson	    "Name", " TTY      Idle  Login  Time  ", (gflag) ? "" :
9074586Sache	    oflag ? "Office  Phone" : "Where");
911590Srgrimes
921590Srgrimes	for (sflag = R_FIRST;; sflag = R_NEXT) {
931590Srgrimes		r = (*db->seq)(db, &key, &data, sflag);
941590Srgrimes		if (r == -1)
9523693Speter			err(1, "db seq");
961590Srgrimes		if (r == 1)
971590Srgrimes			break;
9823693Speter		memmove(&tmp, data.data, sizeof tmp);
9923693Speter		pn = tmp;
1001590Srgrimes
1011590Srgrimes		for (w = pn->whead; w != NULL; w = w->next) {
10211386Speter			namelen = MAXREALNAME;
10311386Speter			if (w->info == LOGGEDIN && !w->writable)
10411386Speter				--namelen;	/* leave space before `*' */
105201140Sed			(void)printf("%-*.*s %-*.*s", MAXLOGNAME, MAXLOGNAME,
10611386Speter				pn->name, MAXREALNAME, namelen,
10711386Speter				pn->realname ? pn->realname : "");
1081590Srgrimes			if (!w->loginat) {
10963007Sasmodai				(void)printf("  *     *   No logins   ");
1101590Srgrimes				goto office;
1111590Srgrimes			}
1121590Srgrimes			(void)putchar(w->info == LOGGEDIN && !w->writable ?
1131590Srgrimes			    '*' : ' ');
1141590Srgrimes			if (*w->tty)
115156282Srwatson				(void)printf("%-7.7s ",
1167371Sjoerg					     (strncmp(w->tty, "tty", 3)
1177371Sjoerg					      && strncmp(w->tty, "cua", 3))
1187371Sjoerg					     ? w->tty : w->tty + 3);
1191590Srgrimes			else
120156282Srwatson				(void)printf("        ");
1211590Srgrimes			if (w->info == LOGGEDIN) {
1221590Srgrimes				stimeprint(w);
1231590Srgrimes				(void)printf("  ");
1241590Srgrimes			} else
1251590Srgrimes				(void)printf("    *  ");
12674586Sache			lc = localtime(&w->loginat);
1279987Swollman#define SECSPERDAY 86400
1289987Swollman#define DAYSPERWEEK 7
1299987Swollman#define DAYSPERNYEAR 365
13074586Sache			if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1)) {
13174586Sache				(void)strftime(p, sizeof(p), "%a", lc);
13274586Sache			} else {
13374586Sache				(void)strftime(p, sizeof(p),
13474586Sache					     d_first ? "%e %b" : "%b %e", lc);
13574586Sache			}
13674586Sache			(void)printf("%-6.6s", p);
13774586Sache			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2) {
13874586Sache				(void)strftime(p, sizeof(p), "%Y", lc);
13974586Sache			} else {
14074586Sache				(void)strftime(p, sizeof(p), "%R", lc);
14174586Sache			}
14274586Sache			(void)printf(" %-5.5s", p);
14399249Sminioffice:
14499249Smini			if (gflag)
14599249Smini				goto no_gecos;
14699249Smini			if (oflag) {
1472537Spst				if (pn->office)
14820157Sache					(void)printf(" %-7.7s", pn->office);
14920157Sache				else if (pn->officephone)
15020157Sache					(void)printf(" %-7.7s", " ");
15120157Sache				if (pn->officephone)
152296873Slidl					(void)printf(" %-.15s",
15320157Sache					    prphone(pn->officephone));
1542537Spst			} else
1552537Spst				(void)printf(" %.*s", MAXHOSTNAME, w->host);
15699249Sminino_gecos:
1571590Srgrimes			putchar('\n');
1581590Srgrimes		}
1591590Srgrimes	}
1601590Srgrimes}
1611590Srgrimes
1621590Srgrimesstatic void
163102944Sdwmalonestimeprint(WHERE *w)
1641590Srgrimes{
16587229Smarkm	struct tm *delta;
1661590Srgrimes
167112987Srwatson	if (w->idletime == -1) {
168112987Srwatson		(void)printf("     ");
169112987Srwatson		return;
170112987Srwatson	}
171112987Srwatson
1721590Srgrimes	delta = gmtime(&w->idletime);
1731590Srgrimes	if (!delta->tm_yday)
1741590Srgrimes		if (!delta->tm_hour)
1751590Srgrimes			if (!delta->tm_min)
1761590Srgrimes				(void)printf("     ");
1771590Srgrimes			else
1781590Srgrimes				(void)printf("%5d", delta->tm_min);
1791590Srgrimes		else
1801590Srgrimes			(void)printf("%2d:%02d",
1811590Srgrimes			    delta->tm_hour, delta->tm_min);
1821590Srgrimes	else
1831590Srgrimes		(void)printf("%4dd", delta->tm_yday);
1841590Srgrimes}
185