Skip to content

Commit

Permalink
misc-utils/lastlog2: Add option -a for listing active users only
Browse files Browse the repository at this point in the history
  • Loading branch information
echoechoin committed Feb 27, 2025
1 parent 467b0c1 commit 00bd8b9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions misc-utils/lastlog2.8.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ information and not a sparse file.

== OPTIONS

*-a*, *--active*::
Print last login records excluding users who have never logged in.

*-b*, *--before* _DAYS_::
Print only last login records older than _DAYS_.

Expand Down
13 changes: 11 additions & 2 deletions misc-utils/lastlog2.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

static char *lastlog2_path = LL2_DEFAULT_DATABASE;

static int aflg;
static int bflg;
static time_t b_days;
static int tflg;
Expand Down Expand Up @@ -78,8 +79,11 @@ static int print_entry(const char *user, int64_t ll_time,
datep = datetime;
}

if (ll_time == 0)
if (ll_time == 0) {
if (aflg)
return 0;
datep = "**Never logged in**";
}

if (!once) {
printf("Username Port From%*s Latest%*s%s\n",
Expand Down Expand Up @@ -108,6 +112,7 @@ static void __attribute__((__noreturn__)) usage(void)
fprintf(output, _(" %s [options]\n"), program_invocation_short_name);

fputs(USAGE_OPTIONS, output);
fputs(_(" -a, --active print lastlog excluding '**Never logged in**' users\n"), output);
fputs(_(" -b, --before DAYS print only records older than DAYS\n"), output);
fputs(_(" -C, --clear clear record of a user (requires -u)\n"), output);
fputs(_(" -d, --database FILE use FILE as lastlog2 database\n"), output);
Expand All @@ -131,6 +136,7 @@ static void __attribute__((__noreturn__)) usage(void)
int main(int argc, char **argv)
{
static const struct option longopts[] = {
{"active", no_argument, NULL, 'a'},
{"before", required_argument, NULL, 'b'},
{"clear", no_argument, NULL, 'C'},
{"database", required_argument, NULL, 'd'},
Expand All @@ -157,8 +163,11 @@ int main(int argc, char **argv)

int c;

while ((c = getopt_long(argc, argv, "b:Cd:hi:r:sSt:u:v", longopts, NULL)) != -1) {
while ((c = getopt_long(argc, argv, "ab:Cd:hi:r:sSt:u:v", longopts, NULL)) != -1) {
switch (c) {
case 'a': /* active; print lastlog excluding '**Never logged in**' users */
aflg = 1;
break;
case 'b': /* before DAYS; Print only records older than DAYS */
{
unsigned long days;
Expand Down

0 comments on commit 00bd8b9

Please sign in to comment.