2 * last(1) from sysvinit project, merged into util-linux in Aug 2013.
4 * Copyright (C) 1991-2004 Miquel van Smoorenburg.
5 * Copyright (C) 2013 Ondrej Oprala <ooprala@redhat.com>
6 * Karel Zak <kzak@redhat.com>
8 * Re-implementation of the 'last' command, this time for Linux. Yes I know
9 * there is BSD last, but I just felt like writing this. No thanks :-). Also,
10 * this version gives lots more info (especially with -x)
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <sys/types.h>
40 #include <netinet/in.h>
42 #include <arpa/inet.h>
48 #include "pathnames.h"
50 #include "closestream.h"
51 #include "carefulputc.h"
53 #include "timeutils.h"
54 #include "monotonic.h"
57 # define SHUTDOWN_TIME 254
60 #ifndef LAST_LOGIN_LEN
61 # define LAST_LOGIN_LEN 8
64 #ifndef LAST_DOMAIN_LEN
65 # define LAST_DOMAIN_LEN 16
68 #ifndef LAST_TIMESTAMP_LEN
69 # define LAST_TIMESTAMP_LEN 32
72 #define UCHUNKSIZE 16384 /* How much we read at once. */
75 unsigned int lastb
:1, /* Is this command 'lastb' */
76 extended
:1, /* Lots of info */
77 showhost
:1, /* Show hostname */
78 altlist
:1, /* Hostname at the end */
79 usedns
:1, /* Use DNS to lookup the hostname */
80 useip
:1; /* Print IP address in number format */
82 unsigned int name_len
; /* Number of login name characters to print */
83 unsigned int domain_len
; /* Number of domain name characters to print */
84 unsigned int maxrecs
; /* Maximum number of records to list */
86 char **show
; /* Match search list */
88 struct timeval boot_time
; /* system boot time */
89 time_t since
; /* at what time to start displaying the file */
90 time_t until
; /* at what time to stop displaying the file */
91 time_t present
; /* who where present at time_t */
92 unsigned int time_fmt
; /* time format */
95 /* Double linked list of struct utmp's */
98 struct utmplist
*next
;
99 struct utmplist
*prev
;
102 /* Types of listing */
104 R_CRASH
= 1, /* No logout record, system boot in between */
105 R_DOWN
, /* System brought down in decent way */
106 R_NORMAL
, /* Normal */
107 R_NOW
, /* Still logged in */
108 R_REBOOT
, /* Reboot record. */
109 R_PHANTOM
, /* No logout record but session is stale. */
110 R_TIMECHANGE
/* NEW_TIME or OLD_TIME */
114 LAST_TIMEFTM_NONE
= 0,
117 LAST_TIMEFTM_ISO8601
,
119 LAST_TIMEFTM_HHMM
, /* non-public */
122 struct last_timefmt
{
124 int in_len
; /* log-in */
126 int out_len
; /* log-out */
130 static struct last_timefmt timefmts
[] = {
131 [LAST_TIMEFTM_NONE
] = { .name
= "notime" },
132 [LAST_TIMEFTM_SHORT
] = {
136 .in_fmt
= LAST_TIMEFTM_CTIME
,
137 .out_fmt
= LAST_TIMEFTM_HHMM
139 [LAST_TIMEFTM_CTIME
] = {
143 .in_fmt
= LAST_TIMEFTM_CTIME
,
144 .out_fmt
= LAST_TIMEFTM_CTIME
146 [LAST_TIMEFTM_ISO8601
] = {
150 .in_fmt
= LAST_TIMEFTM_ISO8601
,
151 .out_fmt
= LAST_TIMEFTM_ISO8601
155 /* Global variables */
156 static unsigned int recsdone
; /* Number of records listed */
157 static time_t lastdate
; /* Last date we've seen */
158 static time_t currentdate
; /* date when we started processing the file */
160 /* --time-format=option parser */
161 static int which_time_format(const char *s
)
165 for (i
= 0; i
< ARRAY_SIZE(timefmts
); i
++) {
166 if (strcmp(timefmts
[i
].name
, s
) == 0)
169 errx(EXIT_FAILURE
, _("unknown time format: %s"), s
);
173 * Read one utmp entry, return in new format.
174 * Automatically reposition file pointer.
176 static int uread(FILE *fp
, struct utmpx
*u
, int *quit
, const char *filename
)
179 static char buf
[UCHUNKSIZE
];
185 if (quit
== NULL
&& u
!= NULL
) {
189 return fread(u
, sizeof(struct utmpx
), 1, fp
);
194 * Initialize and position.
196 utsize
= sizeof(struct utmpx
);
197 fseeko(fp
, 0, SEEK_END
);
201 o
= ((fpos
- 1) / UCHUNKSIZE
) * UCHUNKSIZE
;
202 if (fseeko(fp
, o
, SEEK_SET
) < 0) {
203 warn(_("seek on %s failed"), filename
);
206 bpos
= (int)(fpos
- o
);
207 if (fread(buf
, bpos
, 1, fp
) != 1) {
208 warn(_("cannot read %s"), filename
);
216 * Read one struct. From the buffer if possible.
220 memcpy(u
, buf
+ bpos
, sizeof(struct utmpx
));
225 * Oops we went "below" the buffer. We should be able to
226 * seek back UCHUNKSIZE bytes.
233 * Copy whatever is left in the buffer.
235 memcpy(tmp
+ (-bpos
), buf
, utsize
+ bpos
);
236 if (fseeko(fp
, fpos
, SEEK_SET
) < 0) {
237 warn(_("seek on %s failed"), filename
);
242 * Read another UCHUNKSIZE bytes.
244 if (fread(buf
, UCHUNKSIZE
, 1, fp
) != 1) {
245 warn(_("cannot read %s"), filename
);
250 * The end of the UCHUNKSIZE byte buffer should be the first
251 * few bytes of the current struct utmp.
253 memcpy(tmp
, buf
+ UCHUNKSIZE
+ bpos
, -bpos
);
256 memcpy(u
, tmp
, sizeof(struct utmpx
));
262 * Print a short date.
264 static char *showdate(void)
266 char *s
= ctime(&lastdate
);
274 static void int_handler(int sig
__attribute__((unused
)))
276 errx(EXIT_FAILURE
, _("Interrupted %s"), showdate());
282 static void quit_handler(int sig
__attribute__((unused
)))
284 warnx(_("Interrupted %s"), showdate());
285 signal(SIGQUIT
, quit_handler
);
289 * Lookup a host with DNS.
291 static int dns_lookup(char *result
, int size
, int useip
, int32_t *a
)
293 struct sockaddr_in sin
;
294 struct sockaddr_in6 sin6
;
299 flags
= useip
? NI_NUMERICHOST
: 0;
303 * 1. If last 3 4bytes are 0, must be IPv4
304 * 2. If IPv6 in IPv4, handle as IPv4
305 * 3. Anything else is IPv6
309 if (a
[0] == 0 && a
[1] == 0 && a
[2] == (int32_t)htonl (0xffff))
312 if (mapped
|| (a
[1] == 0 && a
[2] == 0 && a
[3] == 0)) {
314 sin
.sin_family
= AF_INET
;
316 sin
.sin_addr
.s_addr
= mapped
? a
[3] : a
[0];
317 sa
= (struct sockaddr
*)&sin
;
321 memset(&sin6
, 0, sizeof(sin6
));
322 sin6
.sin6_family
= AF_INET6
;
324 memcpy(sin6
.sin6_addr
.s6_addr
, a
, 16);
325 sa
= (struct sockaddr
*)&sin6
;
326 salen
= sizeof(sin6
);
329 return getnameinfo(sa
, salen
, result
, size
, NULL
, 0, flags
);
332 static int time_formatter(int fmt
, char *dst
, size_t dlen
, time_t *when
)
337 case LAST_TIMEFTM_NONE
:
340 case LAST_TIMEFTM_HHMM
:
342 struct tm
*tm
= localtime(when
);
343 if (!snprintf(dst
, dlen
, "%02d:%02d", tm
->tm_hour
, tm
->tm_min
))
347 case LAST_TIMEFTM_CTIME
:
348 snprintf(dst
, dlen
, "%s", ctime(when
));
349 ret
= rtrim_whitespace((unsigned char *) dst
);
351 case LAST_TIMEFTM_ISO8601
:
352 ret
= strtime_iso(when
, ISO_TIMESTAMP_T
, dst
, dlen
);
361 * Remove trailing spaces from a string.
363 static void trim_trailing_spaces(char *s
)
369 while (p
> s
&& isspace(*--p
))
378 * Show one line of information on screen
380 static int list(const struct last_control
*ctl
, struct utmpx
*p
, time_t logout_time
, int what
)
382 time_t secs
, utmp_time
;
383 char logintime
[LAST_TIMESTAMP_LEN
];
384 char logouttime
[LAST_TIMESTAMP_LEN
];
385 char length
[LAST_TIMESTAMP_LEN
];
387 char utline
[sizeof(p
->ut_line
) + 1];
390 int mins
, hours
, days
;
392 struct last_timefmt
*fmt
;
395 * uucp and ftp have special-type entries
397 mem2strcpy(utline
, p
->ut_line
, sizeof(p
->ut_line
), sizeof(utline
));
398 if (strncmp(utline
, "ftp", 3) == 0 && isdigit(utline
[3]))
400 if (strncmp(utline
, "uucp", 4) == 0 && isdigit(utline
[4]))
404 * Is this something we want to show?
408 for (walk
= ctl
->show
; *walk
; walk
++) {
409 if (strncmp(p
->ut_user
, *walk
, sizeof(p
->ut_user
)) == 0 ||
410 strcmp(utline
, *walk
) == 0 ||
411 (strncmp(utline
, "tty", 3) == 0 &&
412 strcmp(utline
+ 3, *walk
) == 0)) break;
414 if (*walk
== NULL
) return 0;
420 fmt
= &timefmts
[ctl
->time_fmt
];
422 utmp_time
= p
->ut_tv
.tv_sec
;
425 if (ctl
->present
< utmp_time
)
427 if (0 < logout_time
&& logout_time
< ctl
->present
)
432 if (time_formatter(fmt
->in_fmt
, logintime
,
433 sizeof(logintime
), &utmp_time
) < 0)
434 errx(EXIT_FAILURE
, _("preallocation size exceeded"));
437 secs
= logout_time
- utmp_time
; /* Under strange circumstances, secs < 0 can happen */
438 mins
= (secs
/ 60) % 60;
439 hours
= (secs
/ 3600) % 24;
442 strcpy(logouttime
, "- ");
443 if (time_formatter(fmt
->out_fmt
, logouttime
+ 2,
444 sizeof(logouttime
) - 2, &logout_time
) < 0)
445 errx(EXIT_FAILURE
, _("preallocation size exceeded"));
447 if (logout_time
== currentdate
) {
448 if (ctl
->time_fmt
> LAST_TIMEFTM_SHORT
) {
449 sprintf(logouttime
, " still running");
452 sprintf(logouttime
, " still");
453 sprintf(length
, "running");
456 sprintf(length
, "(%d+%02d:%02d)", days
, abs(hours
), abs(mins
)); /* hours and mins always shown as positive (w/o minus sign!) even if secs < 0 */
458 sprintf(length
, " (%02d:%02d)", hours
, abs(mins
)); /* mins always shown as positive (w/o minus sign!) even if secs < 0 */
459 } else if (secs
>= 0) {
460 sprintf(length
, " (%02d:%02d)", hours
, mins
);
462 sprintf(length
, " (-00:%02d)", abs(mins
)); /* mins always shown as positive (w/o minus sign!) even if secs < 0 */
467 sprintf(logouttime
, "- crash");
470 sprintf(logouttime
, "- down ");
473 if (ctl
->time_fmt
> LAST_TIMEFTM_SHORT
) {
474 sprintf(logouttime
, " still logged in");
477 sprintf(logouttime
, " still");
478 sprintf(length
, "logged in");
482 if (ctl
->time_fmt
> LAST_TIMEFTM_SHORT
) {
483 sprintf(logouttime
, " gone - no logout");
485 } else if (ctl
->time_fmt
== LAST_TIMEFTM_SHORT
) {
486 sprintf(logouttime
, " gone");
487 sprintf(length
, "- no logout");
490 sprintf(length
, "no logout");
505 * Look up host with DNS if needed.
508 if (ctl
->usedns
|| ctl
->useip
)
509 r
= dns_lookup(domain
, sizeof(domain
), ctl
->useip
, (int32_t*)p
->ut_addr_v6
);
511 mem2strcpy(domain
, p
->ut_host
, sizeof(p
->ut_host
), sizeof(domain
));
515 len
= snprintf(final
, sizeof(final
),
516 "%-8.*s %-12.12s %-16.*s %-*.*s %-*.*s %s\n",
517 ctl
->name_len
, p
->ut_user
, utline
,
518 ctl
->domain_len
, domain
,
519 fmt
->in_len
, fmt
->in_len
, logintime
, fmt
->out_len
, fmt
->out_len
,
522 len
= snprintf(final
, sizeof(final
),
523 "%-8.*s %-12.12s %-*.*s %-*.*s %-12.12s %s\n",
524 ctl
->name_len
, p
->ut_user
, utline
,
525 fmt
->in_len
, fmt
->in_len
, logintime
, fmt
->out_len
, fmt
->out_len
,
526 logouttime
, length
, domain
);
529 len
= snprintf(final
, sizeof(final
),
530 "%-8.*s %-12.12s %-*.*s %-*.*s %s\n",
531 ctl
->name_len
, p
->ut_user
, utline
,
532 fmt
->in_len
, fmt
->in_len
, logintime
, fmt
->out_len
, fmt
->out_len
,
535 #if defined(__GLIBC__)
536 # if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0)
537 final
[sizeof(final
)-1] = '\0';
541 trim_trailing_spaces(final
);
543 * Print out "final" string safely.
545 for (s
= final
; *s
; s
++)
546 fputc_careful(*s
, stdout
, '*');
548 if (len
< 0 || (size_t)len
>= sizeof(final
))
552 if (ctl
->maxrecs
&& ctl
->maxrecs
<= recsdone
)
559 static void __attribute__((__noreturn__
)) usage(const struct last_control
*ctl
)
562 fputs(USAGE_HEADER
, out
);
564 " %s [options] [<username>...] [<tty>...]\n"), program_invocation_short_name
);
566 fputs(USAGE_SEPARATOR
, out
);
567 fputs(_("Show a listing of last logged in users.\n"), out
);
569 fputs(USAGE_OPTIONS
, out
);
570 fputs(_(" -<number> how many lines to show\n"), out
);
571 fputs(_(" -a, --hostlast display hostnames in the last column\n"), out
);
572 fputs(_(" -d, --dns translate the IP number back into a hostname\n"), out
);
574 _(" -f, --file <file> use a specific file instead of %s\n"), ctl
->lastb
? _PATH_BTMP
: _PATH_WTMP
);
575 fputs(_(" -F, --fulltimes print full login and logout times and dates\n"), out
);
576 fputs(_(" -i, --ip display IP numbers in numbers-and-dots notation\n"), out
);
577 fputs(_(" -n, --limit <number> how many lines to show\n"), out
);
578 fputs(_(" -R, --nohostname don't display the hostname field\n"), out
);
579 fputs(_(" -s, --since <time> display the lines since the specified time\n"), out
);
580 fputs(_(" -t, --until <time> display the lines until the specified time\n"), out
);
581 fputs(_(" -p, --present <time> display who were present at the specified time\n"), out
);
582 fputs(_(" -w, --fullnames display full user and domain names\n"), out
);
583 fputs(_(" -x, --system display system shutdown entries and run level changes\n"), out
);
584 fputs(_(" --time-format <format> show timestamps in the specified <format>:\n"
585 " notime|short|full|iso\n"), out
);
587 fputs(USAGE_SEPARATOR
, out
);
588 printf(USAGE_HELP_OPTIONS(22));
589 printf(USAGE_MAN_TAIL("last(1)"));
591 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
594 static int is_phantom(const struct last_control
*ctl
, struct utmpx
*ut
)
597 char path
[sizeof(ut
->ut_line
) + 16];
600 if (ut
->ut_tv
.tv_sec
< ctl
->boot_time
.tv_sec
)
602 ut
->ut_user
[sizeof(ut
->ut_user
) - 1] = '\0';
603 pw
= getpwnam(ut
->ut_user
);
606 sprintf(path
, "/proc/%u/loginuid", ut
->ut_pid
);
607 if (access(path
, R_OK
) == 0) {
608 unsigned int loginuid
;
611 if (!(f
= fopen(path
, "r")))
613 if (fscanf(f
, "%u", &loginuid
) != 1)
616 if (!ret
&& pw
->pw_uid
!= loginuid
)
621 sprintf(path
, "/dev/%s", ut
->ut_line
);
624 if (pw
->pw_uid
!= st
.st_uid
)
630 static void process_wtmp_file(const struct last_control
*ctl
,
631 const char *filename
)
633 FILE *fp
; /* File pointer of wtmp file */
635 struct utmpx ut
; /* Current utmp entry */
636 struct utmplist
*ulist
= NULL
; /* All entries */
637 struct utmplist
*p
; /* Pointer into utmplist */
638 struct utmplist
*next
; /* Pointer into utmplist */
640 time_t lastboot
= 0; /* Last boottime */
641 time_t lastrch
= 0; /* Last run level change */
642 time_t lastdown
; /* Last downtime */
643 time_t begintime
; /* When wtmp begins */
644 int whydown
= 0; /* Why we went down: crash or shutdown */
646 int c
, x
; /* Scratch */
647 struct stat st
; /* To stat the [uw]tmp file */
648 int quit
= 0; /* Flag */
649 int down
= 0; /* Down flag */
655 lastdate
= currentdate
= lastrch
= lastdown
;
658 * Install signal handlers
660 signal(SIGINT
, int_handler
);
661 signal(SIGQUIT
, quit_handler
);
666 if ((fp
= fopen(filename
, "r")) == NULL
)
667 err(EXIT_FAILURE
, _("cannot open %s"), filename
);
670 * Optimize the buffer size.
672 setvbuf(fp
, NULL
, _IOFBF
, UCHUNKSIZE
);
675 * Read first structure to capture the time field
677 if (uread(fp
, &ut
, NULL
, filename
) == 1)
678 begintime
= ut
.ut_tv
.tv_sec
;
680 if (fstat(fileno(fp
), &st
) != 0)
681 err(EXIT_FAILURE
, _("stat of %s failed"), filename
);
682 begintime
= st
.st_ctime
;
687 * Go to end of file minus one structure
688 * and/or initialize utmp reading code.
690 uread(fp
, NULL
, NULL
, filename
);
693 * Read struct after struct backwards from the file.
697 if (uread(fp
, &ut
, &quit
, filename
) != 1)
700 if (ctl
->since
&& ut
.ut_tv
.tv_sec
< ctl
->since
)
703 if (ctl
->until
&& ctl
->until
< ut
.ut_tv
.tv_sec
)
706 lastdate
= ut
.ut_tv
.tv_sec
;
709 quit
= list(ctl
, &ut
, ut
.ut_tv
.tv_sec
, R_NORMAL
);
714 * Set ut_type to the correct type.
716 if (strncmp(ut
.ut_line
, "~", 1) == 0) {
717 if (strncmp(ut
.ut_user
, "shutdown", 8) == 0)
718 ut
.ut_type
= SHUTDOWN_TIME
;
719 else if (strncmp(ut
.ut_user
, "reboot", 6) == 0)
720 ut
.ut_type
= BOOT_TIME
;
721 else if (strncmp(ut
.ut_user
, "runlevel", 8) == 0)
722 ut
.ut_type
= RUN_LVL
;
726 * For stupid old applications that don't fill in
730 if (ut
.ut_type
!= DEAD_PROCESS
&&
731 ut
.ut_user
[0] && ut
.ut_line
[0] &&
732 strcmp(ut
.ut_user
, "LOGIN") != 0)
733 ut
.ut_type
= USER_PROCESS
;
735 * Even worse, applications that write ghost
736 * entries: ut_type set to USER_PROCESS but
739 if (ut
.ut_user
[0] == 0)
740 ut
.ut_type
= DEAD_PROCESS
;
745 if (strcmp(ut
.ut_user
, "date") == 0) {
746 if (ut
.ut_line
[0] == '|')
747 ut
.ut_type
= OLD_TIME
;
748 if (ut
.ut_line
[0] == '{')
749 ut
.ut_type
= NEW_TIME
;
753 switch (ut
.ut_type
) {
756 strcpy(ut
.ut_line
, "system down");
757 quit
= list(ctl
, &ut
, lastboot
, R_NORMAL
);
759 lastdown
= lastrch
= ut
.ut_tv
.tv_sec
;
766 ut
.ut_type
== NEW_TIME
? "new time" :
768 quit
= list(ctl
, &ut
, lastdown
, R_TIMECHANGE
);
772 strcpy(ut
.ut_line
, "system boot");
773 quit
= list(ctl
, &ut
, lastdown
, R_REBOOT
);
774 lastboot
= ut
.ut_tv
.tv_sec
;
780 sprintf(ut
.ut_line
, "(to lvl %c)", x
);
781 quit
= list(ctl
, &ut
, lastrch
, R_NORMAL
);
783 if (x
== '0' || x
== '6') {
784 lastdown
= ut
.ut_tv
.tv_sec
;
786 ut
.ut_type
= SHUTDOWN_TIME
;
788 lastrch
= ut
.ut_tv
.tv_sec
;
793 * This was a login - show the first matching
794 * logout record and delete all records with
798 for (p
= ulist
; p
; p
= next
) {
800 if (strncmp(p
->ut
.ut_line
, ut
.ut_line
,
801 sizeof(ut
.ut_line
)) == 0) {
804 quit
= list(ctl
, &ut
, p
->ut
.ut_tv
.tv_sec
, R_NORMAL
);
808 p
->next
->prev
= p
->prev
;
810 p
->prev
->next
= p
->next
;
817 * Not found? Then crashed, down, still
818 * logged in, or missing logout record.
823 /* Is process still alive? */
824 if (is_phantom(ctl
, &ut
))
828 quit
= list(ctl
, &ut
, lastboot
, c
);
834 * Just store the data if it is
835 * interesting enough.
837 if (ut
.ut_line
[0] == 0)
839 p
= xmalloc(sizeof(struct utmplist
));
840 memcpy(&p
->ut
, &ut
, sizeof(struct utmpx
));
854 /* ignored ut_types */
858 warnx("unrecognized ut_type: %d", ut
.ut_type
);
862 * If we saw a shutdown/reboot record we can remove
863 * the entire current ulist.
866 lastboot
= ut
.ut_tv
.tv_sec
;
867 whydown
= (ut
.ut_type
== SHUTDOWN_TIME
) ? R_DOWN
: R_CRASH
;
868 for (p
= ulist
; p
; p
= next
) {
877 if (ctl
->time_fmt
!= LAST_TIMEFTM_NONE
) {
878 struct last_timefmt
*fmt
;
879 char timestr
[LAST_TIMESTAMP_LEN
];
880 char *tmp
= xstrdup(filename
);
882 fmt
= &timefmts
[ctl
->time_fmt
];
883 if (time_formatter(fmt
->in_fmt
, timestr
,
884 sizeof(timestr
), &begintime
) < 0)
885 errx(EXIT_FAILURE
, _("preallocation size exceeded"));
886 printf(_("\n%s begins %s\n"), basename(tmp
), timestr
);
892 for (p
= ulist
; p
; p
= next
) {
898 int main(int argc
, char **argv
)
900 struct last_control ctl
= {
902 .name_len
= LAST_LOGIN_LEN
,
903 .time_fmt
= LAST_TIMEFTM_SHORT
,
904 .domain_len
= LAST_DOMAIN_LEN
907 size_t i
, nfiles
= 0;
912 OPT_TIME_FORMAT
= CHAR_MAX
+ 1
914 static const struct option long_opts
[] = {
915 { "limit", required_argument
, NULL
, 'n' },
916 { "help", no_argument
, NULL
, 'h' },
917 { "file", required_argument
, NULL
, 'f' },
918 { "nohostname", no_argument
, NULL
, 'R' },
919 { "version", no_argument
, NULL
, 'V' },
920 { "hostlast", no_argument
, NULL
, 'a' },
921 { "since", required_argument
, NULL
, 's' },
922 { "until", required_argument
, NULL
, 't' },
923 { "present", required_argument
, NULL
, 'p' },
924 { "system", no_argument
, NULL
, 'x' },
925 { "dns", no_argument
, NULL
, 'd' },
926 { "ip", no_argument
, NULL
, 'i' },
927 { "fulltimes", no_argument
, NULL
, 'F' },
928 { "fullnames", no_argument
, NULL
, 'w' },
929 { "time-format", required_argument
, NULL
, OPT_TIME_FORMAT
},
932 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
933 { 'F', OPT_TIME_FORMAT
}, /* fulltime, time-format */
936 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
938 setlocale(LC_ALL
, "");
939 bindtextdomain(PACKAGE
, LOCALEDIR
);
941 close_stdout_atexit();
943 * Which file do we want to read?
945 ctl
.lastb
= strcmp(program_invocation_short_name
, "lastb") == 0 ? 1 : 0;
946 while ((c
= getopt_long(argc
, argv
,
947 "hVf:n:RxadFit:p:s:0123456789w", long_opts
, NULL
)) != -1) {
949 err_exclusive_options(c
, long_opts
, excl
, excl_st
);
956 print_version(EXIT_SUCCESS
);
964 ctl
.maxrecs
= strtos32_or_err(optarg
, _("failed to parse number"));
968 files
= xmalloc(sizeof(char *) * argc
);
969 files
[nfiles
++] = xstrdup(optarg
);
981 ctl
.time_fmt
= LAST_TIMEFTM_CTIME
;
984 if (parse_timestamp(optarg
, &p
) < 0)
985 errx(EXIT_FAILURE
, _("invalid time value \"%s\""), optarg
);
986 ctl
.present
= (time_t) (p
/ 1000000);
989 if (parse_timestamp(optarg
, &p
) < 0)
990 errx(EXIT_FAILURE
, _("invalid time value \"%s\""), optarg
);
991 ctl
.since
= (time_t) (p
/ 1000000);
994 if (parse_timestamp(optarg
, &p
) < 0)
995 errx(EXIT_FAILURE
, _("invalid time value \"%s\""), optarg
);
996 ctl
.until
= (time_t) (p
/ 1000000);
999 if (ctl
.name_len
< sizeof(((struct utmpx
*) 0)->ut_user
))
1000 ctl
.name_len
= sizeof(((struct utmpx
*) 0)->ut_user
);
1001 if (ctl
.domain_len
< sizeof(((struct utmpx
*) 0)->ut_host
))
1002 ctl
.domain_len
= sizeof(((struct utmpx
*) 0)->ut_host
);
1004 case '0': case '1': case '2': case '3': case '4':
1005 case '5': case '6': case '7': case '8': case '9':
1006 ctl
.maxrecs
= 10 * ctl
.maxrecs
+ c
- '0';
1008 case OPT_TIME_FORMAT
:
1009 ctl
.time_fmt
= which_time_format(optarg
);
1012 errtryhelp(EXIT_FAILURE
);
1017 ctl
.show
= argv
+ optind
;
1020 files
= xmalloc(sizeof(char *));
1021 files
[nfiles
++] = xstrdup(ctl
.lastb
? _PATH_BTMP
: _PATH_WTMP
);
1024 for (i
= 0; i
< nfiles
; i
++) {
1025 get_boot_time(&ctl
.boot_time
);
1026 process_wtmp_file(&ctl
, files
[i
]);
1030 return EXIT_SUCCESS
;