xref: /freebsd/usr.bin/finger/sprint.c (revision f5147e312f43a9050468de539aeafa072caa1a60)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #if 0
36 #ifndef lint
37 static char sccsid[] = "@(#)sprint.c	8.3 (Berkeley) 4/28/95";
38 #endif
39 #endif
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <db.h>
48 #include <err.h>
49 #include <langinfo.h>
50 #include <pwd.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <time.h>
54 #include <utmpx.h>
55 #include "finger.h"
56 
57 static void	  stimeprint(WHERE *);
58 
59 void
60 sflag_print(void)
61 {
62 	PERSON *pn;
63 	WHERE *w;
64 	int sflag, r, namelen;
65 	char p[80];
66 	PERSON *tmp;
67 	DBT data, key;
68 	struct tm *lc;
69 
70 	if (d_first < 0)
71 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
72 	/*
73 	 * short format --
74 	 *	login name
75 	 *	real name
76 	 *	terminal name (the XX of ttyXX)
77 	 *	if terminal writeable (add an '*' to the terminal name
78 	 *		if not)
79 	 *	if logged in show idle time and day logged in, else
80 	 *		show last login date and time.
81 	 *		If > 6 months, show year instead of time.
82 	 *	if (-o)
83 	 *		office location
84 	 *		office phone
85 	 *	else
86 	 *		remote host
87 	 */
88 #define	MAXREALNAME	16
89 #define MAXHOSTNAME     17      /* in reality, hosts are never longer than 16 */
90 	(void)printf("%-*s %-*s%s %s\n", MAXLOGNAME, "Login", MAXREALNAME,
91 	    "Name", " TTY      Idle  Login  Time  ", (gflag) ? "" :
92 	    oflag ? "Office  Phone" : "Where");
93 
94 	for (sflag = R_FIRST;; sflag = R_NEXT) {
95 		r = (*db->seq)(db, &key, &data, sflag);
96 		if (r == -1)
97 			err(1, "db seq");
98 		if (r == 1)
99 			break;
100 		memmove(&tmp, data.data, sizeof tmp);
101 		pn = tmp;
102 
103 		for (w = pn->whead; w != NULL; w = w->next) {
104 			namelen = MAXREALNAME;
105 			if (w->info == LOGGEDIN && !w->writable)
106 				--namelen;	/* leave space before `*' */
107 			(void)printf("%-*.*s %-*.*s", MAXLOGNAME, MAXLOGNAME,
108 				pn->name, MAXREALNAME, namelen,
109 				pn->realname ? pn->realname : "");
110 			if (!w->loginat) {
111 				(void)printf("  *     *   No logins   ");
112 				goto office;
113 			}
114 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
115 			    '*' : ' ');
116 			if (*w->tty)
117 				(void)printf("%-7.7s ",
118 					     (strncmp(w->tty, "tty", 3)
119 					      && strncmp(w->tty, "cua", 3))
120 					     ? w->tty : w->tty + 3);
121 			else
122 				(void)printf("        ");
123 			if (w->info == LOGGEDIN) {
124 				stimeprint(w);
125 				(void)printf("  ");
126 			} else
127 				(void)printf("    *  ");
128 			lc = localtime(&w->loginat);
129 #define SECSPERDAY 86400
130 #define DAYSPERWEEK 7
131 #define DAYSPERNYEAR 365
132 			if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1)) {
133 				(void)strftime(p, sizeof(p), "%a", lc);
134 			} else {
135 				(void)strftime(p, sizeof(p),
136 					     d_first ? "%e %b" : "%b %e", lc);
137 			}
138 			(void)printf("%-6.6s", p);
139 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2) {
140 				(void)strftime(p, sizeof(p), "%Y", lc);
141 			} else {
142 				(void)strftime(p, sizeof(p), "%R", lc);
143 			}
144 			(void)printf(" %-5.5s", p);
145 office:
146 			if (gflag)
147 				goto no_gecos;
148 			if (oflag) {
149 				if (pn->office)
150 					(void)printf(" %-7.7s", pn->office);
151 				else if (pn->officephone)
152 					(void)printf(" %-7.7s", " ");
153 				if (pn->officephone)
154 					(void)printf(" %-.15s",
155 					    prphone(pn->officephone));
156 			} else
157 				(void)printf(" %.*s", MAXHOSTNAME, w->host);
158 no_gecos:
159 			putchar('\n');
160 		}
161 	}
162 }
163 
164 static void
165 stimeprint(WHERE *w)
166 {
167 	struct tm *delta;
168 
169 	if (w->idletime == -1) {
170 		(void)printf("     ");
171 		return;
172 	}
173 
174 	delta = gmtime(&w->idletime);
175 	if (!delta->tm_yday)
176 		if (!delta->tm_hour)
177 			if (!delta->tm_min)
178 				(void)printf("     ");
179 			else
180 				(void)printf("%5d", delta->tm_min);
181 		else
182 			(void)printf("%2d:%02d",
183 			    delta->tm_hour, delta->tm_min);
184 	else
185 		(void)printf("%4dd", delta->tm_yday);
186 }
187