1 /* $NetBSD: lprint.c,v 1.21 2006/01/04 01:17:54 perry 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.21 2006/01/04 01:17:54 perry 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");
119 int cpr
, len
, maxlen
;
132 * office, office phone, home phone if available
135 (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
136 pn
->name
, pn
->realname
, pn
->dir
);
137 (void)printf("\tShell: %-s\n", *pn
->shell
? pn
->shell
: _PATH_BSHELL
);
142 * try and print office, office phone, and home phone on one line;
143 * if that fails, do line filling so it looks nice.
145 #define OFFICE_TAG "Office"
146 #define OFFICE_PHONE_TAG "Office Phone"
148 if (pn
->office
&& pn
->officephone
&&
149 strlen(pn
->office
) + strlen(pn
->officephone
) +
150 sizeof(OFFICE_TAG
) + 2 <= 5 * TAB_LEN
) {
151 (void)snprintf(tbuf
, sizeof(tbuf
), "%s: %s, %s",
152 OFFICE_TAG
, pn
->office
, prphone(pn
->officephone
));
153 oddfield
= demi_print(tbuf
, oddfield
);
156 (void)snprintf(tbuf
, sizeof(tbuf
), "%s: %s",
157 OFFICE_TAG
, pn
->office
);
158 oddfield
= demi_print(tbuf
, oddfield
);
160 if (pn
->officephone
) {
161 (void)snprintf(tbuf
, sizeof(tbuf
), "%s: %s",
162 OFFICE_PHONE_TAG
, prphone(pn
->officephone
));
163 oddfield
= demi_print(tbuf
, oddfield
);
167 (void)snprintf(tbuf
, sizeof(tbuf
), "%s: %s", "Home Phone",
168 prphone(pn
->homephone
));
169 oddfield
= demi_print(tbuf
, oddfield
);
180 * if messages allowed
181 * where logged in from
183 * when last logged in
185 /* find out longest device name for this user for formatting */
186 for (w
= pn
->whead
, maxlen
= -1; w
!= NULL
; w
= w
->next
)
187 if ((len
= strlen(w
->tty
)) > maxlen
)
189 /* find rest of entries for user */
190 for (w
= pn
->whead
; w
!= NULL
; w
= w
->next
) {
193 tp
= localtime(&w
->loginat
);
196 cpr
= printf("On since %.16s (%s) on %s",
199 * idle time is tough; if have one, print a comma,
200 * then spaces to pad out the device name, then the
201 * idle time. Follow with a comma if a remote login.
203 delta
= gmtime(&w
->idletime
);
204 if (delta
->tm_yday
|| delta
->tm_hour
|| delta
->tm_min
) {
205 cpr
+= printf("%-*s idle ",
206 (int)(maxlen
- strlen(w
->tty
) + 1), ",");
207 if (delta
->tm_yday
> 0) {
208 cpr
+= printf("%d day%s ",
210 delta
->tm_yday
== 1 ? "" : "s");
212 cpr
+= printf("%d:%02d",
213 delta
->tm_hour
, delta
->tm_min
);
220 cpr
+= printf(" (messages off)");
223 if (w
->loginat
== 0) {
224 (void)printf("Never logged in.");
227 tp
= localtime(&w
->loginat
);
230 if (now
- w
->loginat
> SECSPERDAY
* DAYSPERNYEAR
/ 2)
232 printf("Last login %.16s %.4s (%s) on %s",
233 t
, t
+ 20, tzn
, w
->tty
);
235 cpr
= printf("Last login %.16s (%s) on %s",
240 if (LINE_LEN
< (cpr
+ 6 + strlen(w
->host
)))
242 (void)printf(" from %s", w
->host
);
246 if (pn
->mailrecv
== -1)
247 printf("No Mail.\n");
248 else if (pn
->mailrecv
> pn
->mailread
) {
249 tp
= localtime(&pn
->mailrecv
);
252 printf("New mail received %.16s %.4s (%s)\n", t
, t
+ 20, tzn
);
253 tp
= localtime(&pn
->mailread
);
256 printf(" Unread since %.16s %.4s (%s)\n", t
, t
+ 20, tzn
);
258 tp
= localtime(&pn
->mailread
);
261 printf("Mail last read %.16s %.4s (%s)\n", t
, t
+ 20, tzn
);
266 demi_print(const char *str
, int oddfield
)
271 lenthis
= strlen(str
);
274 * We left off on an odd number of fields. If we haven't
275 * crossed the midpoint of the screen, and we have room for
276 * the next field, print it on the same line; otherwise,
277 * print it on a new line.
279 * Note: we insist on having the right hand fields start
280 * no less than 5 tabs out.
282 maxlen
= 5 * TAB_LEN
;
283 if (maxlen
< lenlast
)
285 if (((((maxlen
/ TAB_LEN
) + 1) * TAB_LEN
) +
286 lenthis
) <= LINE_LEN
) {
287 while(lenlast
< (4 * TAB_LEN
)) {
291 (void)printf("\t%s\n", str
); /* force one tab */
293 (void)printf("\n%s", str
); /* go to next line */
294 oddfield
= !oddfield
; /* this'll be undone below */
297 (void)printf("%s", str
);
298 oddfield
= !oddfield
; /* toggle odd/even marker */
304 show_text(const char *directory
, const char *file_name
, const char *header
)
313 (void)snprintf(tbuf
, sizeof(tbuf
), "%s/%s", directory
, file_name
);
314 if ((fd
= open(tbuf
, O_RDONLY
)) < 0 || fstat(fd
, &sb
) ||
318 /* If short enough, and no newlines, show it on a single line.*/
319 if (sb
.st_size
<= (off_t
)(LINE_LEN
- strlen(header
) - 5)) {
320 nr
= read(fd
, tbuf
, sizeof(tbuf
));
325 for (p
= tbuf
, cnt
= nr
; cnt
--; ++p
)
329 (void)printf("%s: ", header
);
330 for (p
= tbuf
, cnt
= nr
; cnt
--; ++p
)
331 vputc(lastc
= (unsigned char)*p
);
338 (void)lseek(fd
, 0L, SEEK_SET
);
340 if ((fp
= fdopen(fd
, "r")) == NULL
)
342 (void)printf("%s:\n", header
);
343 while ((ch
= getc(fp
)) != EOF
)
356 if (eightflag
|| isprint(ch
) || isspace(ch
)) {
361 vis(visout
, ch
, VIS_SAFE
|VIS_NOSLASH
, 0);
362 for (s2
= visout
; *s2
; s2
++)