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