Deleted Added
sdiff udiff text old ( 72109 ) new ( 74586 )
full compact
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#ifndef lint
38#if 0
39static char sccsid[] = "@(#)sprint.c 8.3 (Berkeley) 4/28/95";
40#endif
41static const char rcsid[] =
42 "$FreeBSD: head/usr.bin/finger/sprint.c 72109 2001-02-06 20:13:48Z charnier $";
43#endif /* not lint */
44
45#include <db.h>
46#include <err.h>
47#include <pwd.h>
48#include <stdio.h>
49#include <string.h>
50#include <time.h>
51#include <utmp.h>
52#include "finger.h"
53
54static void stimeprint __P((WHERE *));
55
56void
57sflag_print()
58{
59 extern time_t now;
60 extern int oflag;
61 register PERSON *pn;
62 register WHERE *w;
63 register int sflag, r, namelen;
64 char p[80];
65 PERSON *tmp;
66 DBT data, key;
67
68 /*
69 * short format --
70 * login name
71 * real name
72 * terminal name (the XX of ttyXX)
73 * if terminal writeable (add an '*' to the terminal name
74 * if not)
75 * if logged in show idle time and day logged in, else
76 * show last login date and time.
77 * If > 6 months, show year instead of time.
78 * if (-o)
79 * office location
80 * office phone
81 * else
82 * remote host
83 */
84#define MAXREALNAME 20
85#define MAXHOSTNAME 17 /* in reality, hosts are never longer than 16 */
86 (void)printf("%-*s %-*s%s %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
87 "Name", " TTY Idle Login Time",
88 oflag ? " Office Phone" : " Where");
89
90 for (sflag = R_FIRST;; sflag = R_NEXT) {
91 r = (*db->seq)(db, &key, &data, sflag);
92 if (r == -1)
93 err(1, "db seq");
94 if (r == 1)
95 break;
96 memmove(&tmp, data.data, sizeof tmp);
97 pn = tmp;
98
99 for (w = pn->whead; w != NULL; w = w->next) {
100 namelen = MAXREALNAME;
101 if (w->info == LOGGEDIN && !w->writable)
102 --namelen; /* leave space before `*' */
103 (void)printf("%-*.*s %-*.*s", UT_NAMESIZE, UT_NAMESIZE,
104 pn->name, MAXREALNAME, namelen,
105 pn->realname ? pn->realname : "");
106 if (!w->loginat) {
107 (void)printf(" * * No logins ");
108 goto office;
109 }
110 (void)putchar(w->info == LOGGEDIN && !w->writable ?
111 '*' : ' ');
112 if (*w->tty)
113 (void)printf("%-3.3s ",
114 (strncmp(w->tty, "tty", 3)
115 && strncmp(w->tty, "cua", 3))
116 ? w->tty : w->tty + 3);
117 else
118 (void)printf(" ");
119 if (w->info == LOGGEDIN) {
120 stimeprint(w);
121 (void)printf(" ");
122 } else
123 (void)printf(" * ");
124 strftime(p, sizeof(p), "%c", localtime(&w->loginat));
125#define SECSPERDAY 86400
126#define DAYSPERWEEK 7
127#define DAYSPERNYEAR 365
128 if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1))
129 (void)printf("%.3s ", p);
130 else
131 (void)printf("%.6s", p + 4);
132 if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2)
133 (void)printf(" %.4s", p + 20);
134 else
135 (void)printf(" %.5s", p + 11);
136office: if (oflag) {
137 if (pn->office)
138 (void)printf(" %-7.7s", pn->office);
139 else if (pn->officephone)
140 (void)printf(" %-7.7s", " ");
141 if (pn->officephone)
142 (void)printf(" %-.9s",
143 prphone(pn->officephone));
144 } else
145 (void)printf(" %.*s", MAXHOSTNAME, w->host);
146 putchar('\n');
147 }
148 }
149}
150
151static void
152stimeprint(w)
153 WHERE *w;
154{
155 register struct tm *delta;
156
157 delta = gmtime(&w->idletime);
158 if (!delta->tm_yday)
159 if (!delta->tm_hour)
160 if (!delta->tm_min)
161 (void)printf(" ");
162 else
163 (void)printf("%5d", delta->tm_min);
164 else
165 (void)printf("%2d:%02d",
166 delta->tm_hour, delta->tm_min);
167 else
168 (void)printf("%4dd", delta->tm_yday);
169}