Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / finger / lprint.c
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
7  *
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.
23  *
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.
35  */
36
37 #if 0
38 #ifndef lint
39 static char sccsid[] = "@(#)lprint.c    8.3 (Berkeley) 4/28/95";
40 #endif
41 #endif
42
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD: src/usr.bin/finger/lprint.c,v 1.10.2.4 2002/07/03 01:14:24 des Exp $");
45
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <sys/time.h>
49 #include <ctype.h>
50 #include <db.h>
51 #include <err.h>
52 #include <fcntl.h>
53 #include <langinfo.h>
54 #include <paths.h>
55 #include <pwd.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <unistd.h>
59 #include <utmp.h>
60 #include "finger.h"
61 #include "pathnames.h"
62 #include "extern.h"
63
64 #define LINE_LEN        80
65 #define TAB_LEN         8               /* 8 spaces between tabs */
66
67 static int      demi_print(char *, int);
68 static void     lprint(PERSON *);
69 static void     vputc(unsigned char);
70
71 void
72 lflag_print()
73 {
74         extern int pplan;
75         PERSON *pn;
76         int sflag, r;
77         PERSON *tmp;
78         DBT data, key;
79
80         for (sflag = R_FIRST;; sflag = R_NEXT) {
81                 r = (*db->seq)(db, &key, &data, sflag);
82                 if (r == -1)
83                         err(1, "db seq");
84                 if (r == 1)
85                         break;
86                 memmove(&tmp, data.data, sizeof tmp);
87                 pn = tmp;
88                 if (sflag != R_FIRST)
89                         putchar('\n');
90                 lprint(pn);
91                 if (!pplan) {
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");
99                 }
100         }
101 }
102
103 static void
104 lprint(pn)
105         PERSON *pn;
106 {
107         extern time_t now;
108         struct tm *delta;
109         WHERE *w;
110         int cpr, len, maxlen;
111         struct tm *tp;
112         int oddfield;
113         char t[80];
114
115         if (d_first < 0)
116                 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
117         /*
118          * long format --
119          *      login name
120          *      real name
121          *      home directory
122          *      shell
123          *      office, office phone, home phone if available
124          *      mail status
125          */
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);
129
130         if (gflag)
131                 goto no_gecos;
132         /*
133          * try and print office, office phone, and home phone on one line;
134          * if that fails, do line filling so it looks nice.
135          */
136 #define OFFICE_TAG              "Office"
137 #define OFFICE_PHONE_TAG        "Office Phone"
138         oddfield = 0;
139         if (pn->office && pn->officephone &&
140             strlen(pn->office) + strlen(pn->officephone) +
141             sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
142                 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
143                     OFFICE_TAG, pn->office, prphone(pn->officephone));
144                 oddfield = demi_print(tbuf, oddfield);
145         } else {
146                 if (pn->office) {
147                         (void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
148                             OFFICE_TAG, pn->office);
149                         oddfield = demi_print(tbuf, oddfield);
150                 }
151                 if (pn->officephone) {
152                         (void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
153                             OFFICE_PHONE_TAG, prphone(pn->officephone));
154                         oddfield = demi_print(tbuf, oddfield);
155                 }
156         }
157         if (pn->homephone) {
158                 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
159                     prphone(pn->homephone));
160                 oddfield = demi_print(tbuf, oddfield);
161         }
162         if (oddfield)
163                 putchar('\n');
164
165 no_gecos:
166         /*
167          * long format con't:
168          * if logged in
169          *      terminal
170          *      idle time
171          *      if messages allowed
172          *      where logged in from
173          * if not logged in
174          *      when last logged in
175          */
176         /* find out longest device name for this user for formatting */
177         for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
178                 if ((len = strlen(w->tty)) > maxlen)
179                         maxlen = len;
180         /* find rest of entries for user */
181         for (w = pn->whead; w != NULL; w = w->next) {
182                 if (w->info == LOGGEDIN) {
183                         tp = localtime(&w->loginat);
184                         strftime(t, sizeof(t),
185                             d_first ? "%a %e %b %R (%Z)" : "%a %b %e %R (%Z)",
186                             tp);
187                         cpr = printf("On since %s on %s", t, w->tty);
188                         /*
189                          * idle time is tough; if have one, print a comma,
190                          * then spaces to pad out the device name, then the
191                          * idle time.  Follow with a comma if a remote login.
192                          */
193                         delta = gmtime(&w->idletime);
194                         if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
195                                 cpr += printf("%-*s idle ",
196                                     maxlen - (int)strlen(w->tty) + 1, ",");
197                                 if (delta->tm_yday > 0) {
198                                         cpr += printf("%d day%s ",
199                                            delta->tm_yday,
200                                            delta->tm_yday == 1 ? "" : "s");
201                                 }
202                                 cpr += printf("%d:%02d",
203                                     delta->tm_hour, delta->tm_min);
204                                 if (*w->host) {
205                                         putchar(',');
206                                         ++cpr;
207                                 }
208                         }
209                         if (!w->writable)
210                                 cpr += printf(" (messages off)");
211                 } else if (w->loginat == 0) {
212                         cpr = printf("Never logged in.");
213                 } else {
214                         tp = localtime(&w->loginat);
215                         if (now - w->loginat > 86400 * 365 / 2) {
216                                 strftime(t, sizeof(t),
217                                          d_first ? "%a %e %b %R %Y (%Z)" :
218                                                    "%a %b %e %R %Y (%Z)",
219                                          tp);
220                         } else {
221                                 strftime(t, sizeof(t),
222                                          d_first ? "%a %e %b %R (%Z)" :
223                                                    "%a %b %e %R (%Z)",
224                                          tp);
225                         }
226                         cpr = printf("Last login %s on %s", t, w->tty);
227                 }
228                 if (*w->host) {
229                         if (LINE_LEN < (cpr + 6 + strlen(w->host)))
230                                 (void)printf("\n   ");
231                         (void)printf(" from %s", w->host);
232                 }
233                 putchar('\n');
234         }
235         if (pn->mailrecv == -1)
236                 printf("No Mail.\n");
237         else if (pn->mailrecv > pn->mailread) {
238                 tp = localtime(&pn->mailrecv);
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("New mail received %s\n", t);
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("     Unread since %s\n", t);
250         } else {
251                 tp = localtime(&pn->mailread);
252                 strftime(t, sizeof(t),
253                          d_first ? "%a %e %b %R %Y (%Z)" :
254                                    "%a %b %e %R %Y (%Z)",
255                          tp);
256                 printf("Mail last read %s\n", t);
257         }
258 }
259
260 static int
261 demi_print(str, oddfield)
262         char *str;
263         int oddfield;
264 {
265         static int lenlast;
266         int lenthis, maxlen;
267
268         lenthis = strlen(str);
269         if (oddfield) {
270                 /*
271                  * We left off on an odd number of fields.  If we haven't
272                  * crossed the midpoint of the screen, and we have room for
273                  * the next field, print it on the same line; otherwise,
274                  * print it on a new line.
275                  *
276                  * Note: we insist on having the right hand fields start
277                  * no less than 5 tabs out.
278                  */
279                 maxlen = 5 * TAB_LEN;
280                 if (maxlen < lenlast)
281                         maxlen = lenlast;
282                 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
283                     lenthis) <= LINE_LEN) {
284                         while(lenlast < (4 * TAB_LEN)) {
285                                 putchar('\t');
286                                 lenlast += TAB_LEN;
287                         }
288                         (void)printf("\t%s\n", str);    /* force one tab */
289                 } else {
290                         (void)printf("\n%s", str);      /* go to next line */
291                         oddfield = !oddfield;   /* this'll be undone below */
292                 }
293         } else
294                 (void)printf("%s", str);
295         oddfield = !oddfield;                   /* toggle odd/even marker */
296         lenlast = lenthis;
297         return(oddfield);
298 }
299
300 int
301 show_text(directory, file_name, header)
302         const char *directory, *file_name, *header;
303 {
304         struct stat sb;
305         FILE *fp;
306         int ch, cnt;
307         char *p, lastc;
308         int fd, nr;
309
310         lastc = '\0';
311
312         (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
313         if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
314             sb.st_size == 0)
315                 return(0);
316
317         /* If short enough, and no newlines, show it on a single line.*/
318         if (sb.st_size <= LINE_LEN - strlen(header) - 5) {
319                 nr = read(fd, tbuf, sizeof(tbuf));
320                 if (nr <= 0) {
321                         (void)close(fd);
322                         return(0);
323                 }
324                 for (p = tbuf, cnt = nr; cnt--; ++p)
325                         if (*p == '\n')
326                                 break;
327                 if (cnt <= 1) {
328                         if (*header != '\0')
329                                 (void)printf("%s: ", header);
330                         for (p = tbuf, cnt = nr; cnt--; ++p)
331                                 if (*p != '\r')
332                                         vputc(lastc = *p);
333                         if (lastc != '\n')
334                                 (void)putchar('\n');
335                         (void)close(fd);
336                         return(1);
337                 }
338                 else
339                         (void)lseek(fd, 0L, SEEK_SET);
340         }
341         if ((fp = fdopen(fd, "r")) == NULL)
342                 return(0);
343         if (*header != '\0')
344                 (void)printf("%s:\n", header);
345         while ((ch = getc(fp)) != EOF)
346                 if (ch != '\r')
347                         vputc(lastc = ch);
348         if (lastc != '\n')
349                 (void)putchar('\n');
350         (void)fclose(fp);
351         return(1);
352 }
353
354 static void
355 vputc(ch)
356         unsigned char ch;
357 {
358         int meta;
359
360         if (!isprint(ch) && !isascii(ch)) {
361                 (void)putchar('M');
362                 (void)putchar('-');
363                 ch = toascii(ch);
364                 meta = 1;
365         } else
366                 meta = 0;
367         if (isprint(ch) || (!meta && (ch == ' ' || ch == '\t' || ch == '\n')))
368                 (void)putchar(ch);
369         else {
370                 (void)putchar('^');
371                 (void)putchar(ch == '\177' ? '?' : ch | 0100);
372         }
373 }