last.c revision 1.29 1 /* $NetBSD: last.c,v 1.29 2006/01/22 15:47:10 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. 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.29 2006/01/22 15:47:10 christos 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 if (optind < argc)
173 maxrec = atol(argv[optind] + 1);
174 else
175 usage();
176 if (!maxrec)
177 return 0;
178 }
179 break;
180 case 'f':
181 file = optarg;
182 break;
183 case 'H':
184 hostsize = atoi(optarg);
185 break;
186 case 'h':
187 hostconv(optarg);
188 addarg(HOST_TYPE, optarg);
189 break;
190 case 'L':
191 linesize = atoi(optarg);
192 break;
193 case 'N':
194 namesize = atoi(optarg);
195 break;
196 case 'n':
197 numeric = 1;
198 break;
199 case 'T':
200 fulltime = 1;
201 break;
202 case 't':
203 addarg(TTY_TYPE, ttyconv(optarg));
204 break;
205 case 'x':
206 xflag = 1;
207 break;
208 case '?':
209 default:
210 usage();
211 }
212
213 if (argc) {
214 setlinebuf(stdout);
215 for (argv += optind; *argv; ++argv) {
216 #define COMPATIBILITY
217 #ifdef COMPATIBILITY
218 /* code to allow "last p5" to work */
219 addarg(TTY_TYPE, ttyconv(*argv));
220 #endif
221 addarg(USER_TYPE, *argv);
222 }
223 }
224 if (file == NULL) {
225 #ifdef SUPPORT_UTMPX
226 if (access(_PATH_WTMPX, R_OK) == 0)
227 file = _PATH_WTMPX;
228 else
229 #endif
230 #ifdef SUPPORT_UTMP
231 if (access(_PATH_WTMP, R_OK) == 0)
232 file = _PATH_WTMP;
233 #endif
234 if (file == NULL)
235 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
236 errx(1, "Cannot access `%s' or `%s'", _PATH_WTMPX,
237 _PATH_WTMP);
238 #elif defined(SUPPORT_UTMPX)
239 errx(1, "Cannot access `%s'", _PATH_WTMPX);
240 #elif defined(SUPPORT_UTMP)
241 errx(1, "Cannot access `%s'", _PATH_WTMP);
242 #else
243 errx(1, "No utmp or utmpx support compiled in.");
244 #endif
245 }
246 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
247 if (file[strlen(file) - 1] == 'x' || xflag)
248 wtmpx(file, namesize, linesize, hostsize, numeric);
249 else
250 wtmp(file, namesize, linesize, hostsize, numeric);
251 #elif defined(SUPPORT_UTMPX)
252 wtmpx(file, namesize, linesize, hostsize, numeric);
253 #elif defined(SUPPORT_UTMP)
254 wtmp(file, namesize, linesize, hostsize, numeric);
255 #else
256 errx(1, "No utmp or utmpx support compiled in.");
257 #endif
258 exit(0);
259 }
260
261
262 /*
263 * addarg --
264 * add an entry to a linked list of arguments
265 */
266 static void
267 addarg(int type, char *arg)
268 {
269 ARG *cur;
270
271 if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
272 err(1, "malloc failure");
273 cur->next = arglist;
274 cur->type = type;
275 cur->name = arg;
276 arglist = cur;
277 }
278
279 /*
280 * addtty --
281 * add an entry to a linked list of ttys
282 */
283 static TTY *
284 addtty(const char *ttyname)
285 {
286 TTY *cur;
287
288 if (!(cur = (TTY *)malloc((u_int)sizeof(TTY))))
289 err(1, "malloc failure");
290 cur->next = ttylist;
291 cur->logout = currentout;
292 memmove(cur->tty, ttyname, sizeof(cur->tty));
293 return (ttylist = cur);
294 }
295
296 /*
297 * hostconv --
298 * convert the hostname to search pattern; if the supplied host name
299 * has a domain attached that is the same as the current domain, rip
300 * off the domain suffix since that's what login(1) does.
301 */
302 static void
303 hostconv(char *arg)
304 {
305 static int first = 1;
306 static char *hostdot, name[MAXHOSTNAMELEN + 1];
307 char *argdot;
308
309 if (!(argdot = strchr(arg, '.')))
310 return;
311 if (first) {
312 first = 0;
313 if (gethostname(name, sizeof(name)))
314 err(1, "gethostname");
315 name[sizeof(name) - 1] = '\0';
316 hostdot = strchr(name, '.');
317 }
318 if (hostdot && !strcasecmp(hostdot, argdot))
319 *argdot = '\0';
320 }
321
322 /*
323 * ttyconv --
324 * convert tty to correct name.
325 */
326 static char *
327 ttyconv(char *arg)
328 {
329 char *mval;
330
331 /*
332 * kludge -- we assume that all tty's end with
333 * a two character suffix.
334 */
335 if (strlen(arg) == 2) {
336 if (!strcmp(arg, "co"))
337 mval = strdup("console");
338 else
339 asprintf(&mval, "tty%s", arg);
340 if (!mval)
341 err(1, "malloc failure");
342 return (mval);
343 }
344 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
345 return (arg + 5);
346 return (arg);
347 }
348
349 /*
350 * fmttime --
351 * return pointer to (static) formatted time string.
352 */
353 static char *
354 fmttime(time_t t, int flags)
355 {
356 struct tm *tm;
357 static char tbuf[TBUFLEN];
358
359 tm = (flags & GMT) ? gmtime(&t) : localtime(&t);
360 strftime(tbuf, sizeof(tbuf),
361 (flags & TIMEONLY)
362 ? (flags & FULLTIME ? LTFMTS : TFMTS)
363 : (flags & FULLTIME ? LTFMT : TFMT),
364 tm);
365 return (tbuf);
366 }
367
368 #ifdef SUPPORT_UTMP
369 #define TYPE(a) 0
370 #define NAMESIZE UT_NAMESIZE
371 #define LINESIZE UT_LINESIZE
372 #define HOSTSIZE UT_HOSTSIZE
373 #define ut_timefld ut_time
374 #define FIRSTVALID 0
375 #include "want.c"
376 #undef TYPE /*(a)*/
377 #undef NAMESIZE
378 #undef LINESIZE
379 #undef HOSTSIZE
380 #undef ut_timefld
381 #undef FIRSTVALID
382 #endif
383
384 #ifdef SUPPORT_UTMPX
385 #define utmp utmpx
386 #define want wantx
387 #define wtmp wtmpx
388 #define gethost gethostx
389 #define buf bufx
390 #define onintr onintrx
391 #define TYPE(a) (a)->ut_type
392 #define NAMESIZE UTX_USERSIZE
393 #define LINESIZE UTX_LINESIZE
394 #define HOSTSIZE UTX_HOSTSIZE
395 #define ut_timefld ut_xtime
396 #define FIRSTVALID 1
397 #include "want.c"
398 #endif
399