last.c revision 1.16 1 /* $NetBSD: last.c,v 1.16 2003/02/17 15:08:57 christos 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
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT(
39 "@(#) Copyright (c) 1987, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n");
41 #endif /* not lint */
42
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)last.c 8.2 (Berkeley) 4/2/94";
46 #endif
47 __RCSID("$NetBSD: last.c,v 1.16 2003/02/17 15:08:57 christos Exp $");
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/stat.h>
52
53 #include <err.h>
54 #include <fcntl.h>
55 #include <paths.h>
56 #include <signal.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <time.h>
61 #include <tzfile.h>
62 #include <unistd.h>
63 #ifdef SUPPORT_UTMPX
64 #include <utmpx.h>
65 #endif
66 #ifdef SUPPORT_UTMP
67 #include <utmp.h>
68 #endif
69
70 #ifndef UT_NAMESIZE
71 #define UT_NAMESIZE 8
72 #define UT_LINESIZE 8
73 #define UT_HOSTSIZE 16
74 #endif
75 #ifndef SIGNATURE
76 #define SIGNATURE -1
77 #endif
78
79
80
81 #define NO 0 /* false/no */
82 #define YES 1 /* true/yes */
83
84 #define TBUFLEN 30 /* length of time string buffer */
85 #define TFMT "%a %b %d %R" /* strftime format string */
86 #define LTFMT "%a %b %d %Y %T" /* strftime long format string */
87 #define TFMTS "%R" /* strftime format string - time only */
88 #define LTFMTS "%T" /* strftime long format string - " */
89
90 /* fmttime() flags */
91 #define FULLTIME 0x1 /* show year, seconds */
92 #define TIMEONLY 0x2 /* show time only, not date */
93 #define GMT 0x4 /* show time at GMT, for offsets only */
94
95 #define MAXUTMP 1024;
96
97 typedef struct arg {
98 char *name; /* argument */
99 #define HOST_TYPE -2
100 #define TTY_TYPE -3
101 #define USER_TYPE -4
102 int type; /* type of arg */
103 struct arg *next; /* linked list pointer */
104 } ARG;
105 static ARG *arglist; /* head of linked list */
106
107 typedef struct ttytab {
108 time_t logout; /* log out time */
109 char tty[128]; /* terminal name */
110 struct ttytab *next; /* linked list pointer */
111 } TTY;
112 static TTY *ttylist; /* head of linked list */
113
114 static time_t currentout; /* current logout value */
115 static long maxrec; /* records to display */
116 static int fulltime = 0; /* Display seconds? */
117
118 int main(int, char *[]);
119
120 static void addarg(int, char *);
121 static TTY *addtty(const char *);
122 static void hostconv(char *);
123 static char *ttyconv(char *);
124 #ifdef SUPPORT_UTMPX
125 static void wtmpx(const char *, int, int, int);
126 #endif
127 #ifdef SUPPORT_UTMP
128 static void wtmp(const char *, int, int, int);
129 #endif
130 static char *fmttime(time_t, int);
131 static void usage(void);
132
133 static
134 void usage(void)
135 {
136 (void)fprintf(stderr, "Usage: %s [-#%s] [-f file] [-t tty]"
137 " [-h hostname] [-T] [user ...]\n", getprogname(),
138 #ifdef SUPPORT_UTMPX
139 "w"
140 #else
141 ""
142 #endif
143 );
144 exit(1);
145 }
146
147 int
148 main(int argc, char *argv[])
149 {
150 int ch;
151 char *p;
152 char *file = NULL;
153 int namesize = UT_NAMESIZE;
154 int linesize = UT_LINESIZE;
155 int hostsize = UT_HOSTSIZE;
156
157 maxrec = -1;
158
159 while ((ch = getopt(argc, argv, "0123456789f:h:H:L:N:t:T")) != -1)
160 switch (ch) {
161 case '0': case '1': case '2': case '3': case '4':
162 case '5': case '6': case '7': case '8': case '9':
163 /*
164 * kludge: last was originally designed to take
165 * a number after a dash.
166 */
167 if (maxrec == -1) {
168 p = argv[optind - 1];
169 if (p[0] == '-' && p[1] == ch && !p[2])
170 maxrec = atol(++p);
171 else
172 maxrec = atol(argv[optind] + 1);
173 if (!maxrec)
174 exit(0);
175 }
176 break;
177 case 'f':
178 file = optarg;
179 break;
180 case 'h':
181 hostconv(optarg);
182 addarg(HOST_TYPE, optarg);
183 break;
184 case 't':
185 addarg(TTY_TYPE, ttyconv(optarg));
186 break;
187 case 'U':
188 namesize = atoi(optarg);
189 break;
190 case 'L':
191 linesize = atoi(optarg);
192 break;
193 case 'H':
194 hostsize = atoi(optarg);
195 break;
196 case 'T':
197 fulltime = 1;
198 break;
199 case '?':
200 default:
201 usage();
202 }
203
204 if (argc) {
205 setlinebuf(stdout);
206 for (argv += optind; *argv; ++argv) {
207 #define COMPATIBILITY
208 #ifdef COMPATIBILITY
209 /* code to allow "last p5" to work */
210 addarg(TTY_TYPE, ttyconv(*argv));
211 #endif
212 addarg(USER_TYPE, *argv);
213 }
214 }
215 if (file == NULL) {
216 #ifdef SUPPORT_UTMPX
217 if (access(_PATH_WTMPX, R_OK) == 0)
218 file = _PATH_WTMPX;
219 else
220 #endif
221 #ifdef SUPPORT_UTMP
222 if (access(_PATH_WTMP, R_OK) == 0)
223 file = _PATH_WTMP;
224 #endif
225 if (file == NULL)
226 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
227 errx(1, "Cannot access `%s' or `%s'", _PATH_WTMPX,
228 _PATH_WTMP);
229 #elif defined(SUPPORT_UTMPX)
230 errx(1, "Cannot access `%s'", _PATH_WTMPX);
231 #elif defined(SUPPORT_UTMP)
232 errx(1, "Cannot access `%s'", _PATH_WTMP);
233 #else
234 errx(1, "No utmp or utmpx support compiled in.");
235 #endif
236 }
237 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
238 if (file[strlen(file) - 1] == 'x')
239 wtmpx(file, namesize, linesize, hostsize);
240 else
241 wtmp(file, namesize, linesize, hostsize);
242 #elif defined(SUPPORT_UTMPX)
243 wtmpx(file, namesize, linesize, hostsize);
244 #elif defined(SUPPORT_UTMP)
245 wtmp(file, namesize, linesize, hostsize);
246 #else
247 errx(1, "Nu utmp or utmpx support compiled in.");
248 #endif
249 exit(0);
250 }
251
252
253 /*
254 * addarg --
255 * add an entry to a linked list of arguments
256 */
257 static void
258 addarg(int type, char *arg)
259 {
260 ARG *cur;
261
262 if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
263 err(1, "malloc failure");
264 cur->next = arglist;
265 cur->type = type;
266 cur->name = arg;
267 arglist = cur;
268 }
269
270 /*
271 * addtty --
272 * add an entry to a linked list of ttys
273 */
274 static TTY *
275 addtty(const char *ttyname)
276 {
277 TTY *cur;
278
279 if (!(cur = (TTY *)malloc((u_int)sizeof(TTY))))
280 err(1, "malloc failure");
281 cur->next = ttylist;
282 cur->logout = currentout;
283 memmove(cur->tty, ttyname, sizeof(cur->tty));
284 return (ttylist = cur);
285 }
286
287 /*
288 * hostconv --
289 * convert the hostname to search pattern; if the supplied host name
290 * has a domain attached that is the same as the current domain, rip
291 * off the domain suffix since that's what login(1) does.
292 */
293 static void
294 hostconv(char *arg)
295 {
296 static int first = 1;
297 static char *hostdot, name[MAXHOSTNAMELEN + 1];
298 char *argdot;
299
300 if (!(argdot = strchr(arg, '.')))
301 return;
302 if (first) {
303 first = 0;
304 if (gethostname(name, sizeof(name)))
305 err(1, "gethostname");
306 name[sizeof(name) - 1] = '\0';
307 hostdot = strchr(name, '.');
308 }
309 if (hostdot && !strcasecmp(hostdot, argdot))
310 *argdot = '\0';
311 }
312
313 /*
314 * ttyconv --
315 * convert tty to correct name.
316 */
317 static char *
318 ttyconv(char *arg)
319 {
320 char *mval;
321
322 /*
323 * kludge -- we assume that all tty's end with
324 * a two character suffix.
325 */
326 if (strlen(arg) == 2) {
327 /* either 6 for "ttyxx" or 8 for "console" */
328 if (!(mval = malloc((u_int)8)))
329 err(1, "malloc failure");
330 if (!strcmp(arg, "co"))
331 (void)strcpy(mval, "console");
332 else {
333 (void)strcpy(mval, "tty");
334 (void)strcpy(mval + 3, arg);
335 }
336 return (mval);
337 }
338 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
339 return (arg + 5);
340 return (arg);
341 }
342
343 /*
344 * fmttime --
345 * return pointer to (static) formatted time string.
346 */
347 static char *
348 fmttime(time_t t, int flags)
349 {
350 struct tm *tm;
351 static char tbuf[TBUFLEN];
352
353 tm = (flags & GMT) ? gmtime(&t) : localtime(&t);
354 strftime(tbuf, sizeof(tbuf),
355 (flags & TIMEONLY)
356 ? (flags & FULLTIME ? LTFMTS : TFMTS)
357 : (flags & FULLTIME ? LTFMT : TFMT),
358 tm);
359 return (tbuf);
360 }
361
362 #ifdef SUPPORT_UTMP
363 #define TYPE(a) 0
364 #define NAMESIZE UT_NAMESIZE
365 #define LINESIZE UT_LINESIZE
366 #define HOSTSIZE UT_HOSTSIZE
367 #define ut_timefld ut_time
368 #define FIRSTVALID 0
369 #include "want.c"
370 #undef TYPE(a)
371 #undef NAMESIZE
372 #undef LINESIZE
373 #undef HOSTSIZE
374 #undef ut_timefld
375 #undef FIRSTVALID
376 #endif
377
378 #ifdef SUPPORT_UTMPX
379 #define utmp utmpx
380 #define want wantx
381 #define wtmp wtmpx
382 #define buf bufx
383 #define onintr onintrx
384 #define TYPE(a) (a)->ut_type
385 #define NAMESIZE UTX_USERSIZE
386 #define LINESIZE UTX_LINESIZE
387 #define HOSTSIZE UTX_HOSTSIZE
388 #define ut_timefld ut_xtime
389 #define FIRSTVALID 1
390 #include "want.c"
391 #endif
392