1 /* $NetBSD: last.c,v 1.36 2012/03/15 03:04:05 dholland Exp $ */
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)last.c 8.2 (Berkeley) 4/2/94";
42 __RCSID("$NetBSD: last.c,v 1.36 2012/03/15 03:04:05 dholland Exp $");
45 #include <sys/param.h>
58 #include <arpa/inet.h>
70 #define UT_HOSTSIZE 16
78 #define NO 0 /* false/no */
79 #define YES 1 /* true/yes */
81 #define TBUFLEN 30 /* length of time string buffer */
82 #define TFMT "%a %b %d %R" /* strftime format string */
83 #define LTFMT "%a %b %d %Y %T" /* strftime long format string */
84 #define TFMTS "%R" /* strftime format string - time only */
85 #define LTFMTS "%T" /* strftime long format string - " */
88 #define FULLTIME 0x1 /* show year, seconds */
89 #define TIMEONLY 0x2 /* show time only, not date */
90 #define GMT 0x4 /* show time at GMT, for offsets only */
95 const char *name
; /* argument */
99 int type
; /* type of arg */
100 struct arg
*next
; /* linked list pointer */
102 static ARG
*arglist
; /* head of linked list */
104 typedef struct ttytab
{
105 time_t logout
; /* log out time */
106 char tty
[128]; /* terminal name */
107 struct ttytab
*next
; /* linked list pointer */
109 static TTY
*ttylist
; /* head of linked list */
111 static time_t currentout
; /* current logout value */
112 static long maxrec
; /* records to display */
113 static int fulltime
= 0; /* Display seconds? */
114 static int xflag
; /* Assume file is wtmpx format */
116 static void addarg(int, const char *);
117 static TTY
*addtty(const char *);
118 static void hostconv(char *);
119 static const char *ttyconv(char *);
121 static void wtmpx(const char *, int, int, int, int);
124 static void wtmp(const char *, int, int, int, int);
126 static char *fmttime(time_t, int);
127 __dead
static void usage(void);
132 (void)fprintf(stderr
, "usage: %s [-#%s] [-nTx] [-f file]"
133 " [-H hostsize] [-h host] [-L linesize]\n"
134 "\t [-N namesize] [-t tty] [user ...]\n", getprogname(),
135 #ifdef NOTYET_SUPPORT_UTMPX
145 main(int argc
, char *argv
[])
149 const char *file
= NULL
;
150 int namesize
= UT_NAMESIZE
;
151 int linesize
= UT_LINESIZE
;
152 int hostsize
= UT_HOSTSIZE
;
157 while ((ch
= getopt(argc
, argv
, "0123456789f:H:h:L:nN:Tt:x")) != -1)
159 case '0': case '1': case '2': case '3': case '4':
160 case '5': case '6': case '7': case '8': case '9':
162 * kludge: last was originally designed to take
163 * a number after a dash.
166 p
= argv
[optind
- 1];
167 if (p
[0] == '-' && p
[1] == ch
&& !p
[2])
169 else if (optind
< argc
)
170 maxrec
= atol(argv
[optind
] + 1);
183 hostsize
= atoi(optarg
);
189 addarg(HOST_TYPE
, optarg
);
192 linesize
= atoi(optarg
);
197 namesize
= atoi(optarg
);
208 addarg(TTY_TYPE
, ttyconv(optarg
));
220 for (argv
+= optind
; *argv
; ++argv
) {
221 #define COMPATIBILITY
223 /* code to allow "last p5" to work */
224 addarg(TTY_TYPE
, ttyconv(*argv
));
226 addarg(USER_TYPE
, *argv
);
231 if (access(_PATH_WTMPX
, R_OK
) == 0)
236 if (access(_PATH_WTMP
, R_OK
) == 0)
240 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
241 errx(EXIT_FAILURE
, "Cannot access `%s' or `%s'", _PATH_WTMPX
,
243 #elif defined(SUPPORT_UTMPX)
244 errx(EXIT_FAILURE
, "Cannot access `%s'", _PATH_WTMPX
);
245 #elif defined(SUPPORT_UTMP)
246 errx(EXIT_FAILURE
, "Cannot access `%s'", _PATH_WTMP
);
248 errx(EXIT_FAILURE
, "No utmp or utmpx support compiled in.");
251 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
252 if (file
[strlen(file
) - 1] == 'x' || xflag
)
253 wtmpx(file
, namesize
, linesize
, hostsize
, numeric
);
255 wtmp(file
, namesize
, linesize
, hostsize
, numeric
);
256 #elif defined(SUPPORT_UTMPX)
257 wtmpx(file
, namesize
, linesize
, hostsize
, numeric
);
258 #elif defined(SUPPORT_UTMP)
259 wtmp(file
, namesize
, linesize
, hostsize
, numeric
);
261 errx(EXIT_FAILURE
, "No utmp or utmpx support compiled in.");
269 * add an entry to a linked list of arguments
272 addarg(int type
, const char *arg
)
276 if (!(cur
= (ARG
*)malloc(sizeof(ARG
))))
277 err(EXIT_FAILURE
, "malloc failure");
286 * add an entry to a linked list of ttys
289 addtty(const char *tty
)
293 if (!(cur
= (TTY
*)malloc(sizeof(TTY
))))
294 err(EXIT_FAILURE
, "malloc failure");
296 cur
->logout
= currentout
;
297 memmove(cur
->tty
, tty
, sizeof(cur
->tty
));
298 return (ttylist
= cur
);
303 * convert the hostname to search pattern; if the supplied host name
304 * has a domain attached that is the same as the current domain, rip
305 * off the domain suffix since that's what login(1) does.
310 static int first
= 1;
311 static char *hostdot
, name
[MAXHOSTNAMELEN
+ 1];
314 if (!(argdot
= strchr(arg
, '.')))
318 if (gethostname(name
, sizeof(name
)))
319 err(EXIT_FAILURE
, "gethostname");
320 name
[sizeof(name
) - 1] = '\0';
321 hostdot
= strchr(name
, '.');
323 if (hostdot
&& !strcasecmp(hostdot
, argdot
))
329 * convert tty to correct name.
336 if (!strcmp(arg
, "co"))
339 * kludge -- we assume that all tty's end with
340 * a two character suffix.
342 if (strlen(arg
) == 2) {
343 if (asprintf(&mval
, "tty%s", arg
) == -1)
344 err(EXIT_FAILURE
, "malloc failure");
347 if (!strncmp(arg
, _PATH_DEV
, sizeof(_PATH_DEV
) - 1))
348 return (&arg
[sizeof(_PATH_DEV
) - 1]);
354 * return pointer to (static) formatted time string.
357 fmttime(time_t t
, int flags
)
360 static char tbuf
[TBUFLEN
];
362 tm
= (flags
& GMT
) ? gmtime(&t
) : localtime(&t
);
364 strcpy(tbuf
, "????");
367 strftime(tbuf
, sizeof(tbuf
),
369 ? (flags
& FULLTIME
? LTFMTS
: TFMTS
)
370 : (flags
& FULLTIME
? LTFMT
: TFMT
),
377 #define NAMESIZE UT_NAMESIZE
378 #define LINESIZE UT_LINESIZE
379 #define HOSTSIZE UT_HOSTSIZE
380 #define ut_timefld ut_time
395 #define gethost gethostx
397 #define onintr onintrx
398 #define TYPE(a) (a)->ut_type
399 #define NAMESIZE UTX_USERSIZE
400 #define LINESIZE UTX_LINESIZE
401 #define HOSTSIZE UTX_HOSTSIZE
402 #define ut_timefld ut_xtime