2 * SPDX-License-Identifier: BSD-3-Clause
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
37 static char sccsid[] = "@(#)lprint.c 8.3 (Berkeley) 4/28/95";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/types.h>
45 #include <sys/socket.h>
60 #include "pathnames.h"
63 #define TAB_LEN 8 /* 8 spaces between tabs */
65 static int demi_print(char *, int);
66 static void lprint(PERSON *);
67 static void vputc(unsigned char);
77 for (sflag = R_FIRST;; sflag = R_NEXT) {
78 r = (*db->seq)(db, &key, &data, sflag);
83 memmove(&tmp, data.data, sizeof tmp);
89 (void)show_text(pn->dir,
90 _PATH_FORWARD, "Mail forwarded to");
91 (void)show_text(pn->dir, _PATH_PROJECT, "Project");
92 if (!show_text(pn->dir, _PATH_PLAN, "Plan"))
93 (void)printf("No Plan.\n");
94 (void)show_text(pn->dir,
95 _PATH_PUBKEY, "Public key");
105 int cpr, len, maxlen;
111 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
118 * office, office phone, home phone if available
121 (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
122 pn->name, pn->realname, pn->dir);
123 (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
128 * try and print office, office phone, and home phone on one line;
129 * if that fails, do line filling so it looks nice.
131 #define OFFICE_TAG "Office"
132 #define OFFICE_PHONE_TAG "Office Phone"
134 if (pn->office && pn->officephone &&
135 strlen(pn->office) + strlen(pn->officephone) +
136 sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
137 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
138 OFFICE_TAG, pn->office, prphone(pn->officephone));
139 oddfield = demi_print(tbuf, oddfield);
142 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
143 OFFICE_TAG, pn->office);
144 oddfield = demi_print(tbuf, oddfield);
146 if (pn->officephone) {
147 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
148 OFFICE_PHONE_TAG, prphone(pn->officephone));
149 oddfield = demi_print(tbuf, oddfield);
153 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
154 prphone(pn->homephone));
155 oddfield = demi_print(tbuf, oddfield);
166 * if messages allowed
167 * where logged in from
169 * when last logged in
171 /* find out longest device name for this user for formatting */
172 for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
173 if ((len = strlen(w->tty)) > maxlen)
175 /* find rest of entries for user */
176 for (w = pn->whead; w != NULL; w = w->next) {
177 if (w->info == LOGGEDIN) {
178 tp = localtime(&w->loginat);
179 strftime(t, sizeof(t),
180 d_first ? "%a %e %b %R (%Z)" : "%a %b %e %R (%Z)",
182 cpr = printf("On since %s on %s", t, w->tty);
184 * idle time is tough; if have one, print a comma,
185 * then spaces to pad out the device name, then the
186 * idle time. Follow with a comma if a remote login.
188 delta = gmtime(&w->idletime);
189 if (w->idletime != -1 && (delta->tm_yday ||
190 delta->tm_hour || delta->tm_min)) {
191 cpr += printf("%-*s idle ",
192 maxlen - (int)strlen(w->tty) + 1, ",");
193 if (delta->tm_yday > 0) {
194 cpr += printf("%d day%s ",
196 delta->tm_yday == 1 ? "" : "s");
198 cpr += printf("%d:%02d",
199 delta->tm_hour, delta->tm_min);
206 cpr += printf(" (messages off)");
207 } else if (w->loginat == 0) {
208 cpr = printf("Never logged in.");
210 tp = localtime(&w->loginat);
211 if (now - w->loginat > 86400 * 365 / 2) {
212 strftime(t, sizeof(t),
213 d_first ? "%a %e %b %R %Y (%Z)" :
214 "%a %b %e %R %Y (%Z)",
217 strftime(t, sizeof(t),
218 d_first ? "%a %e %b %R (%Z)" :
222 cpr = printf("Last login %s on %s", t, w->tty);
225 if (LINE_LEN < (cpr + 6 + strlen(w->host)))
227 (void)printf(" from %s", w->host);
231 if (pn->mailrecv == -1)
232 printf("No Mail.\n");
233 else if (pn->mailrecv > pn->mailread) {
234 tp = localtime(&pn->mailrecv);
235 strftime(t, sizeof(t),
236 d_first ? "%a %e %b %R %Y (%Z)" :
237 "%a %b %e %R %Y (%Z)",
239 printf("New mail received %s\n", t);
240 tp = localtime(&pn->mailread);
241 strftime(t, sizeof(t),
242 d_first ? "%a %e %b %R %Y (%Z)" :
243 "%a %b %e %R %Y (%Z)",
245 printf(" Unread since %s\n", t);
247 tp = localtime(&pn->mailread);
248 strftime(t, sizeof(t),
249 d_first ? "%a %e %b %R %Y (%Z)" :
250 "%a %b %e %R %Y (%Z)",
252 printf("Mail last read %s\n", t);
257 demi_print(char *str, int oddfield)
262 lenthis = strlen(str);
265 * We left off on an odd number of fields. If we haven't
266 * crossed the midpoint of the screen, and we have room for
267 * the next field, print it on the same line; otherwise,
268 * print it on a new line.
270 * Note: we insist on having the right hand fields start
271 * no less than 5 tabs out.
273 maxlen = 5 * TAB_LEN;
274 if (maxlen < lenlast)
276 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
277 lenthis) <= LINE_LEN) {
278 while(lenlast < (4 * TAB_LEN)) {
282 (void)printf("\t%s\n", str); /* force one tab */
284 (void)printf("\n%s", str); /* go to next line */
285 oddfield = !oddfield; /* this'll be undone below */
288 (void)printf("%s", str);
289 oddfield = !oddfield; /* toggle odd/even marker */
295 show_text(const char *directory, const char *file_name, const char *header)
305 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
306 if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
310 /* If short enough, and no newlines, show it on a single line.*/
311 if (sb.st_size <= (off_t)(LINE_LEN - strlen(header) - 5)) {
312 nr = read(fd, tbuf, sizeof(tbuf));
317 for (p = tbuf, cnt = nr; cnt--; ++p)
322 (void)printf("%s: ", header);
323 for (p = tbuf, cnt = nr; cnt--; ++p)
332 (void)lseek(fd, 0L, SEEK_SET);
334 if ((fp = fdopen(fd, "r")) == NULL)
337 (void)printf("%s:\n", header);
338 while ((ch = getc(fp)) != EOF)
348 vputc(unsigned char ch)
352 if (!isprint(ch) && !isascii(ch)) {
359 if (isprint(ch) || (!meta && (ch == ' ' || ch == '\t' || ch == '\n')))
363 (void)putchar(ch == '\177' ? '?' : ch | 0100);