1 /* $NetBSD: lprint.c,v 1.23 2013/01/18 22:10:31 christos Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)lprint.c 8.3 (Berkeley) 4/28/95";
40 __RCSID( "$NetBSD: lprint.c,v 1.23 2013/01/18 22:10:31 christos Exp $");
44 #include <sys/types.h>
61 #include "utmpentry.h"
66 #define TAB_LEN 8 /* 8 spaces between tabs */
67 #define _PATH_FORWARD ".forward"
68 #define _PATH_PLAN ".plan"
69 #define _PATH_PROJECT ".project"
71 static int demi_print(const char *, int);
72 static void lprint(PERSON
*);
73 static int show_text(const char *, const char *, const char *);
74 static void vputc(int);
77 #define TIMEZONE(a) tzname[0]
79 #define TIMEZONE(a) (a)->tm_zone
93 for (sflag
= R_FIRST
;; sflag
= R_NEXT
) {
94 r
= (*db
->seq
)(db
, &key
, &data
, sflag
);
99 memmove(&tmp
, data
.data
, sizeof tmp
);
101 if (sflag
!= R_FIRST
)
105 (void)show_text(pn
->dir
,
106 _PATH_FORWARD
, "Mail forwarded to");
107 (void)show_text(pn
->dir
, _PATH_PROJECT
, "Project");
108 if (!show_text(pn
->dir
, _PATH_PLAN
, "Plan"))
109 (void)printf("No Plan.\n");
115 visify(char *buf
, size_t blen
, const char *str
)
117 int len
= strnvisx(buf
, blen
, str
, strlen(str
), VIS_WHITE
|VIS_CSTYLE
);
126 fmt_time(char *buf
, size_t blen
, time_t ti
, time_t n
)
128 struct tm
*tp
= localtime(&ti
);
130 char *t
= asctime(tp
);
131 char *tzn
= TIMEZONE(tp
);
132 if (n
== (time_t)-1 ||
133 n
- ti
> SECSPERDAY
* DAYSPERNYEAR
/ 2)
134 snprintf(buf
, blen
, "%.16s %.4s (%s)", t
, t
+ 20, tzn
);
136 snprintf(buf
, blen
, "%.16s (%s)", t
, tzn
);
138 snprintf(buf
, blen
, "[*bad time 0x%llx*]", (long long)ti
);
142 * idle time is tough; if have one, print a comma,
143 * then spaces to pad out the device name, then the
144 * idle time. Follow with a comma if a remote login.
147 print_idle(time_t t
, int maxlen
, size_t hostlen
, size_t ttylen
) {
150 struct tm
*delta
= gmtime(&t
);
153 return printf("Bad idle 0x%llx", (long long)t
);
155 if (delta
->tm_yday
== 0 && delta
->tm_hour
== 0 && delta
->tm_min
== 0)
158 cpr
= printf("%-*s idle ", (int)(maxlen
- ttylen
+ 1), ",");
159 if (delta
->tm_yday
> 0) {
160 cpr
+= printf("%d day%s ", delta
->tm_yday
,
161 delta
->tm_yday
== 1 ? "" : "s");
163 cpr
+= printf("%d:%02d", delta
->tm_hour
, delta
->tm_min
);
175 int cpr
, len
, maxlen
;
177 char timebuf
[128], ttybuf
[64], hostbuf
[512];
178 size_t ttylen
, hostlen
;
187 * office, office phone, home phone if available
190 (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
191 pn
->name
, pn
->realname
, pn
->dir
);
192 (void)printf("\tShell: %-s\n", *pn
->shell
? pn
->shell
: _PATH_BSHELL
);
197 * try and print office, office phone, and home phone on one line;
198 * if that fails, do line filling so it looks nice.
200 #define OFFICE_TAG "Office"
201 #define OFFICE_PHONE_TAG "Office Phone"
203 if (pn
->office
&& pn
->officephone
&&
204 strlen(pn
->office
) + strlen(pn
->officephone
) +
205 sizeof(OFFICE_TAG
) + 2 <= 5 * TAB_LEN
) {
206 (void)snprintf(timebuf
, sizeof(timebuf
), "%s: %s, %s",
207 OFFICE_TAG
, pn
->office
, prphone(pn
->officephone
));
208 oddfield
= demi_print(timebuf
, oddfield
);
211 (void)snprintf(timebuf
, sizeof(timebuf
), "%s: %s",
212 OFFICE_TAG
, pn
->office
);
213 oddfield
= demi_print(timebuf
, oddfield
);
215 if (pn
->officephone
) {
216 (void)snprintf(timebuf
, sizeof(timebuf
), "%s: %s",
217 OFFICE_PHONE_TAG
, prphone(pn
->officephone
));
218 oddfield
= demi_print(timebuf
, oddfield
);
222 (void)snprintf(timebuf
, sizeof(timebuf
), "%s: %s", "Home Phone",
223 prphone(pn
->homephone
));
224 oddfield
= demi_print(timebuf
, oddfield
);
235 * if messages allowed
236 * where logged in from
238 * when last logged in
240 /* find out longest device name for this user for formatting */
241 for (w
= pn
->whead
, maxlen
= -1; w
!= NULL
; w
= w
->next
) {
242 visify(ttybuf
, sizeof(ttybuf
), w
->tty
);
243 if ((len
= strlen(ttybuf
)) > maxlen
)
246 /* find rest of entries for user */
247 for (w
= pn
->whead
; w
!= NULL
; w
= w
->next
) {
248 ttylen
= visify(ttybuf
, sizeof(ttybuf
), w
->tty
);
249 hostlen
= visify(hostbuf
, sizeof(hostbuf
), w
->host
);
252 fmt_time(timebuf
, sizeof(timebuf
), w
->loginat
, -1);
253 cpr
= printf("On since %s on %s", timebuf
, ttybuf
);
255 cpr
+= print_idle(w
->idletime
, maxlen
, hostlen
,
259 cpr
+= printf(" (messages off)");
262 if (w
->loginat
== 0) {
263 (void)printf("Never logged in.");
266 fmt_time(timebuf
, sizeof(timebuf
), w
->loginat
, now
);
267 cpr
= printf("Last login %s on %s", timebuf
, ttybuf
);
271 if (LINE_LEN
< (cpr
+ 6 + hostlen
))
273 (void)printf(" from %s", hostbuf
);
277 if (pn
->mailrecv
== -1)
278 printf("No Mail.\n");
279 else if (pn
->mailrecv
> pn
->mailread
) {
280 fmt_time(timebuf
, sizeof(timebuf
), pn
->mailrecv
, -1);
281 printf("New mail received %s\n", timebuf
);
282 fmt_time(timebuf
, sizeof(timebuf
), pn
->mailread
, -1);
283 printf(" Unread since %s\n", timebuf
);
285 fmt_time(timebuf
, sizeof(timebuf
), pn
->mailread
, -1);
286 printf("Mail last read %s\n", timebuf
);
291 demi_print(const char *str
, int oddfield
)
296 lenthis
= strlen(str
);
299 * We left off on an odd number of fields. If we haven't
300 * crossed the midpoint of the screen, and we have room for
301 * the next field, print it on the same line; otherwise,
302 * print it on a new line.
304 * Note: we insist on having the right hand fields start
305 * no less than 5 tabs out.
307 maxlen
= 5 * TAB_LEN
;
308 if (maxlen
< lenlast
)
310 if (((((maxlen
/ TAB_LEN
) + 1) * TAB_LEN
) +
311 lenthis
) <= LINE_LEN
) {
312 while(lenlast
< (4 * TAB_LEN
)) {
316 (void)printf("\t%s\n", str
); /* force one tab */
318 (void)printf("\n%s", str
); /* go to next line */
319 oddfield
= !oddfield
; /* this'll be undone below */
322 (void)printf("%s", str
);
323 oddfield
= !oddfield
; /* toggle odd/even marker */
329 show_text(const char *directory
, const char *file_name
, const char *header
)
338 (void)snprintf(tbuf
, sizeof(tbuf
), "%s/%s", directory
, file_name
);
339 if ((fd
= open(tbuf
, O_RDONLY
)) < 0 || fstat(fd
, &sb
) ||
343 /* If short enough, and no newlines, show it on a single line.*/
344 if (sb
.st_size
<= (off_t
)(LINE_LEN
- strlen(header
) - 5)) {
345 nr
= read(fd
, tbuf
, sizeof(tbuf
));
350 for (p
= tbuf
, cnt
= nr
; cnt
--; ++p
)
354 (void)printf("%s: ", header
);
355 for (p
= tbuf
, cnt
= nr
; cnt
--; ++p
)
356 vputc(lastc
= (unsigned char)*p
);
363 (void)lseek(fd
, 0L, SEEK_SET
);
365 if ((fp
= fdopen(fd
, "r")) == NULL
)
367 (void)printf("%s:\n", header
);
368 while ((ch
= getc(fp
)) != EOF
)
381 if (eightflag
|| isprint(ch
) || isspace(ch
)) {
386 vis(visout
, ch
, VIS_SAFE
|VIS_NOSLASH
, 0);
387 for (s2
= visout
; *s2
; s2
++)