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