2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
39 static char sccsid[] = "@(#)lprint.c 8.3 (Berkeley) 4/28/95";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/types.h>
61 #include "pathnames.h"
65 #define TAB_LEN 8 /* 8 spaces between tabs */
67 static int demi_print __P((char *, int));
68 static void lprint __P((PERSON *));
69 static void vputc __P((unsigned char));
80 for (sflag = R_FIRST;; sflag = R_NEXT) {
81 r = (*db->seq)(db, &key, &data, sflag);
86 memmove(&tmp, data.data, sizeof tmp);
92 (void)show_text(pn->dir,
93 _PATH_FORWARD, "Mail forwarded to");
94 (void)show_text(pn->dir, _PATH_PROJECT, "Project");
95 if (!show_text(pn->dir, _PATH_PLAN, "Plan"))
96 (void)printf("No Plan.\n");
97 (void)show_text(pn->dir,
98 _PATH_PUBKEY, "Public key");
110 int cpr, len, maxlen;
116 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
123 * office, office phone, home phone if available
126 (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
127 pn->name, pn->realname, pn->dir);
128 (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
131 * try and print office, office phone, and home phone on one line;
132 * if that fails, do line filling so it looks nice.
134 #define OFFICE_TAG "Office"
135 #define OFFICE_PHONE_TAG "Office Phone"
137 if (pn->office && pn->officephone &&
138 strlen(pn->office) + strlen(pn->officephone) +
139 sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
140 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
141 OFFICE_TAG, pn->office, prphone(pn->officephone));
142 oddfield = demi_print(tbuf, oddfield);
145 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
146 OFFICE_TAG, pn->office);
147 oddfield = demi_print(tbuf, oddfield);
149 if (pn->officephone) {
150 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
151 OFFICE_PHONE_TAG, prphone(pn->officephone));
152 oddfield = demi_print(tbuf, oddfield);
156 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
157 prphone(pn->homephone));
158 oddfield = demi_print(tbuf, oddfield);
168 * if messages allowed
169 * where logged in from
171 * when last logged in
173 /* find out longest device name for this user for formatting */
174 for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
175 if ((len = strlen(w->tty)) > maxlen)
177 /* find rest of entries for user */
178 for (w = pn->whead; w != NULL; w = w->next) {
179 if (w->info == LOGGEDIN) {
180 tp = localtime(&w->loginat);
181 strftime(t, sizeof(t),
182 d_first ? "%a %e %b %R (%Z)" : "%a %b %e %R (%Z)",
184 cpr = printf("On since %s on %s", t, w->tty);
186 * idle time is tough; if have one, print a comma,
187 * then spaces to pad out the device name, then the
188 * idle time. Follow with a comma if a remote login.
190 delta = gmtime(&w->idletime);
191 if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
192 cpr += printf("%-*s idle ",
193 maxlen - (int)strlen(w->tty) + 1, ",");
194 if (delta->tm_yday > 0) {
195 cpr += printf("%d day%s ",
197 delta->tm_yday == 1 ? "" : "s");
199 cpr += printf("%d:%02d",
200 delta->tm_hour, delta->tm_min);
207 cpr += printf(" (messages off)");
208 } else if (w->loginat == 0) {
209 cpr = printf("Never logged in.");
211 tp = localtime(&w->loginat);
212 if (now - w->loginat > 86400 * 365 / 2) {
213 strftime(t, sizeof(t),
214 d_first ? "%a %e %b %R %Y (%Z)" :
215 "%a %b %e %R %Y (%Z)",
218 strftime(t, sizeof(t),
219 d_first ? "%a %e %b %R (%Z)" :
223 cpr = printf("Last login %s on %s", t, w->tty);
226 if (LINE_LEN < (cpr + 6 + strlen(w->host)))
228 (void)printf(" from %s", w->host);
232 if (pn->mailrecv == -1)
233 printf("No Mail.\n");
234 else if (pn->mailrecv > pn->mailread) {
235 tp = localtime(&pn->mailrecv);
236 strftime(t, sizeof(t),
237 d_first ? "%a %e %b %R %Y (%Z)" :
238 "%a %b %e %R %Y (%Z)",
240 printf("New mail received %s\n", t);
241 tp = localtime(&pn->mailread);
242 strftime(t, sizeof(t),
243 d_first ? "%a %e %b %R %Y (%Z)" :
244 "%a %b %e %R %Y (%Z)",
246 printf(" Unread since %s\n", t);
248 tp = localtime(&pn->mailread);
249 strftime(t, sizeof(t),
250 d_first ? "%a %e %b %R %Y (%Z)" :
251 "%a %b %e %R %Y (%Z)",
253 printf("Mail last read %s\n", t);
258 demi_print(str, oddfield)
265 lenthis = strlen(str);
268 * We left off on an odd number of fields. If we haven't
269 * crossed the midpoint of the screen, and we have room for
270 * the next field, print it on the same line; otherwise,
271 * print it on a new line.
273 * Note: we insist on having the right hand fields start
274 * no less than 5 tabs out.
276 maxlen = 5 * TAB_LEN;
277 if (maxlen < lenlast)
279 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
280 lenthis) <= LINE_LEN) {
281 while(lenlast < (4 * TAB_LEN)) {
285 (void)printf("\t%s\n", str); /* force one tab */
287 (void)printf("\n%s", str); /* go to next line */
288 oddfield = !oddfield; /* this'll be undone below */
291 (void)printf("%s", str);
292 oddfield = !oddfield; /* toggle odd/even marker */
298 show_text(directory, file_name, header)
299 const char *directory, *file_name, *header;
309 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
310 if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
314 /* If short enough, and no newlines, show it on a single line.*/
315 if (sb.st_size <= LINE_LEN - strlen(header) - 5) {
316 nr = read(fd, tbuf, sizeof(tbuf));
321 for (p = tbuf, cnt = nr; cnt--; ++p)
326 (void)printf("%s: ", header);
327 for (p = tbuf, cnt = nr; cnt--; ++p)
336 (void)lseek(fd, 0L, SEEK_SET);
338 if ((fp = fdopen(fd, "r")) == NULL)
341 (void)printf("%s:\n", header);
342 while ((ch = getc(fp)) != EOF)
357 if (!isprint(ch) && !isascii(ch)) {
364 if (isprint(ch) || (!meta && (ch == ' ' || ch == '\t' || ch == '\n')))
368 (void)putchar(ch == '\177' ? '?' : ch | 0100);