LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH 1/2] ver_linux: Move stderr redirection from function parameter to function body
@ 2018-05-12  9:02 Alexander Kapshuk
  2018-05-12  9:02 ` [PATCH 2/2] ver_linux: Drop redundant calls to system() to test if file is readable Alexander Kapshuk
  0 siblings, 1 reply; 2+ messages in thread
From: Alexander Kapshuk @ 2018-05-12  9:02 UTC (permalink / raw)
  To: linux-kernel; +Cc: gregkh, alexander.kapshuk

Remove stderr redirection to stdout from all the parameters to the
version() function, and put it with the body of the version() function
instead.
This improves code readability.

Signed-off-by: Alexander Kapshuk <alexander.kapshuk@gmail.com>
---
 scripts/ver_linux | 53 ++++++++++++++++++++++++-----------------------
 1 file changed, 27 insertions(+), 26 deletions(-)

diff --git a/scripts/ver_linux b/scripts/ver_linux
index 545ec7388eb7..0b301bd1637d 100755
--- a/scripts/ver_linux
+++ b/scripts/ver_linux
@@ -13,23 +13,23 @@ BEGIN {
 	system("uname -a")
 	printf("\n")
 
-	printversion("GNU C", version("gcc -dumpversion 2>&1"))
-	printversion("GNU Make", version("make --version 2>&1"))
-	printversion("Binutils", version("ld -v 2>&1"))
-	printversion("Util-linux", version("mount --version 2>&1"))
-	printversion("Mount", version("mount --version 2>&1"))
-	printversion("Module-init-tools", version("depmod -V  2>&1"))
-	printversion("E2fsprogs", version("tune2fs 2>&1"))
-	printversion("Jfsutils", version("fsck.jfs -V 2>&1"))
-	printversion("Reiserfsprogs", version("reiserfsck -V 2>&1"))
-	printversion("Reiser4fsprogs", version("fsck.reiser4 -V 2>&1"))
-	printversion("Xfsprogs", version("xfs_db -V 2>&1"))
-	printversion("Pcmciautils", version("pccardctl -V 2>&1"))
-	printversion("Pcmcia-cs", version("cardmgr -V 2>&1"))
-	printversion("Quota-tools", version("quota -V 2>&1"))
-	printversion("PPP", version("pppd --version 2>&1"))
-	printversion("Isdn4k-utils", version("isdnctrl 2>&1"))
-	printversion("Nfs-utils", version("showmount --version 2>&1"))
+	printversion("GNU C", version("gcc -dumpversion"))
+	printversion("GNU Make", version("make --version"))
+	printversion("Binutils", version("ld -v"))
+	printversion("Util-linux", version("mount --version"))
+	printversion("Mount", version("mount --version"))
+	printversion("Module-init-tools", version("depmod -V"))
+	printversion("E2fsprogs", version("tune2fs"))
+	printversion("Jfsutils", version("fsck.jfs -V"))
+	printversion("Reiserfsprogs", version("reiserfsck -V"))
+	printversion("Reiser4fsprogs", version("fsck.reiser4 -V"))
+	printversion("Xfsprogs", version("xfs_db -V"))
+	printversion("Pcmciautils", version("pccardctl -V"))
+	printversion("Pcmcia-cs", version("cardmgr -V"))
+	printversion("Quota-tools", version("quota -V"))
+	printversion("PPP", version("pppd --version"))
+	printversion("Isdn4k-utils", version("isdnctrl"))
+	printversion("Nfs-utils", version("showmount --version"))
 
 	if (system("test -r /proc/self/maps") == 0) {
 		while (getline <"/proc/self/maps" > 0) {
@@ -42,7 +42,7 @@ BEGIN {
 		}
 	}
 
-	printversion("Dynamic linker (ldd)", version("ldd --version 2>&1"))
+	printversion("Dynamic linker (ldd)", version("ldd --version"))
 
 	while ("ldconfig -p 2>/dev/null" | getline > 0) {
 		if (/(libg|stdc)[+]+\.so/) {
@@ -53,14 +53,14 @@ BEGIN {
 	if (system("test -r " libcpp) == 0)
 		printversion("Linux C++ Library", version("readlink " libcpp))
 
-	printversion("Procps", version("ps --version 2>&1"))
-	printversion("Net-tools", version("ifconfig --version 2>&1"))
-	printversion("Kbd", version("loadkeys -V 2>&1"))
-	printversion("Console-tools", version("loadkeys -V 2>&1"))
-	printversion("Oprofile", version("oprofiled --version 2>&1"))
-	printversion("Sh-utils", version("expr --v 2>&1"))
-	printversion("Udev", version("udevadm --version 2>&1"))
-	printversion("Wireless-tools", version("iwconfig --version 2>&1"))
+	printversion("Procps", version("ps --version"))
+	printversion("Net-tools", version("ifconfig --version"))
+	printversion("Kbd", version("loadkeys -V"))
+	printversion("Console-tools", version("loadkeys -V"))
+	printversion("Oprofile", version("oprofiled --version"))
+	printversion("Sh-utils", version("expr --v"))
+	printversion("Udev", version("udevadm --version"))
+	printversion("Wireless-tools", version("iwconfig --version"))
 
 	if (system("test -r /proc/modules") == 0) {
 		while ("sort /proc/modules" | getline > 0) {
@@ -72,6 +72,7 @@ BEGIN {
 }
 
 function version(cmd,    ver) {
+	cmd = cmd " 2>&1"
 	while (cmd | getline > 0) {
 		if (!/ver_linux/ && match($0, /[0-9]+([.]?[0-9]+)+/)) {
 			ver = substr($0, RSTART, RLENGTH)
-- 
2.17.0

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 2/2] ver_linux: Drop redundant calls to system() to test if file is readable
  2018-05-12  9:02 [PATCH 1/2] ver_linux: Move stderr redirection from function parameter to function body Alexander Kapshuk
@ 2018-05-12  9:02 ` Alexander Kapshuk
  0 siblings, 0 replies; 2+ messages in thread
From: Alexander Kapshuk @ 2018-05-12  9:02 UTC (permalink / raw)
  To: linux-kernel; +Cc: gregkh, alexander.kapshuk

Running 'test -r' on an awk variable name whose value is an empty
string results in test being run with no arguments, and causes system()
to return 0, which indicates success when used to test values returned
by function calls. This results in code within the if blocks being run
when it should not be.
Instead of testing if a file is accessible and readable via calls to
system("test -r " file), rely on the value returned by getline to perform
this kind of testing. Getline returns -1 on error, with the code within
the while loops not being run.

Signed-off-by: Alexander Kapshuk <alexander.kapshuk@gmail.com>
---
 scripts/ver_linux | 28 +++++++++++-----------------
 1 file changed, 11 insertions(+), 17 deletions(-)

diff --git a/scripts/ver_linux b/scripts/ver_linux
index 0b301bd1637d..7227994ccf63 100755
--- a/scripts/ver_linux
+++ b/scripts/ver_linux
@@ -31,14 +31,12 @@ BEGIN {
 	printversion("Isdn4k-utils", version("isdnctrl"))
 	printversion("Nfs-utils", version("showmount --version"))
 
-	if (system("test -r /proc/self/maps") == 0) {
-		while (getline <"/proc/self/maps" > 0) {
-			n = split($0, procmaps, "/")
-			if (/libc.*so$/ && match(procmaps[n], /[0-9]+([.]?[0-9]+)+/)) {
-				ver = substr(procmaps[n], RSTART, RLENGTH)
-				printversion("Linux C Library", ver)
-				break
-			}
+	while (getline <"/proc/self/maps" > 0) {
+		n = split($0, procmaps, "/")
+		if (/libc.*so$/ && match(procmaps[n], /[0-9]+([.]?[0-9]+)+/)) {
+			ver = substr(procmaps[n], RSTART, RLENGTH)
+			printversion("Linux C Library", ver)
+			break
 		}
 	}
 
@@ -50,9 +48,7 @@ BEGIN {
 			break
 		}
 	}
-	if (system("test -r " libcpp) == 0)
-		printversion("Linux C++ Library", version("readlink " libcpp))
-
+	printversion("Linux C++ Library", version("readlink " libcpp))
 	printversion("Procps", version("ps --version"))
 	printversion("Net-tools", version("ifconfig --version"))
 	printversion("Kbd", version("loadkeys -V"))
@@ -62,13 +58,11 @@ BEGIN {
 	printversion("Udev", version("udevadm --version"))
 	printversion("Wireless-tools", version("iwconfig --version"))
 
-	if (system("test -r /proc/modules") == 0) {
-		while ("sort /proc/modules" | getline > 0) {
-			mods = mods sep $1
-			sep = " "
-		}
-		printversion("Modules Loaded", mods)
+	while ("sort /proc/modules" | getline > 0) {
+		mods = mods sep $1
+		sep = " "
 	}
+	printversion("Modules Loaded", mods)
 }
 
 function version(cmd,    ver) {
-- 
2.17.0

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-05-12  9:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-12  9:02 [PATCH 1/2] ver_linux: Move stderr redirection from function parameter to function body Alexander Kapshuk
2018-05-12  9:02 ` [PATCH 2/2] ver_linux: Drop redundant calls to system() to test if file is readable Alexander Kapshuk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).