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