diff options
author | Amadeusz Żołnowski <aidecoe@aidecoe.name> | 2010-06-09 17:29:36 +0200 |
---|---|---|
committer | Harald Hoyer <harald@redhat.com> | 2010-06-09 17:36:02 +0200 |
commit | 2c24ee9a72b8e35934d2fb82cd9c845a16721799 (patch) | |
tree | d84269f4b429be45f6b7d225340e6003272edcf2 /dracut-functions | |
parent | 169f16715145fd087d61ef7fdfd7e9a3cbb68fc9 (diff) | |
download | dracut-2c24ee9a72b8e35934d2fb82cd9c845a16721799.tar.gz dracut-2c24ee9a72b8e35934d2fb82cd9c845a16721799.tar.bz2 dracut-2c24ee9a72b8e35934d2fb82cd9c845a16721799.zip |
dracut-functions: fun. vercmp optimized; credits go to Victor Lowther
Diffstat (limited to 'dracut-functions')
-rwxr-xr-x | dracut-functions | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/dracut-functions b/dracut-functions index fbac2829..500e3aed 100755 --- a/dracut-functions +++ b/dracut-functions @@ -27,19 +27,20 @@ strstr() { [[ $1 =~ $2 ]]; } # Version comparision function. Returns result similar to C strcmp, # but instead of -1 is 2. Function assumes version scheme like does # Linux kernel. +# $1 < $2 -> 2 +# $1 = $2 -> 0 +# $1 > $2 -> 1 +# To remember it easy, ask the question: Which argument is greater? ;-) vercmp() { - local n1 n2 i=1 - - while true - do - n1=$(echo $1 | cut -d'.' -f$i) - n2=$(echo $2 | cut -d'.' -f$i) - - [[ ! $n1 && ! $n2 ]] && return 0 - [[ $n1 -lt $n2 ]] && return 2 - [[ $n1 -gt $n2 ]] && return 1 - - ((i++)) + local i n1=(${1//./ }) n2=(${2//./ }) + + for ((i=0; ; i++)) + do + [[ ${n1[i]} && ! ${n2[i]} ]] && return 1 + [[ ! ${n1[i]} && ${n2[i]} ]] && return 2 + [[ ${n1[i]} && ${n2[i]} ]] || return 0 + ((${n1[i]} > ${n2[i]})) && return 1 + ((${n1[i]} < ${n2[i]})) && return 2 done } |