inet6: require RTF_ANNOUNCE to proxy NS
[dragonfly.git] / usr.bin / finger / lprint.c
blobabec08f9fff447f5152d91f1f7bac553d2a46f25
1 /*
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
10 * are met:
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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * @(#)lprint.c 8.3 (Berkeley) 4/28/95
33 * $FreeBSD: src/usr.bin/finger/lprint.c,v 1.10.2.4 2002/07/03 01:14:24 des Exp $
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/stat.h>
39 #include <sys/time.h>
40 #include <ctype.h>
41 #include <db.h>
42 #include <err.h>
43 #include <fcntl.h>
44 #include <langinfo.h>
45 #include <paths.h>
46 #include <pwd.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include "finger.h"
51 #include "pathnames.h"
52 #include "extern.h"
54 #define LINE_LEN 80
55 #define TAB_LEN 8 /* 8 spaces between tabs */
57 static int demi_print(char *, int);
58 static void lprint(PERSON *);
59 static void vputc(unsigned char);
61 void
62 lflag_print(void)
64 PERSON *pn;
65 int sflag, r;
66 PERSON *tmp;
67 DBT data, key;
69 for (sflag = R_FIRST;; sflag = R_NEXT) {
70 r = (*db->seq)(db, &key, &data, sflag);
71 if (r == -1)
72 err(1, "db seq");
73 if (r == 1)
74 break;
75 memmove(&tmp, data.data, sizeof tmp);
76 pn = tmp;
77 if (sflag != R_FIRST)
78 putchar('\n');
79 lprint(pn);
80 if (!pplan) {
81 show_text(pn->dir,
82 _PATH_FORWARD, "Mail forwarded to");
83 show_text(pn->dir, _PATH_PROJECT, "Project");
84 if (!show_text(pn->dir, _PATH_PLAN, "Plan"))
85 (void)printf("No Plan.\n");
86 show_text(pn->dir,
87 _PATH_PUBKEY, "Public key");
92 static void
93 lprint(PERSON *pn)
95 struct tm *delta;
96 WHERE *w;
97 int cpr, len, maxlen;
98 struct tm *tp;
99 int oddfield;
100 char t[80];
102 if (d_first < 0)
103 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
105 * long format --
106 * login name
107 * real name
108 * home directory
109 * shell
110 * office, office phone, home phone if available
111 * mail status
113 printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
114 pn->name, pn->realname, pn->dir);
115 printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
117 if (gflag)
118 goto no_gecos;
120 * try and print office, office phone, and home phone on one line;
121 * if that fails, do line filling so it looks nice.
123 #define OFFICE_TAG "Office"
124 #define OFFICE_PHONE_TAG "Office Phone"
125 oddfield = 0;
126 if (pn->office && pn->officephone &&
127 strlen(pn->office) + strlen(pn->officephone) +
128 sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
129 snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
130 OFFICE_TAG, pn->office, prphone(pn->officephone));
131 oddfield = demi_print(tbuf, oddfield);
132 } else {
133 if (pn->office) {
134 snprintf(tbuf, sizeof(tbuf), "%s: %s",
135 OFFICE_TAG, pn->office);
136 oddfield = demi_print(tbuf, oddfield);
138 if (pn->officephone) {
139 snprintf(tbuf, sizeof(tbuf), "%s: %s",
140 OFFICE_PHONE_TAG, prphone(pn->officephone));
141 oddfield = demi_print(tbuf, oddfield);
144 if (pn->homephone) {
145 snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
146 prphone(pn->homephone));
147 oddfield = demi_print(tbuf, oddfield);
149 if (oddfield)
150 putchar('\n');
152 no_gecos:
154 * long format con't:
155 * if logged in
156 * terminal
157 * idle time
158 * if messages allowed
159 * where logged in from
160 * if not logged in
161 * when last logged in
163 /* find out longest device name for this user for formatting */
164 for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
165 if ((len = strlen(w->tty)) > maxlen)
166 maxlen = len;
167 /* find rest of entries for user */
168 for (w = pn->whead; w != NULL; w = w->next) {
169 if (w->info == LOGGEDIN) {
170 tp = localtime(&w->loginat);
171 strftime(t, sizeof(t),
172 d_first ? "%a %e %b %R (%Z)" : "%a %b %e %R (%Z)",
173 tp);
174 cpr = printf("On since %s on %s", t, w->tty);
176 * idle time is tough; if have one, print a comma,
177 * then spaces to pad out the device name, then the
178 * idle time. Follow with a comma if a remote login.
180 delta = gmtime(&w->idletime);
181 if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
182 cpr += printf("%-*s idle ",
183 maxlen - (int)strlen(w->tty) + 1, ",");
184 if (delta->tm_yday > 0) {
185 cpr += printf("%d day%s ",
186 delta->tm_yday,
187 delta->tm_yday == 1 ? "" : "s");
189 cpr += printf("%d:%02d",
190 delta->tm_hour, delta->tm_min);
191 if (*w->host) {
192 putchar(',');
193 ++cpr;
196 if (!w->writable)
197 cpr += printf(" (messages off)");
198 } else if (w->loginat == 0) {
199 cpr = printf("Never logged in.");
200 } else {
201 tp = localtime(&w->loginat);
202 if (now - w->loginat > 86400 * 365 / 2) {
203 strftime(t, sizeof(t),
204 d_first ? "%a %e %b %R %Y (%Z)" :
205 "%a %b %e %R %Y (%Z)",
206 tp);
207 } else {
208 strftime(t, sizeof(t),
209 d_first ? "%a %e %b %R (%Z)" :
210 "%a %b %e %R (%Z)",
211 tp);
213 cpr = printf("Last login %s on %s", t, w->tty);
215 if (*w->host) {
216 if (LINE_LEN < (cpr + 6 + strlen(w->host)))
217 (void)printf("\n ");
218 (void)printf(" from %s", w->host);
220 putchar('\n');
222 if (pn->mailrecv == -1)
223 printf("No Mail.\n");
224 else if (pn->mailrecv > pn->mailread) {
225 tp = localtime(&pn->mailrecv);
226 strftime(t, sizeof(t),
227 d_first ? "%a %e %b %R %Y (%Z)" :
228 "%a %b %e %R %Y (%Z)",
229 tp);
230 printf("New mail received %s\n", t);
231 tp = localtime(&pn->mailread);
232 strftime(t, sizeof(t),
233 d_first ? "%a %e %b %R %Y (%Z)" :
234 "%a %b %e %R %Y (%Z)",
235 tp);
236 printf(" Unread since %s\n", t);
237 } else {
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)",
242 tp);
243 printf("Mail last read %s\n", t);
247 static int
248 demi_print(char *str, int oddfield)
250 static int lenlast;
251 int lenthis, maxlen;
253 lenthis = strlen(str);
254 if (oddfield) {
256 * We left off on an odd number of fields. If we haven't
257 * crossed the midpoint of the screen, and we have room for
258 * the next field, print it on the same line; otherwise,
259 * print it on a new line.
261 * Note: we insist on having the right hand fields start
262 * no less than 5 tabs out.
264 maxlen = 5 * TAB_LEN;
265 if (maxlen < lenlast)
266 maxlen = lenlast;
267 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
268 lenthis) <= LINE_LEN) {
269 while(lenlast < (4 * TAB_LEN)) {
270 putchar('\t');
271 lenlast += TAB_LEN;
273 printf("\t%s\n", str); /* force one tab */
274 } else {
275 printf("\n%s", str); /* go to next line */
276 oddfield = !oddfield; /* this'll be undone below */
278 } else
279 printf("%s", str);
280 oddfield = !oddfield; /* toggle odd/even marker */
281 lenlast = lenthis;
282 return(oddfield);
286 show_text(const char *directory, const char *file_name, const char *header)
288 struct stat sb;
289 FILE *fp;
290 int ch, cnt;
291 char *p, lastc;
292 int fd, nr;
294 lastc = '\0';
296 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
297 if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
298 sb.st_size == 0)
299 return(0);
301 /* If short enough, and no newlines, show it on a single line.*/
302 if ((uintptr_t)sb.st_size <= LINE_LEN - strlen(header) - 5) {
303 nr = read(fd, tbuf, sizeof(tbuf));
304 if (nr <= 0) {
305 (void)close(fd);
306 return(0);
308 for (p = tbuf, cnt = nr; cnt--; ++p)
309 if (*p == '\n')
310 break;
311 if (cnt <= 1) {
312 if (*header != '\0')
313 (void)printf("%s: ", header);
314 for (p = tbuf, cnt = nr; cnt--; ++p)
315 if (*p != '\r')
316 vputc(lastc = *p);
317 if (lastc != '\n')
318 (void)putchar('\n');
319 (void)close(fd);
320 return(1);
322 else
323 (void)lseek(fd, 0L, SEEK_SET);
325 if ((fp = fdopen(fd, "r")) == NULL)
326 return(0);
327 if (*header != '\0')
328 (void)printf("%s:\n", header);
329 while ((ch = getc(fp)) != EOF)
330 if (ch != '\r')
331 vputc(lastc = ch);
332 if (lastc != '\n')
333 (void)putchar('\n');
334 (void)fclose(fp);
335 return(1);
338 static void
339 vputc(unsigned char ch)
341 int meta;
343 if (!isprint(ch) && !isascii(ch)) {
344 putchar('M');
345 putchar('-');
346 ch = toascii(ch);
347 meta = 1;
348 } else
349 meta = 0;
350 if (eightflag || isprint(ch) ||
351 (!meta && (ch == ' ' || ch == '\t' || ch == '\n'))) {
352 putchar(ch);
353 } else {
354 putchar('^');
355 putchar(ch == '\177' ? '?' : ch | 0100);