From: kn Date: Thu, 16 Feb 2023 18:10:28 +0000 (+0000) Subject: Rewrite bsort() from hand-rolled recursive to simpler iterative reusing code X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=c8bf1358a0955fbc9dc93a5b7bb9ec1f7e078495;p=openbsd Rewrite bsort() from hand-rolled recursive to simpler iterative reusing code ksh(1) can sort itself and addel() ensures uniqueness, so reuse both to get a much simpler shell version of `sort -u' that is bug-for-bug compatible with the old one but shorter and easier to tweak/reason about. OK afresh1 --- diff --git a/distrib/miniroot/install.sub b/distrib/miniroot/install.sub index a0858392115..7d445b75e79 100644 --- a/distrib/miniroot/install.sub +++ b/distrib/miniroot/install.sub @@ -1,5 +1,5 @@ #!/bin/ksh -# $OpenBSD: install.sub,v 1.1227 2023/02/09 10:38:41 kn Exp $ +# $OpenBSD: install.sub,v 1.1228 2023/02/16 18:10:28 kn Exp $ # # Copyright (c) 1997-2015 Todd Miller, Theo de Raadt, Ken Westerback # Copyright (c) 2015, Robert Peichaer @@ -109,28 +109,6 @@ wait_cgiinfo() { # Utils functions # ------------------------------------------------------------------------------ -# Sort and print unique list of provided arguments. -bsort() { - local _a=$1 _b _l - - (($#)) && shift || return - - for _b; do - [[ $_a == "$_b" ]] && continue - if [[ $_a > $_b ]]; then - _l="$_a $_l" _a=$_b - else - _l="$_b $_l" - fi - done - - # Output the smallest value found. - (($#)) && echo -n "$_a " || echo -n "$_a" - - # Sort remaining values. - bsort $_l -} - # Test the first argument against the remaining ones, return success on a match. isin() { local _a=$1 _b @@ -163,6 +141,17 @@ rmel() { echo -n "$_c" } +# Sort and print unique list of provided arguments. +bsort() { + local _a _l + + set -s -- $@ + for _a; do + _l=$(addel $_a $_l) + done + echo -n $_l +} + # If possible, print the timestamp received from the ftplist.cgi output, # adjusted with the time elapsed since it was received. http_time() {