[PATCH v4 01/11] common/rc: Add _min() and _max() helpers

Ojaswin Mujoo posted 11 patches 1 month, 3 weeks ago
There is a newer version of this series
[PATCH v4 01/11] common/rc: Add _min() and _max() helpers
Posted by Ojaswin Mujoo 1 month, 3 weeks ago
Many programs open code these functionalities so add it as a generic helper
in common/rc

Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
---
 common/rc | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/common/rc b/common/rc
index 96578d15..3858ddce 100644
--- a/common/rc
+++ b/common/rc
@@ -5873,6 +5873,28 @@ _require_program() {
 	_have_program "$1" || _notrun "$tag required"
 }
 
+_min() {
+	local ret
+
+	for arg in "$@"; do
+		if [ -z "$ret" ] || (( $arg < $ret )); then
+			ret="$arg"
+		fi
+	done
+	echo $ret
+}
+
+_max() {
+	local ret
+
+	for arg in "$@"; do
+		if [ -z "$ret" ] || (( $arg > $ret )); then
+			ret="$arg"
+		fi
+	done
+	echo $ret
+}
+
 ################################################################################
 # make sure this script returns success
 /bin/true
-- 
2.49.0
Re: [PATCH v4 01/11] common/rc: Add _min() and _max() helpers
Posted by David Laight 1 month, 3 weeks ago
On Sun, 10 Aug 2025 19:11:52 +0530
Ojaswin Mujoo <ojaswin@linux.ibm.com> wrote:

> Many programs open code these functionalities so add it as a generic helper
> in common/rc
> 
> Reviewed-by: Darrick J. Wong <djwong@kernel.org>
> Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
> ---
>  common/rc | 22 ++++++++++++++++++++++
>  1 file changed, 22 insertions(+)
> 
> diff --git a/common/rc b/common/rc
> index 96578d15..3858ddce 100644
> --- a/common/rc
> +++ b/common/rc
> @@ -5873,6 +5873,28 @@ _require_program() {
>  	_have_program "$1" || _notrun "$tag required"
>  }
>  
> +_min() {
> +	local ret
> +
> +	for arg in "$@"; do
> +		if [ -z "$ret" ] || (( $arg < $ret )); then
> +			ret="$arg"
> +		fi
> +	done
> +	echo $ret
> +}

Perhaps:
	local ret="$1"
	shift
	for arg in "$@"; do
		ret=$(((arg) < (ret) ? (arg) : (ret)))
	done;
	echo "$ret"
that should work for 'min 10 "2 + 3"' (with bash, but not dash).

	David

> +
> +_max() {
> +	local ret
> +
> +	for arg in "$@"; do
> +		if [ -z "$ret" ] || (( $arg > $ret )); then
> +			ret="$arg"
> +		fi
> +	done
> +	echo $ret
> +}
> +
>  ################################################################################
>  # make sure this script returns success
>  /bin/true
Re: [PATCH v4 01/11] common/rc: Add _min() and _max() helpers
Posted by Ojaswin Mujoo 1 month, 2 weeks ago
On Wed, Aug 13, 2025 at 01:20:34PM +0100, David Laight wrote:
> On Sun, 10 Aug 2025 19:11:52 +0530
> Ojaswin Mujoo <ojaswin@linux.ibm.com> wrote:
> 
> > Many programs open code these functionalities so add it as a generic helper
> > in common/rc
> > 
> > Reviewed-by: Darrick J. Wong <djwong@kernel.org>
> > Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
> > ---
> >  common/rc | 22 ++++++++++++++++++++++
> >  1 file changed, 22 insertions(+)
> > 
> > diff --git a/common/rc b/common/rc
> > index 96578d15..3858ddce 100644
> > --- a/common/rc
> > +++ b/common/rc
> > @@ -5873,6 +5873,28 @@ _require_program() {
> >  	_have_program "$1" || _notrun "$tag required"
> >  }
> >  
> > +_min() {
> > +	local ret
> > +
> > +	for arg in "$@"; do
> > +		if [ -z "$ret" ] || (( $arg < $ret )); then
> > +			ret="$arg"
> > +		fi
> > +	done
> > +	echo $ret
> > +}
> 
> Perhaps:
> 	local ret="$1"
> 	shift
> 	for arg in "$@"; do
> 		ret=$(((arg) < (ret) ? (arg) : (ret)))
> 	done;
> 	echo "$ret"
> that should work for 'min 10 "2 + 3"' (with bash, but not dash).
> 
> 	David

Hi David,

Thanks for the feedback. I agree that your way is slightly better but i
would like to keep the current patch as is since we already have some
reviews on it and I would prefer to keep the code as is (especially
since both ways are close enough). Hope this is okay.

Also, we can always do 

_min 10 $((2 + 3)) 

which is a bit more intuitive imo

Regards,
ojaswin

> 
> > +
> > +_max() {
> > +	local ret
> > +
> > +	for arg in "$@"; do
> > +		if [ -z "$ret" ] || (( $arg > $ret )); then
> > +			ret="$arg"
> > +		fi
> > +	done
> > +	echo $ret
> > +}
> > +
> >  ################################################################################
> >  # make sure this script returns success
> >  /bin/true
>