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