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>
47 #include "pathnames.h"
49 #include "closestream.h"
50 #include "carefulputc.h"
52 #include "timeutils.h"
53 #include "monotonic.h"
55 #if defined(_HAVE_UT_TV)
56 # define UL_UT_TIME ut_tv.tv_sec
58 # define UL_UT_TIME ut_time
62 # define SHUTDOWN_TIME 254
65 #ifndef LAST_LOGIN_LEN
66 # define LAST_LOGIN_LEN 8
69 #ifndef LAST_DOMAIN_LEN
70 # define LAST_DOMAIN_LEN 16
73 #ifndef LAST_TIMESTAMP_LEN
74 # define LAST_TIMESTAMP_LEN 32
77 #define UCHUNKSIZE 16384 /* How much we read at once. */
80 unsigned int lastb
:1, /* Is this command 'lastb' */
81 extended
:1, /* Lots of info */
82 showhost
:1, /* Show hostname */
83 altlist
:1, /* Hostname at the end */
84 usedns
:1, /* Use DNS to lookup the hostname */
85 useip
:1; /* Print IP address in number format */
87 unsigned int name_len
; /* Number of login name characters to print */
88 unsigned int domain_len
; /* Number of domain name characters to print */
89 unsigned int maxrecs
; /* Maximum number of records to list */
91 char **show
; /* Match search list */
93 struct timeval boot_time
; /* system boot time */
94 time_t since
; /* at what time to start displaying the file */
95 time_t until
; /* at what time to stop displaying the file */
96 time_t present
; /* who where present at time_t */
97 unsigned int time_fmt
; /* time format */
100 /* Double linked list of struct utmp's */
103 struct utmplist
*next
;
104 struct utmplist
*prev
;
107 /* Types of listing */
109 R_CRASH
= 1, /* No logout record, system boot in between */
110 R_DOWN
, /* System brought down in decent way */
111 R_NORMAL
, /* Normal */
112 R_NOW
, /* Still logged in */
113 R_REBOOT
, /* Reboot record. */
114 R_PHANTOM
, /* No logout record but session is stale. */
115 R_TIMECHANGE
/* NEW_TIME or OLD_TIME */
119 LAST_TIMEFTM_NONE
= 0,
122 LAST_TIMEFTM_ISO8601
,
124 LAST_TIMEFTM_HHMM
, /* non-public */
127 struct last_timefmt
{
129 int in_len
; /* log-in */
131 int out_len
; /* log-out */
135 static struct last_timefmt timefmts
[] = {
136 [LAST_TIMEFTM_NONE
] = { .name
= "notime" },
137 [LAST_TIMEFTM_SHORT
] = {
141 .in_fmt
= LAST_TIMEFTM_CTIME
,
142 .out_fmt
= LAST_TIMEFTM_HHMM
144 [LAST_TIMEFTM_CTIME
] = {
148 .in_fmt
= LAST_TIMEFTM_CTIME
,
149 .out_fmt
= LAST_TIMEFTM_CTIME
151 [LAST_TIMEFTM_ISO8601
] = {
155 .in_fmt
= LAST_TIMEFTM_ISO8601
,
156 .out_fmt
= LAST_TIMEFTM_ISO8601
160 /* Global variables */
161 static unsigned int recsdone
; /* Number of records listed */
162 static time_t lastdate
; /* Last date we've seen */
163 static time_t currentdate
; /* date when we started processing the file */
165 /* --time-format=option parser */
166 static int which_time_format(const char *optarg
)
170 for (i
= 0; i
< ARRAY_SIZE(timefmts
); i
++) {
171 if (strcmp(timefmts
[i
].name
, optarg
) == 0)
174 errx(EXIT_FAILURE
, _("unknown time format: %s"), optarg
);
178 * Read one utmp entry, return in new format.
179 * Automatically reposition file pointer.
181 static int uread(FILE *fp
, struct utmp
*u
, int *quit
, const char *filename
)
184 static char buf
[UCHUNKSIZE
];
190 if (quit
== NULL
&& u
!= NULL
) {
194 return fread(u
, sizeof(struct utmp
), 1, fp
);
199 * Initialize and position.
201 utsize
= sizeof(struct utmp
);
202 fseeko(fp
, 0, SEEK_END
);
206 o
= ((fpos
- 1) / UCHUNKSIZE
) * UCHUNKSIZE
;
207 if (fseeko(fp
, o
, SEEK_SET
) < 0) {
208 warn(_("seek on %s failed"), filename
);
211 bpos
= (int)(fpos
- o
);
212 if (fread(buf
, bpos
, 1, fp
) != 1) {
213 warn(_("cannot read %s"), filename
);
221 * Read one struct. From the buffer if possible.
225 memcpy(u
, buf
+ bpos
, sizeof(struct utmp
));
230 * Oops we went "below" the buffer. We should be able to
231 * seek back UCHUNKSIZE bytes.
238 * Copy whatever is left in the buffer.
240 memcpy(tmp
+ (-bpos
), buf
, utsize
+ bpos
);
241 if (fseeko(fp
, fpos
, SEEK_SET
) < 0) {
242 warn(_("seek on %s failed"), filename
);
247 * Read another UCHUNKSIZE bytes.
249 if (fread(buf
, UCHUNKSIZE
, 1, fp
) != 1) {
250 warn(_("cannot read %s"), filename
);
255 * The end of the UCHUNKSIZE byte buffer should be the first
256 * few bytes of the current struct utmp.
258 memcpy(tmp
, buf
+ UCHUNKSIZE
+ bpos
, -bpos
);
261 memcpy(u
, tmp
, sizeof(struct utmp
));
267 * Print a short date.
269 static char *showdate(void)
271 char *s
= ctime(&lastdate
);
279 static void int_handler(int sig
__attribute__((unused
)))
281 errx(EXIT_FAILURE
, _("Interrupted %s"), showdate());
287 static void quit_handler(int sig
__attribute__((unused
)))
289 warnx(_("Interrupted %s"), showdate());
290 signal(SIGQUIT
, quit_handler
);
294 * Lookup a host with DNS.
296 static int dns_lookup(char *result
, int size
, int useip
, int32_t *a
)
298 struct sockaddr_in sin
;
299 struct sockaddr_in6 sin6
;
304 flags
= useip
? NI_NUMERICHOST
: 0;
308 * 1. If last 3 4bytes are 0, must be IPv4
309 * 2. If IPv6 in IPv4, handle as IPv4
310 * 3. Anything else is IPv6
314 if (a
[0] == 0 && a
[1] == 0 && a
[2] == (int32_t)htonl (0xffff))
317 if (mapped
|| (a
[1] == 0 && a
[2] == 0 && a
[3] == 0)) {
319 sin
.sin_family
= AF_INET
;
321 sin
.sin_addr
.s_addr
= mapped
? a
[3] : a
[0];
322 sa
= (struct sockaddr
*)&sin
;
326 memset(&sin6
, 0, sizeof(sin6
));
327 sin6
.sin6_family
= AF_INET6
;
329 memcpy(sin6
.sin6_addr
.s6_addr
, a
, 16);
330 sa
= (struct sockaddr
*)&sin6
;
331 salen
= sizeof(sin6
);
334 return getnameinfo(sa
, salen
, result
, size
, NULL
, 0, flags
);
337 static int time_formatter(int fmt
, char *dst
, size_t dlen
, time_t *when
)
342 case LAST_TIMEFTM_NONE
:
345 case LAST_TIMEFTM_HHMM
:
347 struct tm
*tm
= localtime(when
);
348 if (!snprintf(dst
, dlen
, "%02d:%02d", tm
->tm_hour
, tm
->tm_min
))
352 case LAST_TIMEFTM_CTIME
:
353 ret
= snprintf(dst
, dlen
, "%s", ctime(when
));
355 case LAST_TIMEFTM_ISO8601
:
356 ret
= strtime_iso(when
, ISO_8601_DATE
|ISO_8601_TIME
|ISO_8601_TIMEZONE
, dst
, dlen
);
365 * Remove trailing spaces from a string.
367 static void trim_trailing_spaces(char *s
)
373 while (p
> s
&& isspace(*--p
))
382 * Show one line of information on screen
384 static int list(const struct last_control
*ctl
, struct utmp
*p
, time_t logout_time
, int what
)
386 time_t secs
, utmp_time
;
387 char logintime
[LAST_TIMESTAMP_LEN
];
388 char logouttime
[LAST_TIMESTAMP_LEN
];
389 char length
[LAST_TIMESTAMP_LEN
];
391 char utline
[UT_LINESIZE
+1];
394 int mins
, hours
, days
;
396 struct last_timefmt
*fmt
;
399 * uucp and ftp have special-type entries
402 strncat(utline
, p
->ut_line
, UT_LINESIZE
);
403 if (strncmp(utline
, "ftp", 3) == 0 && isdigit(utline
[3]))
405 if (strncmp(utline
, "uucp", 4) == 0 && isdigit(utline
[4]))
409 * Is this something we want to show?
413 for (walk
= ctl
->show
; *walk
; walk
++) {
414 if (strncmp(p
->ut_user
, *walk
, UT_NAMESIZE
) == 0 ||
415 strcmp(utline
, *walk
) == 0 ||
416 (strncmp(utline
, "tty", 3) == 0 &&
417 strcmp(utline
+ 3, *walk
) == 0)) break;
419 if (*walk
== NULL
) return 0;
425 fmt
= &timefmts
[ctl
->time_fmt
];
427 utmp_time
= p
->UL_UT_TIME
;
430 if (ctl
->present
< utmp_time
)
432 if (0 < logout_time
&& logout_time
< ctl
->present
)
437 if (time_formatter(fmt
->in_fmt
, logintime
,
438 sizeof(logintime
), &utmp_time
) < 0)
439 errx(EXIT_FAILURE
, _("preallocation size exceeded"));
442 secs
= logout_time
- utmp_time
;
443 mins
= (secs
/ 60) % 60;
444 hours
= (secs
/ 3600) % 24;
447 strcpy(logouttime
, "- ");
448 if (time_formatter(fmt
->out_fmt
, logouttime
+ 2,
449 sizeof(logouttime
) - 2, &logout_time
) < 0)
450 errx(EXIT_FAILURE
, _("preallocation size exceeded"));
452 if (logout_time
== currentdate
) {
453 if (ctl
->time_fmt
> LAST_TIMEFTM_SHORT
) {
454 sprintf(logouttime
, " still running");
457 sprintf(logouttime
, " still");
458 sprintf(length
, "running");
461 sprintf(length
, "(%d+%02d:%02d)", days
, hours
, mins
);
463 sprintf(length
, " (%02d:%02d)", hours
, mins
);
468 sprintf(logouttime
, "- crash");
471 sprintf(logouttime
, "- down ");
474 if (ctl
->time_fmt
> LAST_TIMEFTM_SHORT
) {
475 sprintf(logouttime
, " still logged in");
478 sprintf(logouttime
, " still");
479 sprintf(length
, "logged in");
483 if (ctl
->time_fmt
> LAST_TIMEFTM_SHORT
) {
484 sprintf(logouttime
, " gone - no logout");
486 } else if (ctl
->time_fmt
== LAST_TIMEFTM_SHORT
) {
487 sprintf(logouttime
, " gone");
488 sprintf(length
, "- no logout");
491 sprintf(length
, "no logout");
506 * Look up host with DNS if needed.
509 if (ctl
->usedns
|| ctl
->useip
)
510 r
= dns_lookup(domain
, sizeof(domain
), ctl
->useip
, p
->ut_addr_v6
);
513 if (len
>= (int)sizeof(domain
)) len
= sizeof(domain
) - 1;
515 strncat(domain
, p
->ut_host
, len
);
521 len
= snprintf(final
, sizeof(final
),
522 "%-8.*s %-12.12s %-16.*s %-*.*s %-*.*s %s\n",
523 ctl
->name_len
, p
->ut_user
, utline
,
524 ctl
->domain_len
, domain
,
525 fmt
->in_len
, fmt
->in_len
, logintime
, fmt
->out_len
, fmt
->out_len
,
528 len
= snprintf(final
, sizeof(final
),
529 "%-8.*s %-12.12s %-*.*s %-*.*s %-12.12s %s\n",
530 ctl
->name_len
, p
->ut_user
, utline
,
531 fmt
->in_len
, fmt
->in_len
, logintime
, fmt
->out_len
, fmt
->out_len
,
532 logouttime
, length
, domain
);
535 len
= snprintf(final
, sizeof(final
),
536 "%-8.*s %-12.12s %-*.*s %-*.*s %s\n",
537 ctl
->name_len
, p
->ut_user
, utline
,
538 fmt
->in_len
, fmt
->in_len
, logintime
, fmt
->out_len
, fmt
->out_len
,
541 #if defined(__GLIBC__)
542 # if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0)
543 final
[sizeof(final
)-1] = '\0';
547 trim_trailing_spaces(final
);
549 * Print out "final" string safely.
551 for (s
= final
; *s
; s
++)
552 fputc_careful(*s
, stdout
, '*');
554 if (len
< 0 || (size_t)len
>= sizeof(final
))
558 if (ctl
->maxrecs
&& ctl
->maxrecs
<= recsdone
)
565 static void __attribute__((__noreturn__
)) usage(const struct last_control
*ctl
, FILE *out
)
567 fputs(USAGE_HEADER
, out
);
569 " %s [options] [<username>...] [<tty>...]\n"), program_invocation_short_name
);
571 fputs(USAGE_SEPARATOR
, out
);
572 fputs(_("Show a listing of last logged in users.\n"), out
);
574 fputs(USAGE_OPTIONS
, out
);
575 fputs(_(" -<number> how many lines to show\n"), out
);
576 fputs(_(" -a, --hostlast display hostnames in the last column\n"), out
);
577 fputs(_(" -d, --dns translate the IP number back into a hostname\n"), out
);
579 _(" -f, --file <file> use a specific file instead of %s\n"), ctl
->lastb
? _PATH_BTMP
: _PATH_WTMP
);
580 fputs(_(" -F, --fulltimes print full login and logout times and dates\n"), out
);
581 fputs(_(" -i, --ip display IP numbers in numbers-and-dots notation\n"), out
);
582 fputs(_(" -n, --limit <number> how many lines to show\n"), out
);
583 fputs(_(" -R, --nohostname don't display the hostname field\n"), out
);
584 fputs(_(" -s, --since <time> display the lines since the specified time\n"), out
);
585 fputs(_(" -t, --until <time> display the lines until the specified time\n"), out
);
586 fputs(_(" -p, --present <time> display who were present at the specified time\n"), out
);
587 fputs(_(" -w, --fullnames display full user and domain names\n"), out
);
588 fputs(_(" -x, --system display system shutdown entries and run level changes\n"), out
);
589 fputs(_(" --time-format <format> show timestamps in the specified <format>:\n"
590 " notime|short|full|iso\n"), out
);
592 fputs(USAGE_SEPARATOR
, out
);
593 fputs(USAGE_HELP
, out
);
594 fputs(USAGE_VERSION
, out
);
595 fprintf(out
, USAGE_MAN_TAIL("last(1)"));
597 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
600 static int is_phantom(const struct last_control
*ctl
, struct utmp
*ut
)
606 if (ut
->UL_UT_TIME
< ctl
->boot_time
.tv_sec
)
608 pw
= getpwnam(ut
->ut_user
);
611 sprintf(path
, "/proc/%u/loginuid", ut
->ut_pid
);
612 if (access(path
, R_OK
) == 0) {
613 unsigned int loginuid
;
616 if (!(f
= fopen(path
, "r")))
618 if (fscanf(f
, "%u", &loginuid
) != 1)
621 if (!ret
&& pw
->pw_uid
!= loginuid
)
626 sprintf(path
, "/dev/%s", ut
->ut_line
);
629 if (pw
->pw_uid
!= st
.st_uid
)
635 static void process_wtmp_file(const struct last_control
*ctl
,
636 const char *filename
)
638 FILE *fp
; /* Filepointer of wtmp file */
640 struct utmp ut
; /* Current utmp entry */
641 struct utmplist
*ulist
= NULL
; /* All entries */
642 struct utmplist
*p
; /* Pointer into utmplist */
643 struct utmplist
*next
; /* Pointer into utmplist */
645 time_t lastboot
= 0; /* Last boottime */
646 time_t lastrch
= 0; /* Last run level change */
647 time_t lastdown
; /* Last downtime */
648 time_t begintime
; /* When wtmp begins */
649 int whydown
= 0; /* Why we went down: crash or shutdown */
651 int c
, x
; /* Scratch */
652 struct stat st
; /* To stat the [uw]tmp file */
653 int quit
= 0; /* Flag */
654 int down
= 0; /* Down flag */
660 lastdate
= currentdate
= lastrch
= lastdown
;
663 * Install signal handlers
665 signal(SIGINT
, int_handler
);
666 signal(SIGQUIT
, quit_handler
);
671 if ((fp
= fopen(filename
, "r")) == NULL
)
672 err(EXIT_FAILURE
, _("cannot open %s"), filename
);
675 * Optimize the buffer size.
677 setvbuf(fp
, NULL
, _IOFBF
, UCHUNKSIZE
);
680 * Read first structure to capture the time field
682 if (uread(fp
, &ut
, NULL
, filename
) == 1)
683 begintime
= ut
.UL_UT_TIME
;
685 if (fstat(fileno(fp
), &st
) != 0)
686 err(EXIT_FAILURE
, _("stat of %s failed"), filename
);
687 begintime
= st
.st_ctime
;
692 * Go to end of file minus one structure
693 * and/or initialize utmp reading code.
695 uread(fp
, NULL
, NULL
, filename
);
698 * Read struct after struct backwards from the file.
702 if (uread(fp
, &ut
, &quit
, filename
) != 1)
705 if (ctl
->since
&& ut
.UL_UT_TIME
< ctl
->since
)
708 if (ctl
->until
&& ctl
->until
< ut
.UL_UT_TIME
)
711 lastdate
= ut
.UL_UT_TIME
;
714 quit
= list(ctl
, &ut
, ut
.UL_UT_TIME
, R_NORMAL
);
719 * Set ut_type to the correct type.
721 if (strncmp(ut
.ut_line
, "~", 1) == 0) {
722 if (strncmp(ut
.ut_user
, "shutdown", 8) == 0)
723 ut
.ut_type
= SHUTDOWN_TIME
;
724 else if (strncmp(ut
.ut_user
, "reboot", 6) == 0)
725 ut
.ut_type
= BOOT_TIME
;
726 else if (strncmp(ut
.ut_user
, "runlevel", 8) == 0)
727 ut
.ut_type
= RUN_LVL
;
731 * For stupid old applications that don't fill in
735 if (ut
.ut_type
!= DEAD_PROCESS
&&
736 ut
.ut_user
[0] && ut
.ut_line
[0] &&
737 strcmp(ut
.ut_user
, "LOGIN") != 0)
738 ut
.ut_type
= USER_PROCESS
;
740 * Even worse, applications that write ghost
741 * entries: ut_type set to USER_PROCESS but
744 if (ut
.ut_user
[0] == 0)
745 ut
.ut_type
= DEAD_PROCESS
;
750 if (strcmp(ut
.ut_user
, "date") == 0) {
751 if (ut
.ut_line
[0] == '|')
752 ut
.ut_type
= OLD_TIME
;
753 if (ut
.ut_line
[0] == '{')
754 ut
.ut_type
= NEW_TIME
;
758 switch (ut
.ut_type
) {
761 strcpy(ut
.ut_line
, "system down");
762 quit
= list(ctl
, &ut
, lastboot
, R_NORMAL
);
764 lastdown
= lastrch
= ut
.UL_UT_TIME
;
771 ut
.ut_type
== NEW_TIME
? "new time" :
773 quit
= list(ctl
, &ut
, lastdown
, R_TIMECHANGE
);
777 strcpy(ut
.ut_line
, "system boot");
778 quit
= list(ctl
, &ut
, lastdown
, R_REBOOT
);
779 lastboot
= ut
.UL_UT_TIME
;
785 sprintf(ut
.ut_line
, "(to lvl %c)", x
);
786 quit
= list(ctl
, &ut
, lastrch
, R_NORMAL
);
788 if (x
== '0' || x
== '6') {
789 lastdown
= ut
.UL_UT_TIME
;
791 ut
.ut_type
= SHUTDOWN_TIME
;
793 lastrch
= ut
.UL_UT_TIME
;
798 * This was a login - show the first matching
799 * logout record and delete all records with
803 for (p
= ulist
; p
; p
= next
) {
805 if (strncmp(p
->ut
.ut_line
, ut
.ut_line
,
809 quit
= list(ctl
, &ut
, p
->ut
.UL_UT_TIME
, R_NORMAL
);
813 p
->next
->prev
= p
->prev
;
815 p
->prev
->next
= p
->next
;
822 * Not found? Then crashed, down, still
823 * logged in, or missing logout record.
828 /* Is process still alive? */
829 if (is_phantom(ctl
, &ut
))
833 quit
= list(ctl
, &ut
, lastboot
, c
);
839 * Just store the data if it is
840 * interesting enough.
842 if (ut
.ut_line
[0] == 0)
844 p
= xmalloc(sizeof(struct utmplist
));
845 memcpy(&p
->ut
, &ut
, sizeof(struct utmp
));
857 /* ignored ut_types */
861 warnx("unrecogized ut_type: %d", ut
.ut_type
);
865 * If we saw a shutdown/reboot record we can remove
866 * the entire current ulist.
869 lastboot
= ut
.UL_UT_TIME
;
870 whydown
= (ut
.ut_type
== SHUTDOWN_TIME
) ? R_DOWN
: R_CRASH
;
871 for (p
= ulist
; p
; p
= next
) {
880 printf(_("\n%s begins %s"), basename(filename
), ctime(&begintime
));
883 for (p
= ulist
; p
; p
= next
) {
889 int main(int argc
, char **argv
)
891 struct last_control ctl
= {
893 .name_len
= LAST_LOGIN_LEN
,
894 .time_fmt
= LAST_TIMEFTM_SHORT
,
895 .domain_len
= LAST_DOMAIN_LEN
898 size_t i
, nfiles
= 0;
903 OPT_TIME_FORMAT
= CHAR_MAX
+ 1
905 static const struct option long_opts
[] = {
906 { "limit", required_argument
, NULL
, 'n' },
907 { "help", no_argument
, NULL
, 'h' },
908 { "file", required_argument
, NULL
, 'f' },
909 { "nohostname", no_argument
, NULL
, 'R' },
910 { "version", no_argument
, NULL
, 'V' },
911 { "hostlast", no_argument
, NULL
, 'a' },
912 { "since", required_argument
, NULL
, 's' },
913 { "until", required_argument
, NULL
, 't' },
914 { "present", required_argument
, NULL
, 'p' },
915 { "system", no_argument
, NULL
, 'x' },
916 { "dns", no_argument
, NULL
, 'd' },
917 { "ip", no_argument
, NULL
, 'i' },
918 { "fulltimes", no_argument
, NULL
, 'F' },
919 { "fullnames", no_argument
, NULL
, 'w' },
920 { "time-format", required_argument
, NULL
, OPT_TIME_FORMAT
},
923 static const ul_excl_t excl
[] = { /* rows and cols in in ASCII order */
924 { 'F', OPT_TIME_FORMAT
}, /* fulltime, time-format */
927 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
929 setlocale(LC_ALL
, "");
930 bindtextdomain(PACKAGE
, LOCALEDIR
);
932 atexit(close_stdout
);
934 * Which file do we want to read?
936 ctl
.lastb
= strcmp(program_invocation_short_name
, "lastb") == 0 ? 1 : 0;
937 while ((c
= getopt_long(argc
, argv
,
938 "hVf:n:RxadFit:p:s:0123456789w", long_opts
, NULL
)) != -1) {
940 err_exclusive_options(c
, long_opts
, excl
, excl_st
);
947 printf(UTIL_LINUX_VERSION
);
956 ctl
.maxrecs
= strtos32_or_err(optarg
, _("failed to parse number"));
960 files
= xmalloc(sizeof(char *) * argc
);
961 files
[nfiles
++] = xstrdup(optarg
);
973 ctl
.time_fmt
= LAST_TIMEFTM_CTIME
;
976 if (parse_timestamp(optarg
, &p
) < 0)
977 errx(EXIT_FAILURE
, _("invalid time value \"%s\""), optarg
);
978 ctl
.present
= (time_t) (p
/ 1000000);
981 if (parse_timestamp(optarg
, &p
) < 0)
982 errx(EXIT_FAILURE
, _("invalid time value \"%s\""), optarg
);
983 ctl
.since
= (time_t) (p
/ 1000000);
986 if (parse_timestamp(optarg
, &p
) < 0)
987 errx(EXIT_FAILURE
, _("invalid time value \"%s\""), optarg
);
988 ctl
.until
= (time_t) (p
/ 1000000);
991 if (ctl
.name_len
< UT_NAMESIZE
)
992 ctl
.name_len
= UT_NAMESIZE
;
993 if (ctl
.domain_len
< UT_HOSTSIZE
)
994 ctl
.domain_len
= UT_HOSTSIZE
;
996 case '0': case '1': case '2': case '3': case '4':
997 case '5': case '6': case '7': case '8': case '9':
998 ctl
.maxrecs
= 10 * ctl
.maxrecs
+ c
- '0';
1000 case OPT_TIME_FORMAT
:
1001 ctl
.time_fmt
= which_time_format(optarg
);
1004 usage(&ctl
, stderr
);
1010 ctl
.show
= argv
+ optind
;
1013 files
= xmalloc(sizeof(char *));
1014 files
[nfiles
++] = xstrdup(ctl
.lastb
? _PATH_BTMP
: _PATH_WTMP
);
1017 for (i
= 0; i
< nfiles
; i
++) {
1018 get_boot_time(&ctl
.boot_time
);
1019 process_wtmp_file(&ctl
, files
[i
]);
1023 return EXIT_SUCCESS
;