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