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