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