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