Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 2 Mar 2012 12:44:44 +0200
From:      Gleb Kurtsou <gleb.kurtsou@gmail.com>
To:        Alexander Best <arundel@freebsd.org>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: small change to du, so it will accepts unit suffixes for negative thresholds
Message-ID:  <20120302104444.GA19811@reks>
In-Reply-To: <20120301233822.GA19709@freebsd.org>
References:  <20120301233822.GA19709@freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On (01/03/2012 23:38), Alexander Best wrote:
> hi there,
> 
> i just noticed that du will not accepts something like the following:
> 
> du -t-500M
> 
> whereas
> 
> du -t500M
> 
> will work. i've attached a patch, which makes unit suffixes in connection with
> negative thresholds possible.

Good catch, thanks. A few minor comments below.

> 
> cheers.
> alex

> diff --git a/usr.bin/du/du.1 b/usr.bin/du/du.1
> index 3db1367..01d2ec1 100644
> --- a/usr.bin/du/du.1
> +++ b/usr.bin/du/du.1
> @@ -137,6 +137,10 @@ If
>  is negative, display only entries for which size is less than the absolute
>  value of
>  .Ar threshold .
> +For both positive and negative values,
> +.Ar threshold
> +accepts unit suffixes
> +.Po see Fl h Li option Pc .
>  .It Fl x
>  File system mount points are not traversed.
>  .El
> diff --git a/usr.bin/du/du.c b/usr.bin/du/du.c
> index 7b47b71..51bfd07 100644
> --- a/usr.bin/du/du.c
> +++ b/usr.bin/du/du.c
> @@ -175,13 +175,18 @@ main(int argc, char *argv[])
>  			break;
>  		case 'r':		 /* Compatibility. */
>  			break;
> -		case 't' :
> +		case 't':
> +			if (strncmp(optarg, "-", 1) == 0) {
Why not optarg[0] == '-', it makes intent more clear.
I think we should support "+500M" as well.

Perhaps we'd better use temporal variable for string value instead of
changing optarg.

Or initialize threshold_sign with 0, set it either to 1 or -1 if optarg
starts with '+' or '-' accordingly, and do
expand_number(optarg + (threshold_sign != 0 ? 1 : 0), &threshold)

> +			    optarg++;
> +			    threshold_sign = -1;
> +			}
>  			if (expand_number(optarg, &threshold) != 0 ||
>  			    threshold == 0) {
>  				warnx("invalid threshold: %s", optarg);
optarg can differ from original value because of optarg++ above.

>  				usage();
> -			} else if (threshold < 0)
> -				threshold_sign = -1;
> +			}
> +			if (threshold_sign == -1)
> +			    threshold = -threshold;
>  			break;
>  		case 'x':
>  			ftsoptions |= FTS_XDEV;

> _______________________________________________
> freebsd-hackers@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20120302104444.GA19811>