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