xref: /freebsd/usr.bin/finger/sprint.c (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1989, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
89b50d902SRodney W. Grimes  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
99b50d902SRodney W. Grimes  *
109b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
119b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
129b50d902SRodney W. Grimes  * are met:
139b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
159b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
169b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
179b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
199b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
209b50d902SRodney W. Grimes  *    without specific prior written permission.
219b50d902SRodney W. Grimes  *
229b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329b50d902SRodney W. Grimes  * SUCH DAMAGE.
339b50d902SRodney W. Grimes  */
349b50d902SRodney W. Grimes 
3570ad88f3SEd Schouten #include <sys/param.h>
36f4ac32deSDavid Malone #include <sys/types.h>
37f4ac32deSDavid Malone #include <sys/socket.h>
389b50d902SRodney W. Grimes #include <db.h>
39df3f5d9dSPeter Wemm #include <err.h>
40f4d292b7SAndrey A. Chernov #include <langinfo.h>
419b50d902SRodney W. Grimes #include <pwd.h>
429b50d902SRodney W. Grimes #include <stdio.h>
439b50d902SRodney W. Grimes #include <string.h>
444c86f3adSPhilippe Charnier #include <time.h>
45550dcc59SEd Schouten #include <utmpx.h>
469b50d902SRodney W. Grimes #include "finger.h"
479b50d902SRodney W. Grimes 
48f1bb2cd2SWarner Losh static void	  stimeprint(WHERE *);
499b50d902SRodney W. Grimes 
509b50d902SRodney W. Grimes void
sflag_print(void)51f4ac32deSDavid Malone sflag_print(void)
529b50d902SRodney W. Grimes {
534e030ba6SMark Murray 	PERSON *pn;
544e030ba6SMark Murray 	WHERE *w;
554e030ba6SMark Murray 	int sflag, r, namelen;
567a88bad1SAndrey A. Chernov 	char p[80];
57df3f5d9dSPeter Wemm 	PERSON *tmp;
589b50d902SRodney W. Grimes 	DBT data, key;
59f4d292b7SAndrey A. Chernov 	struct tm *lc;
609b50d902SRodney W. Grimes 
61f4d292b7SAndrey A. Chernov 	if (d_first < 0)
62f4d292b7SAndrey A. Chernov 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
639b50d902SRodney W. Grimes 	/*
649b50d902SRodney W. Grimes 	 * short format --
659b50d902SRodney W. Grimes 	 *	login name
669b50d902SRodney W. Grimes 	 *	real name
679b50d902SRodney W. Grimes 	 *	terminal name (the XX of ttyXX)
689b50d902SRodney W. Grimes 	 *	if terminal writeable (add an '*' to the terminal name
699b50d902SRodney W. Grimes 	 *		if not)
709b50d902SRodney W. Grimes 	 *	if logged in show idle time and day logged in, else
7186641d8fSPaul Traina 	 *		show last login date and time.
7286641d8fSPaul Traina 	 *		If > 6 months, show year instead of time.
7386641d8fSPaul Traina 	 *	if (-o)
749b50d902SRodney W. Grimes 	 *		office location
759b50d902SRodney W. Grimes 	 *		office phone
7686641d8fSPaul Traina 	 *	else
7786641d8fSPaul Traina 	 *		remote host
789b50d902SRodney W. Grimes 	 */
79e0f5d997SRobert Watson #define	MAXREALNAME	16
805b367517SAndrey A. Chernov #define MAXHOSTNAME     17      /* in reality, hosts are never longer than 16 */
8170ad88f3SEd Schouten 	(void)printf("%-*s %-*s%s %s\n", MAXLOGNAME, "Login", MAXREALNAME,
820830bd13SJonathan Mini 	    "Name", " TTY      Idle  Login  Time  ", (gflag) ? "" :
835b367517SAndrey A. Chernov 	    oflag ? "Office  Phone" : "Where");
849b50d902SRodney W. Grimes 
859b50d902SRodney W. Grimes 	for (sflag = R_FIRST;; sflag = R_NEXT) {
869b50d902SRodney W. Grimes 		r = (*db->seq)(db, &key, &data, sflag);
879b50d902SRodney W. Grimes 		if (r == -1)
88df3f5d9dSPeter Wemm 			err(1, "db seq");
899b50d902SRodney W. Grimes 		if (r == 1)
909b50d902SRodney W. Grimes 			break;
91df3f5d9dSPeter Wemm 		memmove(&tmp, data.data, sizeof tmp);
92df3f5d9dSPeter Wemm 		pn = tmp;
939b50d902SRodney W. Grimes 
949b50d902SRodney W. Grimes 		for (w = pn->whead; w != NULL; w = w->next) {
9539c17c43SPeter Wemm 			namelen = MAXREALNAME;
9639c17c43SPeter Wemm 			if (w->info == LOGGEDIN && !w->writable)
9739c17c43SPeter Wemm 				--namelen;	/* leave space before `*' */
9870ad88f3SEd Schouten 			(void)printf("%-*.*s %-*.*s", MAXLOGNAME, MAXLOGNAME,
9939c17c43SPeter Wemm 				pn->name, MAXREALNAME, namelen,
1009b50d902SRodney W. Grimes 				pn->realname ? pn->realname : "");
1019b50d902SRodney W. Grimes 			if (!w->loginat) {
1029b50d902SRodney W. Grimes 				(void)printf("  *     *   No logins   ");
1039b50d902SRodney W. Grimes 				goto office;
1049b50d902SRodney W. Grimes 			}
1059b50d902SRodney W. Grimes 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
1069b50d902SRodney W. Grimes 			    '*' : ' ');
1079b50d902SRodney W. Grimes 			if (*w->tty)
108e0f5d997SRobert Watson 				(void)printf("%-7.7s ",
109b2b31a1cSJoerg Wunsch 					     (strncmp(w->tty, "tty", 3)
110b2b31a1cSJoerg Wunsch 					      && strncmp(w->tty, "cua", 3))
111b2b31a1cSJoerg Wunsch 					     ? w->tty : w->tty + 3);
1129b50d902SRodney W. Grimes 			else
1139b50d902SRodney W. Grimes 				(void)printf("        ");
1149b50d902SRodney W. Grimes 			if (w->info == LOGGEDIN) {
1159b50d902SRodney W. Grimes 				stimeprint(w);
1169b50d902SRodney W. Grimes 				(void)printf("  ");
1179b50d902SRodney W. Grimes 			} else
1189b50d902SRodney W. Grimes 				(void)printf("    *  ");
119f4d292b7SAndrey A. Chernov 			lc = localtime(&w->loginat);
120656dcd43SGarrett Wollman #define SECSPERDAY 86400
121656dcd43SGarrett Wollman #define DAYSPERWEEK 7
122656dcd43SGarrett Wollman #define DAYSPERNYEAR 365
123f4d292b7SAndrey A. Chernov 			if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1)) {
124f4d292b7SAndrey A. Chernov 				(void)strftime(p, sizeof(p), "%a", lc);
125f4d292b7SAndrey A. Chernov 			} else {
126f4d292b7SAndrey A. Chernov 				(void)strftime(p, sizeof(p),
127f4d292b7SAndrey A. Chernov 					     d_first ? "%e %b" : "%b %e", lc);
128f4d292b7SAndrey A. Chernov 			}
129f4d292b7SAndrey A. Chernov 			(void)printf("%-6.6s", p);
130f4d292b7SAndrey A. Chernov 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2) {
131f4d292b7SAndrey A. Chernov 				(void)strftime(p, sizeof(p), "%Y", lc);
132f4d292b7SAndrey A. Chernov 			} else {
133f4d292b7SAndrey A. Chernov 				(void)strftime(p, sizeof(p), "%R", lc);
134f4d292b7SAndrey A. Chernov 			}
135f4d292b7SAndrey A. Chernov 			(void)printf(" %-5.5s", p);
1360830bd13SJonathan Mini office:
1370830bd13SJonathan Mini 			if (gflag)
1380830bd13SJonathan Mini 				goto no_gecos;
1390830bd13SJonathan Mini 			if (oflag) {
14086641d8fSPaul Traina 				if (pn->office)
1415b367517SAndrey A. Chernov 					(void)printf(" %-7.7s", pn->office);
1429b50d902SRodney W. Grimes 				else if (pn->officephone)
1435b367517SAndrey A. Chernov 					(void)printf(" %-7.7s", " ");
1449b50d902SRodney W. Grimes 				if (pn->officephone)
1456e786098SKurt Lidl 					(void)printf(" %-.15s",
1469b50d902SRodney W. Grimes 					    prphone(pn->officephone));
14786641d8fSPaul Traina 			} else
14886641d8fSPaul Traina 				(void)printf(" %.*s", MAXHOSTNAME, w->host);
1490830bd13SJonathan Mini no_gecos:
1509b50d902SRodney W. Grimes 			putchar('\n');
1519b50d902SRodney W. Grimes 		}
1529b50d902SRodney W. Grimes 	}
1539b50d902SRodney W. Grimes }
1549b50d902SRodney W. Grimes 
1559b50d902SRodney W. Grimes static void
stimeprint(WHERE * w)156f4ac32deSDavid Malone stimeprint(WHERE *w)
1579b50d902SRodney W. Grimes {
1584e030ba6SMark Murray 	struct tm *delta;
1599b50d902SRodney W. Grimes 
1607d7d4297SRobert Watson 	if (w->idletime == -1) {
1617d7d4297SRobert Watson 		(void)printf("     ");
1627d7d4297SRobert Watson 		return;
1637d7d4297SRobert Watson 	}
1647d7d4297SRobert Watson 
1659b50d902SRodney W. Grimes 	delta = gmtime(&w->idletime);
1669b50d902SRodney W. Grimes 	if (!delta->tm_yday)
1679b50d902SRodney W. Grimes 		if (!delta->tm_hour)
1689b50d902SRodney W. Grimes 			if (!delta->tm_min)
1699b50d902SRodney W. Grimes 				(void)printf("     ");
1709b50d902SRodney W. Grimes 			else
1719b50d902SRodney W. Grimes 				(void)printf("%5d", delta->tm_min);
1729b50d902SRodney W. Grimes 		else
1739b50d902SRodney W. Grimes 			(void)printf("%2d:%02d",
1749b50d902SRodney W. Grimes 			    delta->tm_hour, delta->tm_min);
1759b50d902SRodney W. Grimes 	else
1769b50d902SRodney W. Grimes 		(void)printf("%4dd", delta->tm_yday);
1779b50d902SRodney W. Grimes }
178