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