[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    last_fancy -n rows and filtering
From:       Sergey Naumov <sknaumov () gmail ! com>
Date:       2011-05-18 19:35:15
Message-ID: BANLkTim-_+AuPMynaH=+UMZgh2uM=c0Q6A () mail ! gmail ! com
[Download RAW message or body]

Hello.

Here is a first version of patch to last_fancy to support -n
<rows_to_show> flag and filtering by provided command line arguments.
Of course all changes are conditional.

bloat-o-meter (unselected conditionals vs unpatched busybox):
function                                             old     new   delta
last_main                                            860     870     +10
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 10/0)               Total: 10 bytes

bloat-o-meter (selected conditionals vs unpatched busybox):
function                                             old     new   delta
show_entry                                           276     381    +105
last_main                                            860     889     +29
.rodata                                             1574    1580      +6
static.rows_shown                                      -       4      +4
rows_to_show                                           -       4      +4
filters                                                -       4      +4
------------------------------------------------------------------------------
(add/remove: 3/0 grow/shrink: 3/0 up/down: 152/0)             Total: 152 bytes

Code could be shrinked a bit if we will not support -n 0 case as "show
all" but "show nothing".
Any suggestions are welcomed.

Thanks in advance,
Sergey Naumov.

["busybox-1.19.0-last_fancy.patch" (text/x-patch)]

diff -uNr busybox-orig/miscutils/Config.src busybox/miscutils/Config.src
--- busybox-orig/miscutils/Config.src	2011-05-12 04:10:33.000000000 +0400
+++ busybox/miscutils/Config.src	2011-05-18 22:40:08.000000000 +0400
@@ -360,6 +360,21 @@
 	  logged into the system (mimics sysvinit last). +900 bytes.
 endchoice
 
+config FEATURE_LAST_FILTER
+	bool "Enable filtering by user/tty"
+	default n
+	depends on LAST && FEATURE_LAST_FANCY
+	help
+	  if <username> or <tty> arguments provided, last will show only
+	  entries that match either one of them.
+
+config FEATURE_LAST_SHOW_N_ROWS
+	bool "Enable -n ROWS_TO_SHOW flag"
+	default n
+	depends on LAST && FEATURE_LAST_FANCY
+	help
+	  Enables -n ROWS_TO_SHOW flag support.
+
 config LESS
 	bool "less"
 	default y
diff -uNr busybox-orig/miscutils/last_fancy.c busybox/miscutils/last_fancy.c
--- busybox-orig/miscutils/last_fancy.c	2011-05-12 04:10:33.000000000 +0400
+++ busybox/miscutils/last_fancy.c	2011-05-18 23:09:28.000000000 +0400
@@ -22,6 +22,14 @@
 #define HEADER_LINE_WIDE  "USER", "TTY", \
 	INET6_ADDRSTRLEN, INET6_ADDRSTRLEN, "HOST", "LOGIN", "  TIME", ""
 
+#if ENABLE_FEATURE_LAST_FILTER
+	char **filters;
+#endif
+
+#if ENABLE_FEATURE_LAST_SHOW_N_ROWS
+	int rows_to_show = 0; /* "0" stands for "all" */
+#endif
+
 enum {
 	NORMAL,
 	LOGGED,
@@ -32,11 +40,25 @@
 };
 
 enum {
-	LAST_OPT_W = (1 << 0),  /* -W wide            */
-	LAST_OPT_f = (1 << 1),  /* -f input file      */
-	LAST_OPT_H = (1 << 2),  /* -H header          */
+	LAST_OPTBIT_W = 0,
+	LAST_OPTBIT_f,
+	IF_FEATURE_LAST_SHOW_N_ROWS(LAST_OPTBIT_n,)
+	LAST_OPTBIT_H, /* has to be last - it is disabled in getopt */
+	LAST_OPT_W = (1 << LAST_OPTBIT_W),  /* -W wide            */
+	LAST_OPT_f = (1 << LAST_OPTBIT_f),  /* -f input file      */
+	IF_FEATURE_LAST_SHOW_N_ROWS(LAST_OPT_n = (1 << LAST_OPTBIT_n),) /* -n rows */
+	LAST_OPT_H = (1 << LAST_OPTBIT_H),  /* -H header          */
 };
 
+
+#define LAST_OPTSTRING "Wf:" \
+	IF_FEATURE_LAST_SHOW_N_ROWS("n:") \
+	/* "H" */
+#define LAST_COMPLEMENTARY "" \
+	IF_FEATURE_LAST_SHOW_N_ROWS("n+:")
+#define LAST_OPTVARS &filename \
+	IF_FEATURE_LAST_SHOW_N_ROWS(,&rows_to_show)
+
 #define show_wide (option_mask32 & LAST_OPT_W)
 
 static void show_entry(struct utmp *ut, int state, time_t dur_secs)
@@ -48,6 +70,32 @@
 	const char *logout_str;
 	const char *duration_str;
 	time_t tmp;
+#if ENABLE_FEATURE_LAST_SHOW_N_ROWS
+	static int rows_shown = 0;
+#endif
+
+#if ENABLE_FEATURE_LAST_FILTER
+	/* filter out records with mismatching user and tty */
+	if (*filters) {
+		char **filter = filters;
+		for (; *filter; filter++)
+			if (strcmp(ut->ut_user, *filter) == 0 ||
+			    strcmp(ut->ut_line, *filter) == 0 )
+				goto match; /* get rid of break; and if (*filter == NULL) */
+		return;
+	}
+match:
+#endif
+
+#if ENABLE_FEATURE_LAST_SHOW_N_ROWS
+	/* if we drop compatibility for -n 0 that shows all now, then
+	 * code can be a bit simpler: just if (rows_to_show-- == 0) return;
+	 * Should we support this useless case?
+	 */
+	if (rows_to_show && rows_shown >= rows_to_show)
+		return;
+	rows_shown++;
+#endif
 
 	/* manpages say ut_tv.tv_sec *is* time_t,
 	 * but some systems have it wrong */
@@ -165,7 +213,8 @@
 	smallint going_down;
 	smallint boot_down;
 
-	opt = getopt32(argv, "Wf:" /* "H" */, &filename);
+	opt_complementary = LAST_COMPLEMENTARY;
+	opt = getopt32(argv, LAST_OPTSTRING, LAST_OPTVARS);
 #ifdef BUT_UTIL_LINUX_LAST_HAS_NO_SUCH_OPT
 	if (opt & LAST_OPT_H) {
 		/* Print header line */
@@ -177,6 +226,10 @@
 	}
 #endif
 
+#if ENABLE_FEATURE_LAST_FILTER
+	filters = argv + optind;
+#endif
+
 	file = xopen(filename, O_RDONLY);
 	{
 		/* in case the file is empty... */


_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic