It is always a bad idea to mix up different languages
in one environment. Another point is, to bury awk in
base-files. There is no reason for it, these are only
quick hacks from the old days of whiterussian 8-).

now we use ash-builtins and dont fork.
i have done some benchmarks, and the code is
nearly 2 times faster, than the old one.
(10000 passes: 115sec old <-> 62sec new code)

Signed-off-by: Bastian Bittorf <bittorf at bluebottle.com>

>From 726f36df7b668604df4fe6fce6b8e588e05ae881 Mon Sep 17 00:00:00 2001
From: Bastian Bittorf <bitt...@bluebottle.com>
Date: Sun, 16 Jan 2011 12:23:28 +0100
Subject: [PATCH] refactoring find_mtd_part() without awk, without forking, clean coding style

---
 package/base-files/files/etc/functions.sh |   31 ++++++++++++++++++++++------
 1 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/package/base-files/files/etc/functions.sh b/package/base-files/files/etc/functions.sh
index acea617..fe19375 100755
--- a/package/base-files/files/etc/functions.sh
+++ b/package/base-files/files/etc/functions.sh
@@ -220,13 +220,30 @@ include() {
 	done
 }
 
-find_mtd_part() {
-	local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
-	local PREFIX=/dev/mtdblock
-	
-	PART="${PART##mtd}"
-	[ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
-	echo "${PART:+$PREFIX$PART}"
+find_mtd_part()
+{
+	local partition_name="$1"		# e.g. rootfs_data
+	local line partition_number prefix
+
+	while read line; do {
+		case "$line" in
+			*"\"$partition_name\""*)
+				IFS=":"
+				set $line
+				partition_number=${1##mtd}	# mtd3 -> 3
+				unset IFS
+				break
+			;;
+		esac
+	} done </proc/mtd
+
+	if [ -d /dev/mtdblock ]; then
+		prefix="/dev/mtdblock/"
+	else
+		prefix="/dev/mtdblock"
+	fi
+
+	echo "${partition_number:+$prefix$partition_number}"
 }
 
 strtok() { # <string> { <variable> [<separator>] ... }
-- 
1.6.3.3

_______________________________________________
openwrt-devel mailing list
openwrt-devel@lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel

Reply via email to