xref: /openbsd-src/usr.bin/finger/sprint.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: sprint.c,v 1.13 2009/10/27 23:59:38 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 #include <sys/types.h>
36 #include <sys/time.h>
37 #include <tzfile.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <err.h>
42 #include "finger.h"
43 #include "extern.h"
44 
45 void
46 sflag_print(void)
47 {
48 	PERSON *pn;
49 	WHERE *w;
50 	int cnt;
51 	char *p;
52 	PERSON **list;
53 	struct storage *mem;
54 
55 	list = sort();
56 	/*
57 	 * short format --
58 	 *	login name
59 	 *	real name
60 	 *	terminal name (the XX of ttyXX)
61 	 *	if terminal writeable (add an '*' to the terminal name
62 	 *		if not)
63 	 *	if logged in show idle time and day logged in, else
64 	 *		show last login date and time.  If > 6 months,
65 	 *		show year instead of time.  If < 6 days,
66 	 *		show day name instead of month & day.
67 	 *	if -h given
68 	 *		remote host
69 	 *	else if -o given (overriding -h) (default)
70 	 *		office location
71 	 *		office phone
72 	 */
73 #define NAME_WIDTH	8
74 #define	MAXREALNAME	20
75 #define	MAXHOSTNAME	20
76 	(void)printf("%-*.*s %-*s %s %s\n",
77 	    NAME_WIDTH, UT_NAMESIZE, "Login", MAXREALNAME,
78 	    "Name", "Tty  Idle  Login Time  ",
79 	    (oflag) ? "Office     Office Phone" : "Where");
80 	for (cnt = 0; cnt < entries; ++cnt) {
81 		pn = list[cnt];
82 		for (w = pn->whead; w != NULL; w = w->next) {
83 			mem =  NULL;
84 			(void)printf("%-*.*s %-*.*s ",
85 			    NAME_WIDTH, UT_NAMESIZE, vs(&mem, pn->name),
86 			    MAXREALNAME, MAXREALNAME,
87 			    pn->realname ? vs(&mem, pn->realname) : "");
88 			if (!w->loginat) {
89 				(void)printf("  *     *  No logins   ");
90 				goto office;
91 			}
92 			(void)putchar(w->info == LOGGEDIN && !w->writable ?
93 			    '*' : ' ');
94 			if (*w->tty)
95 				(void)printf("%-2.2s ",
96 				    w->tty[0] != 't' || w->tty[1] != 't' ||
97 				    w->tty[2] != 'y' ? w->tty : w->tty + 3);
98 			else
99 				(void)printf("   ");
100 			if (w->info == LOGGEDIN) {
101 				stimeprint(w);
102 				(void)printf("  ");
103 			} else
104 				(void)printf("    *  ");
105 			p = ctime(&w->loginat);
106 			if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1))
107 				(void)printf("   %.3s", p);
108 			else
109 				(void)printf("%.6s", p + 4);
110 			if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
111 				(void)printf(" %.4s ", p + 20);
112 			else
113 				(void)printf(" %.5s", p + 11);
114 office:
115 			putchar(' ');
116 			if (oflag) {
117 				if (pn->office)
118 					(void)printf("%-10.10s",
119 					    vs(&mem, pn->office));
120 				else if (pn->officephone)
121 					(void)printf("%-10.10s", " ");
122 				if (pn->officephone)
123 					(void)printf(" %-.15s",
124 					    vs(&mem, prphone(pn->officephone)));
125 			} else
126 				(void)printf("%.*s", MAXHOSTNAME, w->host);
127 			putchar('\n');
128 			free_storage(mem);
129 		}
130 	}
131 }
132 
133 PERSON **
134 sort(void)
135 {
136 	PERSON *pn, **lp;
137 	PERSON **list;
138 
139 	if (!(list = (PERSON **)calloc((u_int)entries, sizeof(PERSON *))))
140 		err(1, "malloc");
141 	for (lp = list, pn = phead; pn != NULL; pn = pn->next)
142 		*lp++ = pn;
143 	(void)qsort(list, entries, sizeof(PERSON *), psort);
144 	return (list);
145 }
146 
147 int
148 psort(const void *p, const void *t)
149 {
150 	return (strcmp((*(PERSON **)p)->name, (*(PERSON **)t)->name));
151 }
152 
153 void
154 stimeprint(WHERE *w)
155 {
156 	struct tm *delta;
157 
158 	delta = gmtime(&w->idletime);
159 	if (!delta->tm_yday) {
160 		if (!delta->tm_hour) {
161 			if (!delta->tm_min)
162 				(void)printf("    -");
163 			else
164 				(void)printf("%5d", delta->tm_min);
165 		 } else {
166 			(void)printf("%2d:%02d",
167 			    delta->tm_hour, delta->tm_min);
168 		 }
169 	} else
170 		(void)printf("%4dd", delta->tm_yday);
171 }
172