get mxge to build, stage 29/many
[dragonfly.git] / usr.bin / finger / lprint.c
blob51b50f1a75076db925a3d2df924ac865d33c9896
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. 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
34 * SUCH DAMAGE.
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>
42 #include <sys/socket.h>
43 #include <sys/stat.h>
44 #include <sys/time.h>
45 #include <ctype.h>
46 #include <db.h>
47 #include <err.h>
48 #include <fcntl.h>
49 #include <langinfo.h>
50 #include <paths.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <utmp.h>
56 #include "finger.h"
57 #include "pathnames.h"
58 #include "extern.h"
60 #define LINE_LEN 80
61 #define TAB_LEN 8 /* 8 spaces between tabs */
63 static int demi_print(char *, int);
64 static void lprint(PERSON *);
65 static void vputc(unsigned char);
67 void
68 lflag_print(void)
70 PERSON *pn;
71 int sflag, r;
72 PERSON *tmp;
73 DBT data, key;
75 for (sflag = R_FIRST;; sflag = R_NEXT) {
76 r = (*db->seq)(db, &key, &data, sflag);
77 if (r == -1)
78 err(1, "db seq");
79 if (r == 1)
80 break;
81 memmove(&tmp, data.data, sizeof tmp);
82 pn = tmp;
83 if (sflag != R_FIRST)
84 putchar('\n');
85 lprint(pn);
86 if (!pplan) {
87 show_text(pn->dir,
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");
92 show_text(pn->dir,
93 _PATH_PUBKEY, "Public key");
98 static void
99 lprint(PERSON *pn)
101 struct tm *delta;
102 WHERE *w;
103 int cpr, len, maxlen;
104 struct tm *tp;
105 int oddfield;
106 char t[80];
108 if (d_first < 0)
109 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
111 * long format --
112 * login name
113 * real name
114 * home directory
115 * shell
116 * office, office phone, home phone if available
117 * mail status
119 printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
120 pn->name, pn->realname, pn->dir);
121 printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
123 if (gflag)
124 goto no_gecos;
126 * try and print office, office phone, and home phone on one line;
127 * if that fails, do line filling so it looks nice.
129 #define OFFICE_TAG "Office"
130 #define OFFICE_PHONE_TAG "Office Phone"
131 oddfield = 0;
132 if (pn->office && pn->officephone &&
133 strlen(pn->office) + strlen(pn->officephone) +
134 sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
135 snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
136 OFFICE_TAG, pn->office, prphone(pn->officephone));
137 oddfield = demi_print(tbuf, oddfield);
138 } else {
139 if (pn->office) {
140 snprintf(tbuf, sizeof(tbuf), "%s: %s",
141 OFFICE_TAG, pn->office);
142 oddfield = demi_print(tbuf, oddfield);
144 if (pn->officephone) {
145 snprintf(tbuf, sizeof(tbuf), "%s: %s",
146 OFFICE_PHONE_TAG, prphone(pn->officephone));
147 oddfield = demi_print(tbuf, oddfield);
150 if (pn->homephone) {
151 snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
152 prphone(pn->homephone));
153 oddfield = demi_print(tbuf, oddfield);
155 if (oddfield)
156 putchar('\n');
158 no_gecos:
160 * long format con't:
161 * if logged in
162 * terminal
163 * idle time
164 * if messages allowed
165 * where logged in from
166 * if not logged in
167 * when last logged in
169 /* find out longest device name for this user for formatting */
170 for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
171 if ((len = strlen(w->tty)) > maxlen)
172 maxlen = len;
173 /* find rest of entries for user */
174 for (w = pn->whead; w != NULL; w = w->next) {
175 if (w->info == LOGGEDIN) {
176 tp = localtime(&w->loginat);
177 strftime(t, sizeof(t),
178 d_first ? "%a %e %b %R (%Z)" : "%a %b %e %R (%Z)",
179 tp);
180 cpr = printf("On since %s on %s", t, w->tty);
182 * idle time is tough; if have one, print a comma,
183 * then spaces to pad out the device name, then the
184 * idle time. Follow with a comma if a remote login.
186 delta = gmtime(&w->idletime);
187 if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
188 cpr += printf("%-*s idle ",
189 maxlen - (int)strlen(w->tty) + 1, ",");
190 if (delta->tm_yday > 0) {
191 cpr += printf("%d day%s ",
192 delta->tm_yday,
193 delta->tm_yday == 1 ? "" : "s");
195 cpr += printf("%d:%02d",
196 delta->tm_hour, delta->tm_min);
197 if (*w->host) {
198 putchar(',');
199 ++cpr;
202 if (!w->writable)
203 cpr += printf(" (messages off)");
204 } else if (w->loginat == 0) {
205 cpr = printf("Never logged in.");
206 } else {
207 tp = localtime(&w->loginat);
208 if (now - w->loginat > 86400 * 365 / 2) {
209 strftime(t, sizeof(t),
210 d_first ? "%a %e %b %R %Y (%Z)" :
211 "%a %b %e %R %Y (%Z)",
212 tp);
213 } else {
214 strftime(t, sizeof(t),
215 d_first ? "%a %e %b %R (%Z)" :
216 "%a %b %e %R (%Z)",
217 tp);
219 cpr = printf("Last login %s on %s", t, w->tty);
221 if (*w->host) {
222 if (LINE_LEN < (cpr + 6 + strlen(w->host)))
223 (void)printf("\n ");
224 (void)printf(" from %s", w->host);
226 putchar('\n');
228 if (pn->mailrecv == -1)
229 printf("No Mail.\n");
230 else if (pn->mailrecv > pn->mailread) {
231 tp = localtime(&pn->mailrecv);
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("New mail received %s\n", t);
237 tp = localtime(&pn->mailread);
238 strftime(t, sizeof(t),
239 d_first ? "%a %e %b %R %Y (%Z)" :
240 "%a %b %e %R %Y (%Z)",
241 tp);
242 printf(" Unread since %s\n", t);
243 } else {
244 tp = localtime(&pn->mailread);
245 strftime(t, sizeof(t),
246 d_first ? "%a %e %b %R %Y (%Z)" :
247 "%a %b %e %R %Y (%Z)",
248 tp);
249 printf("Mail last read %s\n", t);
253 static int
254 demi_print(char *str, int oddfield)
256 static int lenlast;
257 int lenthis, maxlen;
259 lenthis = strlen(str);
260 if (oddfield) {
262 * We left off on an odd number of fields. If we haven't
263 * crossed the midpoint of the screen, and we have room for
264 * the next field, print it on the same line; otherwise,
265 * print it on a new line.
267 * Note: we insist on having the right hand fields start
268 * no less than 5 tabs out.
270 maxlen = 5 * TAB_LEN;
271 if (maxlen < lenlast)
272 maxlen = lenlast;
273 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
274 lenthis) <= LINE_LEN) {
275 while(lenlast < (4 * TAB_LEN)) {
276 putchar('\t');
277 lenlast += TAB_LEN;
279 printf("\t%s\n", str); /* force one tab */
280 } else {
281 printf("\n%s", str); /* go to next line */
282 oddfield = !oddfield; /* this'll be undone below */
284 } else
285 printf("%s", str);
286 oddfield = !oddfield; /* toggle odd/even marker */
287 lenlast = lenthis;
288 return(oddfield);
292 show_text(const char *directory, const char *file_name, const char *header)
294 struct stat sb;
295 FILE *fp;
296 int ch, cnt;
297 char *p, lastc;
298 int fd, nr;
300 lastc = '\0';
302 (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
303 if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
304 sb.st_size == 0)
305 return(0);
307 /* If short enough, and no newlines, show it on a single line.*/
308 if (sb.st_size <= LINE_LEN - strlen(header) - 5) {
309 nr = read(fd, tbuf, sizeof(tbuf));
310 if (nr <= 0) {
311 (void)close(fd);
312 return(0);
314 for (p = tbuf, cnt = nr; cnt--; ++p)
315 if (*p == '\n')
316 break;
317 if (cnt <= 1) {
318 if (*header != '\0')
319 (void)printf("%s: ", header);
320 for (p = tbuf, cnt = nr; cnt--; ++p)
321 if (*p != '\r')
322 vputc(lastc = *p);
323 if (lastc != '\n')
324 (void)putchar('\n');
325 (void)close(fd);
326 return(1);
328 else
329 (void)lseek(fd, 0L, SEEK_SET);
331 if ((fp = fdopen(fd, "r")) == NULL)
332 return(0);
333 if (*header != '\0')
334 (void)printf("%s:\n", header);
335 while ((ch = getc(fp)) != EOF)
336 if (ch != '\r')
337 vputc(lastc = ch);
338 if (lastc != '\n')
339 (void)putchar('\n');
340 (void)fclose(fp);
341 return(1);
344 static void
345 vputc(unsigned char ch)
347 int meta;
349 if (!isprint(ch) && !isascii(ch)) {
350 putchar('M');
351 putchar('-');
352 ch = toascii(ch);
353 meta = 1;
354 } else
355 meta = 0;
356 if (eightflag || isprint(ch) ||
357 (!meta && (ch == ' ' || ch == '\t' || ch == '\n'))) {
358 putchar(ch);
359 } else {
360 putchar('^');
361 putchar(ch == '\177' ? '?' : ch | 0100);