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