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