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
36 * @(#)lprint.c 8.3 (Berkeley) 4/28/95
37 * $FreeBSD: src/usr.bin/finger/lprint.c,v 1.10.2.4 2002/07/03 01:14:24 des Exp $
38 * $DragonFly: src/usr.bin/finger/lprint.c,v 1.4 2004/09/03 19:13:23 dillon Exp $
41 #include <sys/types.h>
56 #include "pathnames.h"
60 #define TAB_LEN 8 /* 8 spaces between tabs */
62 static int demi_print(char *, int);
63 static void lprint(PERSON *);
64 static void vputc(unsigned char);
75 for (sflag = R_FIRST;; sflag = R_NEXT) {
76 r = (*db->seq)(db, &key, &data, sflag);
81 memmove(&tmp, data.data, sizeof tmp);
88 _PATH_FORWARD, "Mail forwarded to");
89 show_text(pn->dir, _PATH_PROJECT, "Project");
90 if (!show_text(pn->dir, _PATH_PLAN, "Plan"))
91 (void)printf("No Plan.\n");
93 _PATH_PUBKEY, "Public key");
104 int cpr, len, maxlen;
110 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
117 * office, office phone, home phone if available
120 printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
121 pn->name, pn->realname, pn->dir);
122 printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
127 * try and print office, office phone, and home phone on one line;
128 * if that fails, do line filling so it looks nice.
130 #define OFFICE_TAG "Office"
131 #define OFFICE_PHONE_TAG "Office Phone"
133 if (pn->office && pn->officephone &&
134 strlen(pn->office) + strlen(pn->officephone) +
135 sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
136 snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
137 OFFICE_TAG, pn->office, prphone(pn->officephone));
138 oddfield = demi_print(tbuf, oddfield);
141 snprintf(tbuf, sizeof(tbuf), "%s: %s",
142 OFFICE_TAG, pn->office);
143 oddfield = demi_print(tbuf, oddfield);
145 if (pn->officephone) {
146 snprintf(tbuf, sizeof(tbuf), "%s: %s",
147 OFFICE_PHONE_TAG, prphone(pn->officephone));
148 oddfield = demi_print(tbuf, oddfield);
152 snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
153 prphone(pn->homephone));
154 oddfield = demi_print(tbuf, oddfield);
165 * if messages allowed
166 * where logged in from
168 * when last logged in
170 /* find out longest device name for this user for formatting */
171 for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
172 if ((len = strlen(w->tty)) > maxlen)
174 /* find rest of entries for user */
175 for (w = pn->whead; w != NULL; w = w->next) {
176 if (w->info == LOGGEDIN) {
177 tp = localtime(&w->loginat);
178 strftime(t, sizeof(t),
179 d_first ? "%a %e %b %R (%Z)" : "%a %b %e %R (%Z)",
181 cpr = printf("On since %s on %s", t, w->tty);
183 * idle time is tough; if have one, print a comma,
184 * then spaces to pad out the device name, then the
185 * idle time. Follow with a comma if a remote login.
187 delta = gmtime(&w->idletime);
188 if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
189 cpr += printf("%-*s idle ",
190 maxlen - (int)strlen(w->tty) + 1, ",");
191 if (delta->tm_yday > 0) {
192 cpr += printf("%d day%s ",
194 delta->tm_yday == 1 ? "" : "s");
196 cpr += printf("%d:%02d",
197 delta->tm_hour, delta->tm_min);
204 cpr += printf(" (messages off)");
205 } else if (w->loginat == 0) {
206 cpr = printf("Never logged in.");
208 tp = localtime(&w->loginat);
209 if (now - w->loginat > 86400 * 365 / 2) {
210 strftime(t, sizeof(t),
211 d_first ? "%a %e %b %R %Y (%Z)" :
212 "%a %b %e %R %Y (%Z)",
215 strftime(t, sizeof(t),
216 d_first ? "%a %e %b %R (%Z)" :
220 cpr = printf("Last login %s on %s", t, w->tty);
223 if (LINE_LEN < (cpr + 6 + strlen(w->host)))
225 (void)printf(" from %s", w->host);
229 if (pn->mailrecv == -1)
230 printf("No Mail.\n");
231 else if (pn->mailrecv > pn->mailread) {
232 tp = localtime(&pn->mailrecv);
233 strftime(t, sizeof(t),
234 d_first ? "%a %e %b %R %Y (%Z)" :
235 "%a %b %e %R %Y (%Z)",
237 printf("New mail received %s\n", t);
238 tp = localtime(&pn->mailread);
239 strftime(t, sizeof(t),
240 d_first ? "%a %e %b %R %Y (%Z)" :
241 "%a %b %e %R %Y (%Z)",
243 printf(" Unread since %s\n", t);
245 tp = localtime(&pn->mailread);
246 strftime(t, sizeof(t),
247 d_first ? "%a %e %b %R %Y (%Z)" :
248 "%a %b %e %R %Y (%Z)",
250 printf("Mail last read %s\n", t);
255 demi_print(char *str, int oddfield)
260 lenthis = strlen(str);
263 * We left off on an odd number of fields. If we haven't
264 * crossed the midpoint of the screen, and we have room for
265 * the next field, print it on the same line; otherwise,
266 * print it on a new line.
268 * Note: we insist on having the right hand fields start
269 * no less than 5 tabs out.
271 maxlen = 5 * TAB_LEN;
272 if (maxlen < lenlast)
274 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
275 lenthis) <= LINE_LEN) {
276 while(lenlast < (4 * TAB_LEN)) {
280 printf("\t%s\n", str); /* force one tab */
282 printf("\n%s", str); /* go to next line */
283 oddfield = !oddfield; /* this'll be undone below */
287 oddfield = !oddfield; /* toggle odd/even marker */
293 show_text(const char *directory, const char *file_name, const char *header)
303 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
304 if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
308 /* If short enough, and no newlines, show it on a single line.*/
309 if (sb.st_size <= LINE_LEN - strlen(header) - 5) {
310 nr = read(fd, tbuf, sizeof(tbuf));
315 for (p = tbuf, cnt = nr; cnt--; ++p)
320 (void)printf("%s: ", header);
321 for (p = tbuf, cnt = nr; cnt--; ++p)
330 (void)lseek(fd, 0L, SEEK_SET);
332 if ((fp = fdopen(fd, "r")) == NULL)
335 (void)printf("%s:\n", header);
336 while ((ch = getc(fp)) != EOF)
346 vputc(unsigned char ch)
350 if (!isprint(ch) && !isascii(ch)) {
357 if (eightflag || isprint(ch) ||
358 (!meta && (ch == ' ' || ch == '\t' || ch == '\n'))) {
362 putchar(ch == '\177' ? '?' : ch | 0100);