1 /* @(#)last.c 8.2 (Berkeley) 4/2/94 */
2 /* $NetBSD: last.c,v 1.15 2000/06/30 06:19:58 simonb Exp $ */
5 * Copyright (c) 1987, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/param.h>
60 #define UT_HOSTSIZE 16
68 #define NO 0 /* false/no */
69 #define YES 1 /* true/yes */
71 #define TBUFLEN 30 /* length of time string buffer */
72 #define TFMT "%a %b %d %R" /* strftime format string */
73 #define LTFMT "%a %b %d %Y %T" /* strftime long format string */
74 #define TFMTS "%R" /* strftime format string - time only */
75 #define LTFMTS "%T" /* strftime long format string - " */
78 #define FULLTIME 0x1 /* show year, seconds */
79 #define TIMEONLY 0x2 /* show time only, not date */
80 #define GMT 0x4 /* show time at GMT, for offsets only */
85 char *name; /* argument */
89 int type; /* type of arg */
90 struct arg *next; /* linked list pointer */
92 static ARG *arglist; /* head of linked list */
94 typedef struct ttytab {
95 time_t logout; /* log out time */
96 char tty[128]; /* terminal name */
97 struct ttytab *next; /* linked list pointer */
99 static TTY *ttylist; /* head of linked list */
101 static time_t currentout; /* current logout value */
102 static long maxrec; /* records to display */
103 static int fulltime = 0; /* Display seconds? */
105 int main(int, char *[]);
107 static void addarg(int, char *);
108 static TTY *addtty(const char *);
109 static void hostconv(char *);
110 static char *ttyconv(char *);
112 static void wtmpx(const char *, int, int, int);
115 static void wtmp(const char *, int, int, int);
117 static char *fmttime(time_t, int);
118 static void usage(void);
123 (void)fprintf(stderr, "Usage: %s [-#%s] [-f file] [-t tty]"
124 " [-h hostname] [-T] [user ...]\n", getprogname(),
135 main(int argc, char *argv[])
139 const char *file = NULL;
140 int namesize = UT_NAMESIZE;
141 int linesize = UT_LINESIZE;
142 int hostsize = UT_HOSTSIZE;
146 while ((ch = getopt(argc, argv, "0123456789f:h:H:L:N:t:T")) != -1)
148 case '0': case '1': case '2': case '3': case '4':
149 case '5': case '6': case '7': case '8': case '9':
151 * kludge: last was originally designed to take
152 * a number after a dash.
155 p = argv[optind - 1];
156 if (p[0] == '-' && p[1] == ch && !p[2])
159 maxrec = atol(argv[optind] + 1);
169 addarg(HOST_TYPE, optarg);
172 addarg(TTY_TYPE, ttyconv(optarg));
175 namesize = atoi(optarg);
178 linesize = atoi(optarg);
181 hostsize = atoi(optarg);
193 for (argv += optind; *argv; ++argv) {
194 #define COMPATIBILITY
196 /* code to allow "last p5" to work */
197 addarg(TTY_TYPE, ttyconv(*argv));
199 addarg(USER_TYPE, *argv);
203 /* XXX: for now, default to utmp */
205 if (access(_PATH_WTMP, R_OK) == 0)
210 if (access(_PATH_WTMPX, R_OK) == 0)
214 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
215 errx(1, "Cannot access `%s' or `%s'", _PATH_WTMPX,
217 #elif defined(SUPPORT_UTMPX)
218 errx(1, "Cannot access `%s'", _PATH_WTMPX);
219 #elif defined(SUPPORT_UTMP)
220 errx(1, "Cannot access `%s'", _PATH_WTMP);
222 errx(1, "No utmp or utmpx support compiled in.");
225 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
226 if (file[strlen(file) - 1] == 'x')
227 wtmpx(file, namesize, linesize, hostsize);
229 wtmp(file, namesize, linesize, hostsize);
230 #elif defined(SUPPORT_UTMPX)
231 wtmpx(file, namesize, linesize, hostsize);
232 #elif defined(SUPPORT_UTMP)
233 wtmp(file, namesize, linesize, hostsize);
235 errx(1, "Nu utmp or utmpx support compiled in.");
243 * add an entry to a linked list of arguments
246 addarg(int type, char *arg)
250 if (!(cur = (ARG *)malloc((u_int)sizeof(ARG))))
251 err(1, "malloc failure");
260 * add an entry to a linked list of ttys
263 addtty(const char *tty)
267 if (!(cur = (TTY *)malloc((u_int)sizeof(TTY))))
268 err(1, "malloc failure");
270 cur->logout = currentout;
271 memmove(cur->tty, tty, sizeof(cur->tty));
272 return (ttylist = cur);
277 * convert the hostname to search pattern; if the supplied host name
278 * has a domain attached that is the same as the current domain, rip
279 * off the domain suffix since that's what login(1) does.
284 static int first = 1;
285 static char *hostdot, name[MAXHOSTNAMELEN + 1];
288 if (!(argdot = strchr(arg, '.')))
292 if (gethostname(name, sizeof(name)))
293 err(1, "gethostname");
294 name[sizeof(name) - 1] = '\0';
295 hostdot = strchr(name, '.');
297 if (hostdot && !strcasecmp(hostdot, argdot))
303 * convert tty to correct name.
311 * kludge -- we assume that all tty's end with
312 * a two character suffix.
314 if (strlen(arg) == 2) {
315 /* either 6 for "ttyxx" or 8 for "console" */
316 if (!(mval = malloc((u_int)8)))
317 err(1, "malloc failure");
318 if (!strcmp(arg, "co"))
319 (void)strcpy(mval, "console");
321 (void)strcpy(mval, "tty");
322 (void)strcpy(mval + 3, arg);
326 if (!strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1))
333 * return pointer to (static) formatted time string.
336 fmttime(time_t t, int flags)
339 static char tbuf[TBUFLEN];
341 tm = (flags & GMT) ? gmtime(&t) : localtime(&t);
342 strftime(tbuf, sizeof(tbuf),
344 ? (flags & FULLTIME ? LTFMTS : TFMTS)
345 : (flags & FULLTIME ? LTFMT : TFMT),
352 #define NAMESIZE UT_NAMESIZE
353 #define LINESIZE UT_LINESIZE
354 #define HOSTSIZE UT_HOSTSIZE
355 #define ut_timefld ut_time
371 #define onintr onintrx
372 #define TYPE(a) (a)->ut_type
373 #define NAMESIZE UTX_USERSIZE
374 #define LINESIZE UTX_LINESIZE
375 #define HOSTSIZE UTX_HOSTSIZE
376 #define ut_timefld ut_xtime