Skip to content

Commit

Permalink
last: support matching for username and/or tty
Browse files Browse the repository at this point in the history
  • Loading branch information
thkukuk committed Dec 13, 2023
1 parent a09f270 commit 527f235
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
9 changes: 7 additions & 2 deletions man/wtmpdb.8.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,21 @@
<variablelist>
<varlistentry>
<term><command>last</command>
<optional><replaceable>option</replaceable>…</optional></term>
<optional><replaceable>option</replaceable>…</optional>
<optional><replaceable>username</replaceable>…</optional>
<optional><replaceable>tty</replaceable>…</optional></term>
<listitem>
<para>
<command>wtmpdb last</command> goes through the
<filename>/var/lib/wtmpdb/wtmp.db</filename> database (or the
database designated by the <command>-f</command> option) and
displays a list of of all users logged in and logged out. The
output can be restricted to different patterns via various
options.
options. If one or more usernames and/or ttys are given
<command>wtmpdb last</command> will only show the entries matching
those arguments.
</para>

<para>
The login and logout times of the special user
<command>reboot</command> are the boot and shutdown times of the
Expand Down
22 changes: 18 additions & 4 deletions src/wtmpdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ static unsigned long currentry = 0; /* number of entries already printed */
static time_t present = 0; /* Who was present at the specified time */
static time_t since = 0; /* Who was logged in after this time? */
static time_t until = 0; /* Who was logged in until this time? */
static char **match = NULL; /* user/tty to display only */


/* isipaddr - find out if string provided is an IP address or not
Expand Down Expand Up @@ -304,6 +305,20 @@ print_entry (void *unused __attribute__((__unused__)),
if (present && (present < (time_t)(login_t/USEC_PER_SEC)))
return 0;

if (match)
{
char **walk;

for (walk = match; *walk; walk++)
{
if (strcmp (user, *walk) == 0 ||
strcmp(tty, *walk) == 0)
break;
}
if (*walk == NULL)
return 0;
}

format_time (login_fmt, logintime, sizeof (logintime),
login_t/USEC_PER_SEC);

Expand Down Expand Up @@ -473,6 +488,8 @@ usage (int retval)
fputs (" -t, --until TIME Display who was logged in until TIME\n", output);
fputs (" -w, --fullnames Display full IP addresses and user and domain names\n", output);
fputs (" -x, --system Display system shutdown entries\n", output);
fputs (" [username...] Display only entries matching these arguments\n", output);
fputs (" [tty...] Display only entries matching these arguments\n", output);
fputs ("TIME must be in the format \"YYYY-MM-DD HH:MM:SS\"\n", output);
fputs ("\n", output);

Expand Down Expand Up @@ -649,10 +666,7 @@ main_last (int argc, char **argv)
}

if (argc > optind)
{
fprintf (stderr, "Unexpected argument: %s\n", argv[optind]);
usage (EXIT_FAILURE);
}
match = argv + optind;

if (nohostname && hostlast)
{
Expand Down

0 comments on commit 527f235

Please sign in to comment.