Subject: [PATCH iproute2] nstat: case-insensitive pattern matching

From: Anton Danilov <[email-masked]>
Date: Wed, 8 Jul 2020 15:38:02 +0300

The option 'nocase' allows ignore case in the pattern matching.

Examples:
    nstat --nocase *drop*
    nstat -azi icmp*

Signed-off-by: Anton Danilov <[email-masked]>
---
 man/man8/rtacct.8 |  8 +++++++-
 misc/nstat.c      | 14 ++++++++++----
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/man/man8/rtacct.8 b/man/man8/rtacct.8
index ccdbf6ca..cb6ac912 100644
--- a/man/man8/rtacct.8
+++ b/man/man8/rtacct.8
@@ -4,7 +4,7 @@
 nstat, rtacct - network statistics tools.
 
 .SH SYNOPSIS
-Usage: nstat [ -h?vVzrnasd:t:jp ] [ PATTERN [ PATTERN ] ]
+Usage: nstat [ -h?vVzrnasd:t:jpi ] [ PATTERN [ PATTERN ] ]
 .br
 Usage: rtacct [ -h?vVzrnasd:t: ] [ ListOfRealms ]
 
@@ -14,6 +14,9 @@ and
 .B rtacct
 are simple tools to monitor kernel snmp counters and network interface statistics.
 
+.B nstat
+can filter kernel snmp counters by name with one or several specified wildcards.
+
 .SH OPTIONS
 .B \-h, \-\-help
 Print help
@@ -44,6 +47,9 @@ When combined with
 .BR \-\-json ,
 pretty print the output.
 .TP
+.B \-i, \-\-nocase
+Ignore case in pattern matching.
+.TP
 .B \-d, \-\-scan <INTERVAL>
 Run in daemon mode collecting statistics. <INTERVAL> is interval between measurements in seconds.
 .TP
diff --git a/misc/nstat.c b/misc/nstat.c
index 425e75ef..243caebe 100644
--- a/misc/nstat.c
+++ b/misc/nstat.c
@@ -43,6 +43,7 @@ int time_constant;
 double W;
 char **patterns;
 int npatterns;
+int nocase;
 
 char info_source[128];
 int source_mismatch;
@@ -114,7 +115,7 @@ static int match(const char *id)
 		return 1;
 
 	for (i = 0; i < npatterns; i++) {
-		if (!fnmatch(patterns[i], id, 0))
+		if (!fnmatch(patterns[i], id, nocase ? FNM_CASEFOLD : 0))
 			return 1;
 	}
 	return 0;
@@ -551,6 +552,7 @@ static void usage(void)
 		"   -h, --help		this message\n"
 		"   -a, --ignore	ignore history\n"
 		"   -d, --scan=SECS	sample every statistics every SECS\n"
+		"   -i, --nocase	ignore case in pattern matching\n"
 		"   -j, --json		format output in JSON\n"
 		"   -n, --nooutput	do history only\n"
 		"   -p, --pretty	pretty print\n"
@@ -566,11 +568,12 @@ static const struct option longopts[] = {
 	{ "help", 0, 0, 'h' },
 	{ "ignore",  0,  0, 'a' },
 	{ "scan", 1, 0, 'd'},
-	{ "nooutput", 0, 0, 'n' },
+	{ "nocase", 0, 0, 'i' },
 	{ "json", 0, 0, 'j' },
+	{ "nooutput", 0, 0, 'n' },
+	{ "pretty", 0, 0, 'p' },
 	{ "reset", 0, 0, 'r' },
 	{ "noupdate", 0, 0, 's' },
-	{ "pretty", 0, 0, 'p' },
 	{ "interval", 1, 0, 't' },
 	{ "version", 0, 0, 'V' },
 	{ "zeros", 0, 0, 'z' },
@@ -585,7 +588,7 @@ int main(int argc, char *argv[])
 	int ch;
 	int fd;
 
-	while ((ch = getopt_long(argc, argv, "h?vVzrnasd:t:jp",
+	while ((ch = getopt_long(argc, argv, "h?vVzrnasd:t:jpi",
 				 longopts, NULL)) != EOF) {
 		switch (ch) {
 		case 'z':
@@ -619,6 +622,9 @@ int main(int argc, char *argv[])
 		case 'p':
 			pretty = 1;
 			break;
+		case 'i':
+			nocase = 1;
+			break;
 		case 'v':
 		case 'V':
 			printf("nstat utility, iproute2-ss%s\n", SNAPSHOT);
-- 
2.26.2