sprint.c revision 202191
1132048Snjl/*
2134081Sphk * Copyright (c) 1989, 1993
34Srgrimes *	The Regents of the University of California.  All rights reserved.
44Srgrimes *
54Srgrimes * This code is derived from software contributed to Berkeley by
64Srgrimes * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
74Srgrimes *
84Srgrimes * Redistribution and use in source and binary forms, with or without
941693Simp * modification, are permitted provided that the following conditions
1041693Simp * are met:
1141693Simp * 1. Redistributions of source code must retain the above copyright
1241693Simp *    notice, this list of conditions and the following disclaimer.
135512Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
142838Sdg *    notice, this list of conditions and the following disclaimer in the
152838Sdg *    documentation and/or other materials provided with the distribution.
162838Sdg * 3. All advertising materials mentioning features or use of this software
175512Sjoerg *    must display the following acknowledgement:
185512Sjoerg *	This product includes software developed by the University of
192838Sdg *	California, Berkeley and its contributors.
205417Sjoerg * 4. Neither the name of the University nor the names of its contributors
212838Sdg *    may be used to endorse or promote products derived from this software
2276588Sjoerg *    without specific prior written permission.
2377663Sjoerg *
2476588Sjoerg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3378927Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3478927Sjoerg * SUCH DAMAGE.
3578927Sjoerg */
364Srgrimes
374Srgrimes#if 0
384Srgrimes#ifndef lint
394Srgrimesstatic char sccsid[] = "@(#)sprint.c	8.3 (Berkeley) 4/28/95";
404Srgrimes#endif
414Srgrimes#endif
424Srgrimes
434Srgrimes#include <sys/cdefs.h>
444Srgrimes__FBSDID("$FreeBSD: head/usr.bin/finger/sprint.c 202191 2010-01-13 17:50:58Z ed $");
454Srgrimes
464Srgrimes#include <sys/param.h>
474Srgrimes#include <sys/types.h>
484Srgrimes#include <sys/socket.h>
49471Srgrimes#include <db.h>
50134081Sphk#include <err.h>
514Srgrimes#include <langinfo.h>
524Srgrimes#include <pwd.h>
53116181Sobrien#include <stdio.h>
54116181Sobrien#include <string.h>
55116181Sobrien#include <time.h>
5629494Sjoerg#include <utmpx.h>
574Srgrimes#include "finger.h"
581110Salm
5960041Sphkstatic void	  stimeprint(WHERE *);
6045720Speter
6176166Smarkmvoid
62103675Sphksflag_print(void)
6345720Speter{
6477800Sjoerg	PERSON *pn;
6587992Sjoerg	WHERE *w;
6676166Smarkm	int sflag, r, namelen;
67134081Sphk	char p[80];
6876166Smarkm	PERSON *tmp;
691110Salm	DBT data, key;
7045720Speter	struct tm *lc;
7176166Smarkm
72164033Srwatson	if (d_first < 0)
732838Sdg		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
7445720Speter	/*
75134081Sphk	 * short format --
76131767Simp	 *	login name
7745720Speter	 *	real name
78134081Sphk	 *	terminal name (the XX of ttyXX)
79134081Sphk	 *	if terminal writeable (add an '*' to the terminal name
80132080Simp	 *		if not)
8145720Speter	 *	if logged in show idle time and day logged in, else
825417Sjoerg	 *		show last login date and time.
8345720Speter	 *		If > 6 months, show year instead of time.
8445720Speter	 *	if (-o)
8547643Sdfr	 *		office location
86131767Simp	 *		office phone
8747643Sdfr	 *	else
8845720Speter	 *		remote host
89134081Sphk	 */
9093238Sphk#define	MAXREALNAME	16
91134081Sphk#define MAXHOSTNAME     17      /* in reality, hosts are never longer than 16 */
92134081Sphk	(void)printf("%-*s %-*s%s %s\n", MAXLOGNAME, "Login", MAXREALNAME,
93134081Sphk	    "Name", " TTY      Idle  Login  Time  ", (gflag) ? "" :
9430574Sjoerg	    oflag ? "Office  Phone" : "Where");
95134081Sphk
96134081Sphk	for (sflag = R_FIRST;; sflag = R_NEXT) {
97134081Sphk		r = (*db->seq)(db, &key, &data, sflag);
98134081Sphk		if (r == -1)
99134081Sphk			err(1, "db seq");
100184976Sjkim		if (r == 1)
101184976Sjkim			break;
102134081Sphk		memmove(&tmp, data.data, sizeof tmp);
103134081Sphk		pn = tmp;
104134081Sphk
10579487Sjoerg		for (w = pn->whead; w != NULL; w = w->next) {
106134081Sphk			namelen = MAXREALNAME;
10779487Sjoerg			if (w->info == LOGGEDIN && !w->writable)
1085417Sjoerg				--namelen;	/* leave space before `*' */
10987992Sjoerg			(void)printf("%-*.*s %-*.*s", MAXLOGNAME, MAXLOGNAME,
110134081Sphk				pn->name, MAXREALNAME, namelen,
111134081Sphk				pn->realname ? pn->realname : "");
112134081Sphk			if (!w->loginat) {
113134081Sphk				(void)printf("  *     *   No logins   ");
114135736Sphk				goto office;
115134081Sphk			}
116134081Sphk			(void)putchar(w->info == LOGGEDIN && !w->writable ?
117134081Sphk			    '*' : ' ');
118134081Sphk			if (*w->tty)
11996459Sjoerg				(void)printf("%-7.7s ",
12096459Sjoerg					     (strncmp(w->tty, "tty", 3)
12196459Sjoerg					      && strncmp(w->tty, "cua", 3))
12296459Sjoerg					     ? w->tty : w->tty + 3);
12396459Sjoerg			else
12496459Sjoerg				(void)printf("        ");
12596459Sjoerg			if (w->info == LOGGEDIN) {
12696459Sjoerg				stimeprint(w);
12796459Sjoerg				(void)printf("  ");
12896459Sjoerg			} else
12996459Sjoerg				(void)printf("    *  ");
130134081Sphk			lc = localtime(&w->loginat);
13187992Sjoerg#define SECSPERDAY 86400
132135736Sphk#define DAYSPERWEEK 7
133863Sache#define DAYSPERNYEAR 365
13487992Sjoerg			if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1)) {
135134081Sphk				(void)strftime(p, sizeof(p), "%a", lc);
13687992Sjoerg			} else {
137134081Sphk				(void)strftime(p, sizeof(p),
138134081Sphk					     d_first ? "%e %b" : "%b %e", lc);
139134081Sphk			}
140134081Sphk			(void)printf("%-6.6s", p);
14187992Sjoerg			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2) {
142874Sache				(void)strftime(p, sizeof(p), "%Y", lc);
14387992Sjoerg			} else {
144134081Sphk				(void)strftime(p, sizeof(p), "%R", lc);
145134081Sphk			}
146134081Sphk			(void)printf(" %-5.5s", p);
1474Srgrimesoffice:
1484Srgrimes			if (gflag)
149134081Sphk				goto no_gecos;
150134081Sphk			if (oflag) {
151134081Sphk				if (pn->office)
152134081Sphk					(void)printf(" %-7.7s", pn->office);
153134081Sphk				else if (pn->officephone)
15487992Sjoerg					(void)printf(" %-7.7s", " ");
155134081Sphk				if (pn->officephone)
156134081Sphk					(void)printf(" %-.9s",
157134081Sphk					    prphone(pn->officephone));
15887992Sjoerg			} else
1595512Sjoerg				(void)printf(" %.*s", MAXHOSTNAME, w->host);
16087992Sjoergno_gecos:
161134081Sphk			putchar('\n');
16287992Sjoerg		}
163134081Sphk	}
16487992Sjoerg}
165134081Sphk
166134081Sphkstatic void
16787992Sjoergstimeprint(WHERE *w)
16887992Sjoerg{
169134081Sphk	struct tm *delta;
170134081Sphk
171134081Sphk	if (w->idletime == -1) {
172134081Sphk		(void)printf("     ");
173134081Sphk		return;
174134081Sphk	}
175134081Sphk
176134081Sphk	delta = gmtime(&w->idletime);
177134081Sphk	if (!delta->tm_yday)
178136360Speadar		if (!delta->tm_hour)
179134081Sphk			if (!delta->tm_min)
180134081Sphk				(void)printf("     ");
18178058Sjoerg			else
182134081Sphk				(void)printf("%5d", delta->tm_min);
183134081Sphk		else
184134081Sphk			(void)printf("%2d:%02d",
1854Srgrimes			    delta->tm_hour, delta->tm_min);
186134081Sphk	else
187134081Sphk		(void)printf("%4dd", delta->tm_yday);
188134081Sphk}
189134081Sphk