Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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  * @(#)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.2 2003/06/17 04:29:26 dillon Exp $
39  */
40
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <ctype.h>
45 #include <db.h>
46 #include <err.h>
47 #include <fcntl.h>
48 #include <langinfo.h>
49 #include <paths.h>
50 #include <pwd.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <utmp.h>
55 #include "finger.h"
56 #include "pathnames.h"
57 #include "extern.h"
58
59 #define LINE_LEN        80
60 #define TAB_LEN         8               /* 8 spaces between tabs */
61
62 static int      demi_print(char *, int);
63 static void     lprint(PERSON *);
64 static void     vputc(unsigned char);
65
66 void
67 lflag_print()
68 {
69         extern int pplan;
70         PERSON *pn;
71         int sflag, r;
72         PERSON *tmp;
73         DBT data, key;
74
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                         (void)show_text(pn->dir,
88                             _PATH_FORWARD, "Mail forwarded to");
89                         (void)show_text(pn->dir, _PATH_PROJECT, "Project");
90                         if (!show_text(pn->dir, _PATH_PLAN, "Plan"))
91                                 (void)printf("No Plan.\n");
92                         (void)show_text(pn->dir,
93                             _PATH_PUBKEY, "Public key");
94                 }
95         }
96 }
97
98 static void
99 lprint(pn)
100         PERSON *pn;
101 {
102         extern time_t now;
103         struct tm *delta;
104         WHERE *w;
105         int cpr, len, maxlen;
106         struct tm *tp;
107         int oddfield;
108         char t[80];
109
110         if (d_first < 0)
111                 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
112         /*
113          * long format --
114          *      login name
115          *      real name
116          *      home directory
117          *      shell
118          *      office, office phone, home phone if available
119          *      mail status
120          */
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);
124
125         if (gflag)
126                 goto no_gecos;
127         /*
128          * try and print office, office phone, and home phone on one line;
129          * if that fails, do line filling so it looks nice.
130          */
131 #define OFFICE_TAG              "Office"
132 #define OFFICE_PHONE_TAG        "Office Phone"
133         oddfield = 0;
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);
140         } else {
141                 if (pn->office) {
142                         (void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
143                             OFFICE_TAG, pn->office);
144                         oddfield = demi_print(tbuf, oddfield);
145                 }
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);
150                 }
151         }
152         if (pn->homephone) {
153                 (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
154                     prphone(pn->homephone));
155                 oddfield = demi_print(tbuf, oddfield);
156         }
157         if (oddfield)
158                 putchar('\n');
159
160 no_gecos:
161         /*
162          * long format con't:
163          * if logged in
164          *      terminal
165          *      idle time
166          *      if messages allowed
167          *      where logged in from
168          * if not logged in
169          *      when last logged in
170          */
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)
174                         maxlen = len;
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)",
181                             tp);
182                         cpr = printf("On since %s on %s", t, w->tty);
183                         /*
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.
187                          */
188                         delta = gmtime(&w->idletime);
189                         if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
190                                 cpr += printf("%-*s idle ",
191                                     maxlen - (int)strlen(w->tty) + 1, ",");
192                                 if (delta->tm_yday > 0) {
193                                         cpr += printf("%d day%s ",
194                                            delta->tm_yday,
195                                            delta->tm_yday == 1 ? "" : "s");
196                                 }
197                                 cpr += printf("%d:%02d",
198                                     delta->tm_hour, delta->tm_min);
199                                 if (*w->host) {
200                                         putchar(',');
201                                         ++cpr;
202                                 }
203                         }
204                         if (!w->writable)
205                                 cpr += printf(" (messages off)");
206                 } else if (w->loginat == 0) {
207                         cpr = printf("Never logged in.");
208                 } else {
209                         tp = localtime(&w->loginat);
210                         if (now - w->loginat > 86400 * 365 / 2) {
211                                 strftime(t, sizeof(t),
212                                          d_first ? "%a %e %b %R %Y (%Z)" :
213                                                    "%a %b %e %R %Y (%Z)",
214                                          tp);
215                         } else {
216                                 strftime(t, sizeof(t),
217                                          d_first ? "%a %e %b %R (%Z)" :
218                                                    "%a %b %e %R (%Z)",
219                                          tp);
220                         }
221                         cpr = printf("Last login %s on %s", t, w->tty);
222                 }
223                 if (*w->host) {
224                         if (LINE_LEN < (cpr + 6 + strlen(w->host)))
225                                 (void)printf("\n   ");
226                         (void)printf(" from %s", w->host);
227                 }
228                 putchar('\n');
229         }
230         if (pn->mailrecv == -1)
231                 printf("No Mail.\n");
232         else if (pn->mailrecv > pn->mailread) {
233                 tp = localtime(&pn->mailrecv);
234                 strftime(t, sizeof(t),
235                          d_first ? "%a %e %b %R %Y (%Z)" :
236                                    "%a %b %e %R %Y (%Z)",
237                          tp);
238                 printf("New mail received %s\n", t);
239                 tp = localtime(&pn->mailread);
240                 strftime(t, sizeof(t),
241                          d_first ? "%a %e %b %R %Y (%Z)" :
242                                    "%a %b %e %R %Y (%Z)",
243                          tp);
244                 printf("     Unread since %s\n", t);
245         } else {
246                 tp = localtime(&pn->mailread);
247                 strftime(t, sizeof(t),
248                          d_first ? "%a %e %b %R %Y (%Z)" :
249                                    "%a %b %e %R %Y (%Z)",
250                          tp);
251                 printf("Mail last read %s\n", t);
252         }
253 }
254
255 static int
256 demi_print(str, oddfield)
257         char *str;
258         int oddfield;
259 {
260         static int lenlast;
261         int lenthis, maxlen;
262
263         lenthis = strlen(str);
264         if (oddfield) {
265                 /*
266                  * We left off on an odd number of fields.  If we haven't
267                  * crossed the midpoint of the screen, and we have room for
268                  * the next field, print it on the same line; otherwise,
269                  * print it on a new line.
270                  *
271                  * Note: we insist on having the right hand fields start
272                  * no less than 5 tabs out.
273                  */
274                 maxlen = 5 * TAB_LEN;
275                 if (maxlen < lenlast)
276                         maxlen = lenlast;
277                 if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
278                     lenthis) <= LINE_LEN) {
279                         while(lenlast < (4 * TAB_LEN)) {
280                                 putchar('\t');
281                                 lenlast += TAB_LEN;
282                         }
283                         (void)printf("\t%s\n", str);    /* force one tab */
284                 } else {
285                         (void)printf("\n%s", str);      /* go to next line */
286                         oddfield = !oddfield;   /* this'll be undone below */
287                 }
288         } else
289                 (void)printf("%s", str);
290         oddfield = !oddfield;                   /* toggle odd/even marker */
291         lenlast = lenthis;
292         return(oddfield);
293 }
294
295 int
296 show_text(directory, file_name, header)
297         const char *directory, *file_name, *header;
298 {
299         struct stat sb;
300         FILE *fp;
301         int ch, cnt;
302         char *p, lastc;
303         int fd, nr;
304
305         lastc = '\0';
306
307         (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
308         if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
309             sb.st_size == 0)
310                 return(0);
311
312         /* If short enough, and no newlines, show it on a single line.*/
313         if (sb.st_size <= LINE_LEN - strlen(header) - 5) {
314                 nr = read(fd, tbuf, sizeof(tbuf));
315                 if (nr <= 0) {
316                         (void)close(fd);
317                         return(0);
318                 }
319                 for (p = tbuf, cnt = nr; cnt--; ++p)
320                         if (*p == '\n')
321                                 break;
322                 if (cnt <= 1) {
323                         if (*header != '\0')
324                                 (void)printf("%s: ", header);
325                         for (p = tbuf, cnt = nr; cnt--; ++p)
326                                 if (*p != '\r')
327                                         vputc(lastc = *p);
328                         if (lastc != '\n')
329                                 (void)putchar('\n');
330                         (void)close(fd);
331                         return(1);
332                 }
333                 else
334                         (void)lseek(fd, 0L, SEEK_SET);
335         }
336         if ((fp = fdopen(fd, "r")) == NULL)
337                 return(0);
338         if (*header != '\0')
339                 (void)printf("%s:\n", header);
340         while ((ch = getc(fp)) != EOF)
341                 if (ch != '\r')
342                         vputc(lastc = ch);
343         if (lastc != '\n')
344                 (void)putchar('\n');
345         (void)fclose(fp);
346         return(1);
347 }
348
349 static void
350 vputc(ch)
351         unsigned char ch;
352 {
353         int meta;
354
355         if (!isprint(ch) && !isascii(ch)) {
356                 (void)putchar('M');
357                 (void)putchar('-');
358                 ch = toascii(ch);
359                 meta = 1;
360         } else
361                 meta = 0;
362         if (isprint(ch) || (!meta && (ch == ' ' || ch == '\t' || ch == '\n')))
363                 (void)putchar(ch);
364         else {
365                 (void)putchar('^');
366                 (void)putchar(ch == '\177' ? '?' : ch | 0100);
367         }
368 }