xref: /openbsd/usr.bin/finger/sprint.c (revision 24b00d2b)
1*24b00d2bSmickey /*	$OpenBSD: sprint.c,v 1.5 1998/07/10 15:45:18 mickey Exp $	*/
21258a77dSderaadt 
3df930be7Sderaadt /*
4df930be7Sderaadt  * Copyright (c) 1989 The Regents of the University of California.
5df930be7Sderaadt  * All rights reserved.
6df930be7Sderaadt  *
7df930be7Sderaadt  * This code is derived from software contributed to Berkeley by
8df930be7Sderaadt  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
9df930be7Sderaadt  *
10df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
11df930be7Sderaadt  * modification, are permitted provided that the following conditions
12df930be7Sderaadt  * are met:
13df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
14df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
15df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
16df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
17df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
18df930be7Sderaadt  * 3. All advertising materials mentioning features or use of this software
19df930be7Sderaadt  *    must display the following acknowledgement:
20df930be7Sderaadt  *	This product includes software developed by the University of
21df930be7Sderaadt  *	California, Berkeley and its contributors.
22df930be7Sderaadt  * 4. Neither the name of the University nor the names of its contributors
23df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
24df930be7Sderaadt  *    without specific prior written permission.
25df930be7Sderaadt  *
26df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df930be7Sderaadt  * SUCH DAMAGE.
37df930be7Sderaadt  */
38df930be7Sderaadt 
39df930be7Sderaadt #ifndef lint
40df930be7Sderaadt /*static char sccsid[] = "from: @(#)sprint.c	5.8 (Berkeley) 12/4/90";*/
41*24b00d2bSmickey static char rcsid[] = "$OpenBSD: sprint.c,v 1.5 1998/07/10 15:45:18 mickey Exp $";
42df930be7Sderaadt #endif /* not lint */
43df930be7Sderaadt 
44df930be7Sderaadt #include <sys/types.h>
45df930be7Sderaadt #include <sys/time.h>
46df930be7Sderaadt #include <tzfile.h>
47df930be7Sderaadt #include <stdio.h>
48c74ee44fSkstailey #include <stdlib.h>
49*24b00d2bSmickey #include <err.h>
50df930be7Sderaadt #include "finger.h"
51c74ee44fSkstailey #include "extern.h"
52df930be7Sderaadt 
53c74ee44fSkstailey void
54df930be7Sderaadt sflag_print()
55df930be7Sderaadt {
56c74ee44fSkstailey 	PERSON *pn;
57c74ee44fSkstailey 	WHERE *w;
58c74ee44fSkstailey 	int cnt;
59c74ee44fSkstailey 	char *p;
60c74ee44fSkstailey 	PERSON **list;
61df930be7Sderaadt 
62df930be7Sderaadt 	list = sort();
63df930be7Sderaadt 	/*
64df930be7Sderaadt 	 * short format --
65df930be7Sderaadt 	 *	login name
66df930be7Sderaadt 	 *	real name
67df930be7Sderaadt 	 *	terminal name (the XX of ttyXX)
68df930be7Sderaadt 	 *	if terminal writeable (add an '*' to the terminal name
69df930be7Sderaadt 	 *		if not)
70df930be7Sderaadt 	 *	if logged in show idle time and day logged in, else
71c74ee44fSkstailey 	 *		show last login date and time.  If > 6 months,
72c74ee44fSkstailey 	 *		show year instead of time.  If < 6 days,
73c74ee44fSkstailey 	 *		show day name instead of month & day.
74c74ee44fSkstailey 	 *	if -h given
75c74ee44fSkstailey 	 *		remote host
76c74ee44fSkstailey 	 *	else if -o given (overriding -h) (default)
77df930be7Sderaadt 	 *		office location
78df930be7Sderaadt 	 *		office phone
79df930be7Sderaadt 	 */
80df930be7Sderaadt #define	MAXREALNAME	20
81c74ee44fSkstailey #define	MAXHOSTNAME	20
82c74ee44fSkstailey 	(void)printf("%-*s %-*s %s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
83c74ee44fSkstailey 	    "Name", "Tty  Idle  Login Time  ",
84c74ee44fSkstailey 	    (oflag) ? "Office     Office Phone" : "Where");
85df930be7Sderaadt 	for (cnt = 0; cnt < entries; ++cnt) {
86df930be7Sderaadt 		pn = list[cnt];
87df930be7Sderaadt 		for (w = pn->whead; w != NULL; w = w->next) {
88df930be7Sderaadt 			(void)printf("%-*.*s %-*.*s ", UT_NAMESIZE, UT_NAMESIZE,
8960bc1fb7Skstailey 			    vs(pn->name), MAXREALNAME, MAXREALNAME,
9060bc1fb7Skstailey 			    pn->realname ? vs(pn->realname) : "");
91df930be7Sderaadt 			if (!w->loginat) {
92df930be7Sderaadt 				(void)printf("  *     *  No logins   ");
93df930be7Sderaadt 				goto office;
94df930be7Sderaadt 			}
95df930be7Sderaadt 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
96df930be7Sderaadt 			    '*' : ' ');
97df930be7Sderaadt 			if (*w->tty)
98df930be7Sderaadt 				(void)printf("%-2.2s ",
99df930be7Sderaadt 				    w->tty[0] != 't' || w->tty[1] != 't' ||
100df930be7Sderaadt 				    w->tty[2] != 'y' ? w->tty : w->tty + 3);
101df930be7Sderaadt 			else
102df930be7Sderaadt 				(void)printf("   ");
103df930be7Sderaadt 			if (w->info == LOGGEDIN) {
104df930be7Sderaadt 				stimeprint(w);
105df930be7Sderaadt 				(void)printf("  ");
106df930be7Sderaadt 			} else
107df930be7Sderaadt 				(void)printf("    *  ");
108df930be7Sderaadt 			p = ctime(&w->loginat);
109c74ee44fSkstailey 			if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1))
110c74ee44fSkstailey 				(void)printf("   %.3s", p);
111c74ee44fSkstailey 			else
112df930be7Sderaadt 				(void)printf("%.6s", p + 4);
113df930be7Sderaadt 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
114df930be7Sderaadt 				(void)printf(" %.4s ", p + 20);
115df930be7Sderaadt 			else
116df930be7Sderaadt 				(void)printf(" %.5s", p + 11);
117c74ee44fSkstailey office:
118c74ee44fSkstailey 			putchar(' ');
119c74ee44fSkstailey 			if (oflag) {
120c74ee44fSkstailey 				if (pn->office)
12160bc1fb7Skstailey 					(void)printf("%-10.10s",
12260bc1fb7Skstailey 					    vs(pn->office));
123df930be7Sderaadt 				else if (pn->officephone)
124df930be7Sderaadt 					(void)printf("%-10.10s", " ");
125df930be7Sderaadt 				if (pn->officephone)
126df930be7Sderaadt 					(void)printf(" %-.15s",
12760bc1fb7Skstailey 					    vs(prphone(pn->officephone)));
128c74ee44fSkstailey 			} else
129c74ee44fSkstailey 				(void)printf("%.*s", MAXHOSTNAME, w->host);
130df930be7Sderaadt 			putchar('\n');
131df930be7Sderaadt 		}
132df930be7Sderaadt 	}
133df930be7Sderaadt }
134df930be7Sderaadt 
135df930be7Sderaadt PERSON **
136df930be7Sderaadt sort()
137df930be7Sderaadt {
138c74ee44fSkstailey 	PERSON *pn, **lp;
139df930be7Sderaadt 	PERSON **list;
140df930be7Sderaadt 
141*24b00d2bSmickey 	if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *)))))
142*24b00d2bSmickey 		err(1, "malloc");
143df930be7Sderaadt 	for (lp = list, pn = phead; pn != NULL; pn = pn->next)
144df930be7Sderaadt 		*lp++ = pn;
145df930be7Sderaadt 	(void)qsort(list, entries, sizeof(PERSON *), psort);
146df930be7Sderaadt 	return(list);
147df930be7Sderaadt }
148df930be7Sderaadt 
149c74ee44fSkstailey int
150df930be7Sderaadt psort(p, t)
151c74ee44fSkstailey 	const void *p, *t;
152df930be7Sderaadt {
153c74ee44fSkstailey 	return(strcmp((*(PERSON **)p)->name, (*(PERSON **)t)->name));
154df930be7Sderaadt }
155df930be7Sderaadt 
156c74ee44fSkstailey void
157df930be7Sderaadt stimeprint(w)
158df930be7Sderaadt 	WHERE *w;
159df930be7Sderaadt {
160c74ee44fSkstailey 	struct tm *delta;
161df930be7Sderaadt 
162df930be7Sderaadt 	delta = gmtime(&w->idletime);
163df930be7Sderaadt 	if (!delta->tm_yday)
164df930be7Sderaadt 		if (!delta->tm_hour)
165df930be7Sderaadt 			if (!delta->tm_min)
166df930be7Sderaadt 				(void)printf("    -");
167df930be7Sderaadt 			else
168df930be7Sderaadt 				(void)printf("%5d", delta->tm_min);
169df930be7Sderaadt 		else
170df930be7Sderaadt 			(void)printf("%2d:%02d",
171df930be7Sderaadt 			    delta->tm_hour, delta->tm_min);
172df930be7Sderaadt 	else
173df930be7Sderaadt 		(void)printf("%4dd", delta->tm_yday);
174df930be7Sderaadt }
175