1 /* $NetBSD: last.c,v 1.32 2008/07/21 14:19:23 lukem 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.32 2008/07/21 14:19:23 lukem 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 int main(int, char *[]);
118 static void addarg(int, const char *);
119 static TTY
*addtty(const char *);
120 static void hostconv(char *);
121 static const char *ttyconv(char *);
123 static void wtmpx(const char *, int, int, int, int);
126 static void wtmp(const char *, int, int, int, int);
128 static char *fmttime(time_t, int);
129 static void usage(void);
134 (void)fprintf(stderr
, "usage: %s [-#%s] [-nTx] [-f file]"
135 " [-H hostsize] [-h host] [-L linesize]\n"
136 "\t [-N namesize] [-t tty] [user ...]\n", getprogname(),
137 #ifdef NOTYET_SUPPORT_UTMPX
147 main(int argc
, char *argv
[])
151 const char *file
= NULL
;
152 int namesize
= UT_NAMESIZE
;
153 int linesize
= UT_LINESIZE
;
154 int hostsize
= UT_HOSTSIZE
;
159 while ((ch
= getopt(argc
, argv
, "0123456789f:H:h:L:nN:Tt:x")) != -1)
161 case '0': case '1': case '2': case '3': case '4':
162 case '5': case '6': case '7': case '8': case '9':
164 * kludge: last was originally designed to take
165 * a number after a dash.
168 p
= argv
[optind
- 1];
169 if (p
[0] == '-' && p
[1] == ch
&& !p
[2])
171 else if (optind
< argc
)
172 maxrec
= atol(argv
[optind
] + 1);
185 hostsize
= atoi(optarg
);
191 addarg(HOST_TYPE
, optarg
);
194 linesize
= atoi(optarg
);
199 namesize
= atoi(optarg
);
210 addarg(TTY_TYPE
, ttyconv(optarg
));
222 for (argv
+= optind
; *argv
; ++argv
) {
223 #define COMPATIBILITY
225 /* code to allow "last p5" to work */
226 addarg(TTY_TYPE
, ttyconv(*argv
));
228 addarg(USER_TYPE
, *argv
);
233 if (access(_PATH_WTMPX
, R_OK
) == 0)
238 if (access(_PATH_WTMP
, R_OK
) == 0)
242 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
243 errx(EXIT_FAILURE
, "Cannot access `%s' or `%s'", _PATH_WTMPX
,
245 #elif defined(SUPPORT_UTMPX)
246 errx(EXIT_FAILURE
, "Cannot access `%s'", _PATH_WTMPX
);
247 #elif defined(SUPPORT_UTMP)
248 errx(EXIT_FAILURE
, "Cannot access `%s'", _PATH_WTMP
);
250 errx(EXIT_FAILURE
, "No utmp or utmpx support compiled in.");
253 #if defined(SUPPORT_UTMPX) && defined(SUPPORT_UTMP)
254 if (file
[strlen(file
) - 1] == 'x' || xflag
)
255 wtmpx(file
, namesize
, linesize
, hostsize
, numeric
);
257 wtmp(file
, namesize
, linesize
, hostsize
, numeric
);
258 #elif defined(SUPPORT_UTMPX)
259 wtmpx(file
, namesize
, linesize
, hostsize
, numeric
);
260 #elif defined(SUPPORT_UTMP)
261 wtmp(file
, namesize
, linesize
, hostsize
, numeric
);
263 errx(EXIT_FAILURE
, "No utmp or utmpx support compiled in.");
271 * add an entry to a linked list of arguments
274 addarg(int type
, const char *arg
)
278 if (!(cur
= (ARG
*)malloc(sizeof(ARG
))))
279 err(EXIT_FAILURE
, "malloc failure");
288 * add an entry to a linked list of ttys
291 addtty(const char *tty
)
295 if (!(cur
= (TTY
*)malloc(sizeof(TTY
))))
296 err(EXIT_FAILURE
, "malloc failure");
298 cur
->logout
= currentout
;
299 memmove(cur
->tty
, tty
, sizeof(cur
->tty
));
300 return (ttylist
= cur
);
305 * convert the hostname to search pattern; if the supplied host name
306 * has a domain attached that is the same as the current domain, rip
307 * off the domain suffix since that's what login(1) does.
312 static int first
= 1;
313 static char *hostdot
, name
[MAXHOSTNAMELEN
+ 1];
316 if (!(argdot
= strchr(arg
, '.')))
320 if (gethostname(name
, sizeof(name
)))
321 err(EXIT_FAILURE
, "gethostname");
322 name
[sizeof(name
) - 1] = '\0';
323 hostdot
= strchr(name
, '.');
325 if (hostdot
&& !strcasecmp(hostdot
, argdot
))
331 * convert tty to correct name.
338 if (!strcmp(arg
, "co"))
341 * kludge -- we assume that all tty's end with
342 * a two character suffix.
344 if (strlen(arg
) == 2) {
345 if (asprintf(&mval
, "tty%s", arg
) == -1)
346 err(EXIT_FAILURE
, "malloc failure");
349 if (!strncmp(arg
, _PATH_DEV
, sizeof(_PATH_DEV
) - 1))
350 return (&arg
[sizeof(_PATH_DEV
) - 1]);
356 * return pointer to (static) formatted time string.
359 fmttime(time_t t
, int flags
)
362 static char tbuf
[TBUFLEN
];
364 tm
= (flags
& GMT
) ? gmtime(&t
) : localtime(&t
);
365 strftime(tbuf
, sizeof(tbuf
),
367 ? (flags
& FULLTIME
? LTFMTS
: TFMTS
)
368 : (flags
& FULLTIME
? LTFMT
: TFMT
),
375 #define NAMESIZE UT_NAMESIZE
376 #define LINESIZE UT_LINESIZE
377 #define HOSTSIZE UT_HOSTSIZE
378 #define ut_timefld ut_time
393 #define gethost gethostx
395 #define onintr onintrx
396 #define TYPE(a) (a)->ut_type
397 #define NAMESIZE UTX_USERSIZE
398 #define LINESIZE UTX_LINESIZE
399 #define HOSTSIZE UTX_HOSTSIZE
400 #define ut_timefld ut_xtime