bba754d75f7fb87791ded230af6f0fc475282a94
[dragonfly.git] / usr.bin / finger / sprint.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  * @(#)sprint.c 8.3 (Berkeley) 4/28/95
37  * $FreeBSD: src/usr.bin/finger/sprint.c,v 1.11.2.5 2002/07/03 01:14:24 des Exp $
38  * $DragonFly: src/usr.bin/finger/sprint.c,v 1.4 2008/02/09 17:12:05 matthias Exp $
39  */
40
41 #include <sys/socket.h>
42 #include <db.h>
43 #include <err.h>
44 #include <langinfo.h>
45 #include <pwd.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <time.h>
49 #include <utmp.h>
50 #include "finger.h"
51
52 static void       stimeprint(WHERE *);
53
54 void
55 sflag_print(void)
56 {
57         PERSON *pn;
58         WHERE *w;
59         int sflag, r, namelen;
60         char p[80];
61         PERSON *tmp;
62         DBT data, key;
63         struct tm *lc;
64
65         if (d_first < 0)
66                 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
67         /*
68          * short format --
69          *      login name
70          *      real name
71          *      terminal name (the XX of ttyXX)
72          *      if terminal writeable (add an '*' to the terminal name
73          *              if not)
74          *      if logged in show idle time and day logged in, else
75          *              show last login date and time.
76          *              If > 6 months, show year instead of time.
77          *      if (-o)
78          *              office location
79          *              office phone
80          *      else
81          *              remote host
82          */
83 #define MAXREALNAME     20
84 #define MAXHOSTNAME     17      /* in reality, hosts are never longer than 16 */
85         (void)printf("%-*s %-*s%s  %s\n", UT_NAMESIZE, "Login", MAXREALNAME,
86             "Name", " TTY  Idle  Login  Time ", (gflag) ? "" :
87             oflag ? "Office  Phone" : "Where");
88
89         for (sflag = R_FIRST;; sflag = R_NEXT) {
90                 r = (*db->seq)(db, &key, &data, sflag);
91                 if (r == -1)
92                         err(1, "db seq");
93                 if (r == 1)
94                         break;
95                 memmove(&tmp, data.data, sizeof tmp);
96                 pn = tmp;
97
98                 for (w = pn->whead; w != NULL; w = w->next) {
99                         namelen = MAXREALNAME;
100                         if (w->info == LOGGEDIN && !w->writable)
101                                 --namelen;      /* leave space before `*' */
102                         (void)printf("%-*.*s %-*.*s", UT_NAMESIZE, UT_NAMESIZE,
103                                 pn->name, MAXREALNAME, namelen,
104                                 pn->realname ? pn->realname : "");
105                         if (!w->loginat) {
106                                 (void)printf("  *     *   No logins   ");
107                                 goto office;
108                         }
109                         (void)putchar(w->info == LOGGEDIN && !w->writable ?
110                             '*' : ' ');
111                         if (*w->tty)
112                                 (void)printf("%-3.3s ",
113                                              (strncmp(w->tty, "tty", 3)
114                                               && strncmp(w->tty, "cua", 3))
115                                              ? w->tty : w->tty + 3);
116                         else
117                                 (void)printf("    ");
118                         if (w->info == LOGGEDIN) {
119                                 stimeprint(w);
120                                 (void)printf("  ");
121                         } else
122                                 (void)printf("    *  ");
123                         lc = localtime(&w->loginat);
124 #define SECSPERDAY 86400
125 #define DAYSPERWEEK 7
126 #define DAYSPERNYEAR 365
127                         if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1)) {
128                                 (void)strftime(p, sizeof(p), "%a", lc);
129                         } else {
130                                 (void)strftime(p, sizeof(p),
131                                              d_first ? "%e %b" : "%b %e", lc);
132                         }
133                         (void)printf("%-6.6s", p);
134                         if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2) {
135                                 (void)strftime(p, sizeof(p), "%Y", lc);
136                         } else {
137                                 (void)strftime(p, sizeof(p), "%R", lc);
138                         }
139                         (void)printf(" %-5.5s ", p);
140 office:
141                         if (gflag)
142                                 goto no_gecos;
143                         if (oflag) {
144                                 if (pn->office)
145                                         (void)printf(" %-7.7s", pn->office);
146                                 else if (pn->officephone)
147                                         (void)printf(" %-7.7s", " ");
148                                 if (pn->officephone)
149                                         (void)printf(" %-.9s",
150                                             prphone(pn->officephone));
151                         } else
152                                 (void)printf(" %.*s", MAXHOSTNAME, w->host);
153 no_gecos:
154                         putchar('\n');
155                 }
156         }
157 }
158
159 static void
160 stimeprint(WHERE *w)
161 {
162         struct tm *delta;
163
164         delta = gmtime(&w->idletime);
165         if (!delta->tm_yday)
166                 if (!delta->tm_hour)
167                         if (!delta->tm_min)
168                                 (void)printf("     ");
169                         else
170                                 (void)printf("%5d", delta->tm_min);
171                 else
172                         (void)printf("%2d:%02d",
173                             delta->tm_hour, delta->tm_min);
174         else
175                 (void)printf("%4dd", delta->tm_yday);
176 }