Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / usr.bin / last / want.c
1 /*      $NetBSD: last.c,v 1.15 2000/06/30 06:19:58 simonb Exp $ */
2
3 /*
4  * Copyright (c) 1987, 1993, 1994
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by the University of
18  *      California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 static struct utmp *buf;
36
37 static void onintr(int);
38 static int want(struct utmp *, int);
39
40 /*
41  * wtmp --
42  *      read through the wtmp file
43  */
44 void
45 wtmp(const char *file, int namesz, int linesz, int hostsz)
46 {
47         struct utmp     *bp;            /* current structure */
48         TTY     *T;                     /* tty list entry */
49         struct stat     stb;            /* stat of file for sz */
50         time_t  delta;                  /* time difference */
51         off_t   bl;
52         int     bytes, wfd;
53         char    *ct;
54         const char *crmsg;
55         size_t  len = sizeof(*buf) * MAXUTMP;
56
57         if ((buf = malloc(len)) == NULL)
58                 err(1, "Cannot allocate utmp buffer");
59
60         crmsg = NULL;
61
62         if ((wfd = open(file, O_RDONLY, 0)) < 0 || fstat(wfd, &stb) == -1)
63                 err(1, "%s", file);
64         bl = (stb.st_size + len - 1) / len;
65
66         buf[FIRSTVALID].ut_timefld = time(NULL);
67         (void)signal(SIGINT, onintr);
68         (void)signal(SIGQUIT, onintr);
69
70         while (--bl >= 0) {
71                 if (lseek(wfd, bl * len, SEEK_SET) == -1 ||
72                     (bytes = read(wfd, buf, len)) == -1)
73                         err(1, "%s", file);
74                 for (bp = &buf[bytes / sizeof(*buf) - 1]; bp >= buf; --bp) {
75                         /*
76                          * if the terminal line is '~', the machine stopped.
77                          * see utmp(5) for more info.
78                          */
79                         if (bp->ut_line[0] == '~' && !bp->ut_line[1]) {
80                                 /* everybody just logged out */
81                                 for (T = ttylist; T; T = T->next)
82                                         T->logout = -bp->ut_timefld;
83                                 currentout = -bp->ut_timefld;
84                                 crmsg = strncmp(bp->ut_name, "shutdown",
85                                     namesz) ? "crash" : "shutdown";
86                                 if (want(bp, NO)) {
87                                         ct = fmttime(bp->ut_timefld, fulltime);
88                                         printf("%-*.*s  %-*.*s %-*.*s %s\n",
89                                             namesz, namesz, bp->ut_name,
90                                             linesz, linesz, bp->ut_line,
91                                             hostsz, hostsz, bp->ut_host, ct);
92                                         if (maxrec != -1 && !--maxrec)
93                                                 return;
94                                 }
95                                 continue;
96                         }
97                         /*
98                          * if the line is '{' or '|', date got set; see
99                          * utmp(5) for more info.
100                          */
101                         if ((bp->ut_line[0] == '{' || bp->ut_line[0] == '|')
102                             && !bp->ut_line[1]) {
103                                 if (want(bp, NO)) {
104                                         ct = fmttime(bp->ut_timefld, fulltime);
105                                 printf("%-*.*s  %-*.*s %-*.*s %s\n",
106                                     namesz, namesz,
107                                     bp->ut_name,
108                                     linesz, linesz,
109                                     bp->ut_line,
110                                     hostsz, hostsz,
111                                     bp->ut_host,
112                                     ct);
113                                         if (maxrec && !--maxrec)
114                                                 return;
115                                 }
116                                 continue;
117                         }
118                         /* find associated tty */
119                         for (T = ttylist;; T = T->next) {
120                                 if (!T) {
121                                         /* add new one */
122                                         T = addtty(bp->ut_line);
123                                         break;
124                                 }
125                                 if (!strncmp(T->tty, bp->ut_line, LINESIZE))
126                                         break;
127                         }
128                         if (TYPE(bp) == SIGNATURE)
129                                 continue;
130                         if (bp->ut_name[0] && want(bp, YES)) {
131                                 ct = fmttime(bp->ut_timefld, fulltime);
132                                 printf("%-*.*s  %-*.*s %-*.*s %s ",
133                                     namesz, namesz, bp->ut_name,
134                                     linesz, linesz, bp->ut_line,
135                                     hostsz, hostsz, bp->ut_host,
136                                     ct);
137                                 if (!T->logout)
138                                         puts("  still logged in");
139                                 else {
140                                         if (T->logout < 0) {
141                                                 T->logout = -T->logout;
142                                                 printf("- %s", crmsg);
143                                         }
144                                         else
145                                                 printf("- %s",
146                                                     fmttime(T->logout,
147                                                     fulltime | TIMEONLY));
148                                         delta = T->logout - bp->ut_timefld;
149                                         if (delta < SECSPERDAY)
150                                                 printf("  (%s)\n",
151                                                     fmttime(delta,
152                                                     fulltime | TIMEONLY | GMT));
153                                         else
154                                                 printf(" (%ld+%s)\n",
155                                                     delta / SECSPERDAY,
156                                                     fmttime(delta,
157                                                     fulltime | TIMEONLY | GMT));
158                                 }
159                                 if (maxrec != -1 && !--maxrec)
160                                         return;
161                         }
162                         T->logout = bp->ut_timefld;
163                 }
164         }
165         fulltime = 1;   /* show full time */
166         crmsg = fmttime(buf[FIRSTVALID].ut_timefld, FULLTIME);
167         if ((ct = strrchr(file, '/')) != NULL)
168                 ct++;
169         printf("\n%s begins %s\n", ct ? ct : file, crmsg);
170 }
171
172 /*
173  * want --
174  *      see if want this entry
175  */
176 static int
177 want(struct utmp *bp, int check)
178 {
179         ARG *step;
180
181         if (check) {
182                 /*
183                  * when uucp and ftp log in over a network, the entry in
184                  * the utmp file is the name plus their process id.  See
185                  * etc/ftpd.c and usr.bin/uucp/uucpd.c for more information.
186                  */
187                 if (!strncmp(bp->ut_line, "ftp", sizeof("ftp") - 1))
188                         bp->ut_line[3] = '\0';
189                 else if (!strncmp(bp->ut_line, "uucp", sizeof("uucp") - 1))
190                         bp->ut_line[4] = '\0';
191         }
192         if (!arglist)
193                 return (YES);
194
195         for (step = arglist; step; step = step->next)
196                 switch(step->type) {
197                 case HOST_TYPE:
198                         if (!strncasecmp(step->name, bp->ut_host, HOSTSIZE))
199                                 return (YES);
200                         break;
201                 case TTY_TYPE:
202                         if (!strncmp(step->name, bp->ut_line, LINESIZE))
203                                 return (YES);
204                         break;
205                 case USER_TYPE:
206                         if (!strncmp(step->name, bp->ut_name, NAMESIZE))
207                                 return (YES);
208                         break;
209         }
210         return (NO);
211 }
212
213 /*
214  * onintr --
215  *      on interrupt, we inform the user how far we've gotten
216  */
217 static void
218 onintr(int signo)
219 {
220
221         printf("\ninterrupted %s\n", fmttime(buf[FIRSTVALID].ut_timefld,
222             FULLTIME));
223         if (signo == SIGINT)
224                 exit(1);
225         (void)fflush(stdout);           /* fix required for rsh */
226 }