xref: /openbsd/usr.bin/finger/sprint.c (revision 70ef01f6)
1*70ef01f6Sdavid /*	$OpenBSD: sprint.c,v 1.9 2003/07/10 00:06:50 david 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.
18f75387cbSmillert  * 3. Neither the name of the University nor the names of its contributors
19df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
20df930be7Sderaadt  *    without specific prior written permission.
21df930be7Sderaadt  *
22df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df930be7Sderaadt  * SUCH DAMAGE.
33df930be7Sderaadt  */
34df930be7Sderaadt 
35df930be7Sderaadt #ifndef lint
36df930be7Sderaadt /*static char sccsid[] = "from: @(#)sprint.c	5.8 (Berkeley) 12/4/90";*/
37*70ef01f6Sdavid static char rcsid[] = "$OpenBSD: sprint.c,v 1.9 2003/07/10 00:06:50 david Exp $";
38df930be7Sderaadt #endif /* not lint */
39df930be7Sderaadt 
40df930be7Sderaadt #include <sys/types.h>
41df930be7Sderaadt #include <sys/time.h>
42df930be7Sderaadt #include <tzfile.h>
43df930be7Sderaadt #include <stdio.h>
44c74ee44fSkstailey #include <stdlib.h>
45*70ef01f6Sdavid #include <string.h>
4624b00d2bSmickey #include <err.h>
47df930be7Sderaadt #include "finger.h"
48c74ee44fSkstailey #include "extern.h"
49df930be7Sderaadt 
50c74ee44fSkstailey void
511837a5caSderaadt sflag_print(void)
52df930be7Sderaadt {
53c74ee44fSkstailey 	PERSON *pn;
54c74ee44fSkstailey 	WHERE *w;
55c74ee44fSkstailey 	int cnt;
56c74ee44fSkstailey 	char *p;
57c74ee44fSkstailey 	PERSON **list;
58df930be7Sderaadt 
59df930be7Sderaadt 	list = sort();
60df930be7Sderaadt 	/*
61df930be7Sderaadt 	 * short format --
62df930be7Sderaadt 	 *	login name
63df930be7Sderaadt 	 *	real name
64df930be7Sderaadt 	 *	terminal name (the XX of ttyXX)
65df930be7Sderaadt 	 *	if terminal writeable (add an '*' to the terminal name
66df930be7Sderaadt 	 *		if not)
67df930be7Sderaadt 	 *	if logged in show idle time and day logged in, else
68c74ee44fSkstailey 	 *		show last login date and time.  If > 6 months,
69c74ee44fSkstailey 	 *		show year instead of time.  If < 6 days,
70c74ee44fSkstailey 	 *		show day name instead of month & day.
71c74ee44fSkstailey 	 *	if -h given
72c74ee44fSkstailey 	 *		remote host
73c74ee44fSkstailey 	 *	else if -o given (overriding -h) (default)
74df930be7Sderaadt 	 *		office location
75df930be7Sderaadt 	 *		office phone
76df930be7Sderaadt 	 */
7735c79e0bSderaadt #define NAME_WIDTH	8
78df930be7Sderaadt #define	MAXREALNAME	20
79c74ee44fSkstailey #define	MAXHOSTNAME	20
8035c79e0bSderaadt 	(void)printf("%-*.*s %-*s %s %s\n",
8135c79e0bSderaadt 	    NAME_WIDTH, UT_NAMESIZE, "Login", MAXREALNAME,
82c74ee44fSkstailey 	    "Name", "Tty  Idle  Login Time  ",
83c74ee44fSkstailey 	    (oflag) ? "Office     Office Phone" : "Where");
84df930be7Sderaadt 	for (cnt = 0; cnt < entries; ++cnt) {
85df930be7Sderaadt 		pn = list[cnt];
86df930be7Sderaadt 		for (w = pn->whead; w != NULL; w = w->next) {
8735c79e0bSderaadt 			(void)printf("%-*.*s %-*.*s ",
8835c79e0bSderaadt 			    NAME_WIDTH, UT_NAMESIZE, vs(pn->name),
8935c79e0bSderaadt 			    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 **
1361837a5caSderaadt sort(void)
137df930be7Sderaadt {
138c74ee44fSkstailey 	PERSON *pn, **lp;
139df930be7Sderaadt 	PERSON **list;
140df930be7Sderaadt 
14124b00d2bSmickey 	if (!(list = (PERSON **)malloc((u_int)(entries * sizeof(PERSON *)))))
14224b00d2bSmickey 		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
1501837a5caSderaadt psort(const void *p, const void *t)
151df930be7Sderaadt {
152c74ee44fSkstailey 	return(strcmp((*(PERSON **)p)->name, (*(PERSON **)t)->name));
153df930be7Sderaadt }
154df930be7Sderaadt 
155c74ee44fSkstailey void
1561837a5caSderaadt stimeprint(WHERE *w)
157df930be7Sderaadt {
158c74ee44fSkstailey 	struct tm *delta;
159df930be7Sderaadt 
160df930be7Sderaadt 	delta = gmtime(&w->idletime);
161df930be7Sderaadt 	if (!delta->tm_yday)
162df930be7Sderaadt 		if (!delta->tm_hour)
163df930be7Sderaadt 			if (!delta->tm_min)
164df930be7Sderaadt 				(void)printf("    -");
165df930be7Sderaadt 			else
166df930be7Sderaadt 				(void)printf("%5d", delta->tm_min);
167df930be7Sderaadt 		else
168df930be7Sderaadt 			(void)printf("%2d:%02d",
169df930be7Sderaadt 			    delta->tm_hour, delta->tm_min);
170df930be7Sderaadt 	else
171df930be7Sderaadt 		(void)printf("%4dd", delta->tm_yday);
172df930be7Sderaadt }
173