]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
misc-utils/lastlog2: Add option -a for listing active users only
authorWanBingjiang <614699596@qq.com>
Wed, 26 Feb 2025 07:17:19 +0000 (15:17 +0800)
committerWanBingjiang <614699596@qq.com>
Thu, 27 Feb 2025 10:01:11 +0000 (18:01 +0800)
misc-utils/lastlog2.8.adoc
misc-utils/lastlog2.c

index c34af24123b82fdf648cb6a45376eaf434660307..5e3289db74ebb1b0df520e6a08d9ac3277b80fb4 100644 (file)
@@ -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_.
 
index 2a68c2580d3a125500a62a5b5a196cdaa0bd67e6..91ba699cf27e6dd27c4815d7bf69a3626919bfee 100644 (file)
@@ -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;
@@ -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",
@@ -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);
@@ -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'},
@@ -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;