patch for lastlog

[email protected] Thu, 25 Aug 2005 18:13:29 -0700
Newsgroups gmane.linux.pld.shadow.general
Message-ID <[email protected]>
Attached is a relatively simple patch against lastlog from shadow-4.0.12 which
adds, what I believe is a useful new functionality: the ability to show only
people who have last logged in more than x days ago.
I came upon this problem when I decided to remove all accounts of users on my
server who haven't logged in during the past two years, so I wrote this quick
patch. I hope this becomes useful for anyone in the same situation.
I'm not sure if --before would be the best name for the parameter, so feel free
to change it if you can find a better name.

Miles
lastlog.diff (text/x-patch, 2.1 KB)
--- lastlog.c.orig	2005-08-24 23:07:14.000000000 -0700
+++ lastlog.c	2005-08-24 23:08:53.000000000 -0700
@@ -50,9 +50,13 @@
 static off_t user;		/* one single user, specified on command line */
 static int days;		/* number of days to consider for print command */
 static time_t seconds;		/* that number of days in seconds */
+static int inverse_days;	/* number of days to consider for print command */
+static time_t inverse_seconds;	/* that number of days in seconds */
+
 
 static int uflg = 0;		/* set if user is a valid user id */
 static int tflg = 0;		/* print is restricted to most recent days */
+static int bflg = 0;		/* print excludes most recent days */
 static struct lastlog lastlog;	/* scratch structure to play with ... */
 static struct stat statbuf;	/* fstat buffer for file size */
 static struct passwd *pwent;
@@ -66,7 +70,8 @@
 			   "Options:\n"
 			   "  -u, --user LOGIN	print lastlog record for user with specified LOGIN\n"
 			   "  -h, --help		display this help message and exit\n"
-			   "  -t, --time DAYS	print only lastlog records more recent than DAYS\n"));
+			   "  -t, --time DAYS	print only lastlog records more recent than DAYS\n"
+			   "  -b, --before DAYS	print only lastlog results older than DAYS\n"));
 	exit (1);
 }
 
@@ -148,6 +153,9 @@
 			if (tflg && NOW - lastlog.ll_time > seconds)
 				continue;
 
+			if (bflg && NOW - lastlog.ll_time < inverse_seconds)
+				continue;
+
 			print_one (pwent);
 		}
 	}
@@ -164,12 +172,13 @@
 		static struct option const longopts[] = {
 			{"help", no_argument, NULL, 'h'},
 			{"time", required_argument, NULL, 't'},
+			{"before", required_argument, NULL, 'b'},
 			{"user", required_argument, NULL, 'u'},
 			{NULL, 0, NULL, '\0'}
 		};
 
 		while ((c =
-			getopt_long (argc, argv, "ht:u:", longopts,
+			getopt_long (argc, argv, "ht:b:u:", longopts,
 				     NULL)) != -1) {
 			switch (c) {
 			case 'h':
@@ -180,6 +189,11 @@
 				seconds = days * DAY;
 				tflg++;
 				break;
+			case 'b':
+				inverse_days = atoi (optarg);
+				inverse_seconds = inverse_days * DAY;
+				bflg++;
+				break;
 			case 'u':
 				pwent = getpwnam (optarg);
 				if (!pwent) {