xref: /freebsd/usr.bin/finger/sprint.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
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 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <db.h>
46 #include <err.h>
47 #include <langinfo.h>
48 #include <pwd.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <time.h>
52 #include <utmpx.h>
53 #include "finger.h"
54 
55 static void	  stimeprint(WHERE *);
56 
57 void
58 sflag_print(void)
59 {
60 	PERSON *pn;
61 	WHERE *w;
62 	int sflag, r, namelen;
63 	char p[80];
64 	PERSON *tmp;
65 	DBT data, key;
66 	struct tm *lc;
67 
68 	if (d_first < 0)
69 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
70 	/*
71 	 * short format --
72 	 *	login name
73 	 *	real name
74 	 *	terminal name (the XX of ttyXX)
75 	 *	if terminal writeable (add an '*' to the terminal name
76 	 *		if not)
77 	 *	if logged in show idle time and day logged in, else
78 	 *		show last login date and time.
79 	 *		If > 6 months, show year instead of time.
80 	 *	if (-o)
81 	 *		office location
82 	 *		office phone
83 	 *	else
84 	 *		remote host
85 	 */
86 #define	MAXREALNAME	16
87 #define MAXHOSTNAME     17      /* in reality, hosts are never longer than 16 */
88 	(void)printf("%-*s %-*s%s %s\n", MAXLOGNAME, "Login", MAXREALNAME,
89 	    "Name", " TTY      Idle  Login  Time  ", (gflag) ? "" :
90 	    oflag ? "Office  Phone" : "Where");
91 
92 	for (sflag = R_FIRST;; sflag = R_NEXT) {
93 		r = (*db->seq)(db, &key, &data, sflag);
94 		if (r == -1)
95 			err(1, "db seq");
96 		if (r == 1)
97 			break;
98 		memmove(&tmp, data.data, sizeof tmp);
99 		pn = tmp;
100 
101 		for (w = pn->whead; w != NULL; w = w->next) {
102 			namelen = MAXREALNAME;
103 			if (w->info == LOGGEDIN && !w->writable)
104 				--namelen;	/* leave space before `*' */
105 			(void)printf("%-*.*s %-*.*s", MAXLOGNAME, MAXLOGNAME,
106 				pn->name, MAXREALNAME, namelen,
107 				pn->realname ? pn->realname : "");
108 			if (!w->loginat) {
109 				(void)printf("  *     *   No logins   ");
110 				goto office;
111 			}
112 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
113 			    '*' : ' ');
114 			if (*w->tty)
115 				(void)printf("%-7.7s ",
116 					     (strncmp(w->tty, "tty", 3)
117 					      && strncmp(w->tty, "cua", 3))
118 					     ? w->tty : w->tty + 3);
119 			else
120 				(void)printf("        ");
121 			if (w->info == LOGGEDIN) {
122 				stimeprint(w);
123 				(void)printf("  ");
124 			} else
125 				(void)printf("    *  ");
126 			lc = localtime(&w->loginat);
127 #define SECSPERDAY 86400
128 #define DAYSPERWEEK 7
129 #define DAYSPERNYEAR 365
130 			if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1)) {
131 				(void)strftime(p, sizeof(p), "%a", lc);
132 			} else {
133 				(void)strftime(p, sizeof(p),
134 					     d_first ? "%e %b" : "%b %e", lc);
135 			}
136 			(void)printf("%-6.6s", p);
137 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2) {
138 				(void)strftime(p, sizeof(p), "%Y", lc);
139 			} else {
140 				(void)strftime(p, sizeof(p), "%R", lc);
141 			}
142 			(void)printf(" %-5.5s", p);
143 office:
144 			if (gflag)
145 				goto no_gecos;
146 			if (oflag) {
147 				if (pn->office)
148 					(void)printf(" %-7.7s", pn->office);
149 				else if (pn->officephone)
150 					(void)printf(" %-7.7s", " ");
151 				if (pn->officephone)
152 					(void)printf(" %-.15s",
153 					    prphone(pn->officephone));
154 			} else
155 				(void)printf(" %.*s", MAXHOSTNAME, w->host);
156 no_gecos:
157 			putchar('\n');
158 		}
159 	}
160 }
161 
162 static void
163 stimeprint(WHERE *w)
164 {
165 	struct tm *delta;
166 
167 	if (w->idletime == -1) {
168 		(void)printf("     ");
169 		return;
170 	}
171 
172 	delta = gmtime(&w->idletime);
173 	if (!delta->tm_yday)
174 		if (!delta->tm_hour)
175 			if (!delta->tm_min)
176 				(void)printf("     ");
177 			else
178 				(void)printf("%5d", delta->tm_min);
179 		else
180 			(void)printf("%2d:%02d",
181 			    delta->tm_hour, delta->tm_min);
182 	else
183 		(void)printf("%4dd", delta->tm_yday);
184 }
185