From m.seaman@infracaninophile.co.uk  Tue Apr 15 07:21:25 2003
Return-Path: <m.seaman@infracaninophile.co.uk>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 9C5FB37B401
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 15 Apr 2003 07:21:25 -0700 (PDT)
Received: from smtp.infracaninophile.co.uk (ns0.infracaninophile.co.uk [81.2.69.218])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 8173043FAF
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 15 Apr 2003 07:21:23 -0700 (PDT)
	(envelope-from m.seaman@infracaninophile.co.uk)
Received: from happy-idiot-talk.infracaninophile.co.uk (localhost [127.0.0.1])
	by smtp.infracaninophile.co.uk (8.12.9/8.12.9) with ESMTP id h3FELBYk046950
	(version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO)
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 15 Apr 2003 15:21:11 +0100 (BST)
	(envelope-from matthew@happy-idiot-talk.infracaninophile.co.uk)
Received: (from matthew@localhost)
	by happy-idiot-talk.infracaninophile.co.uk (8.12.9/8.12.9/Submit) id h3FELBpq046949;
	Tue, 15 Apr 2003 15:21:11 +0100 (BST)
	(envelope-from matthew)
Message-Id: <200304151421.h3FELBpq046949@happy-idiot-talk.infracaninophile.co.uk>
Date: Tue, 15 Apr 2003 15:21:11 +0100 (BST)
From: Matthew Seaman <m.seaman@infracaninophile.co.uk>
Reply-To: Matthew Seaman <m.seaman@infracaninophile.co.uk>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [Patch] find -size -- express argument in kilo, mega... bytes
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         50988
>Category:       bin
>Synopsis:       [Patch] find -size -- express argument in kilo, mega... bytes
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Tue Apr 15 07:30:01 PDT 2003
>Closed-Date:    Thu Jun 01 20:37:07 GMT 2006
>Last-Modified:  Thu Jun 01 20:37:07 GMT 2006
>Originator:     Matthew Seaman
>Release:        FreeBSD 4.8-STABLE i386
>Organization:
Infracaninophile
>Environment:
System: FreeBSD happy-idiot-talk.infracaninophile.co.uk 4.8-STABLE FreeBSD 4.8-STABLE #3: Sat Apr 12 09:42:52 BST 2003 root@happy-idiot-talk.infracaninophile.co.uk:/usr/obj/usr/src/sys/HAPPY-IDIOT-TALK i386


>Description:

find(1)'s -size primary accepts an argument either as a multiple of
512 blocks or a the number of characters (bytes) in a file.  This
patch add the capability for a trailing scale indicator to cause the
specified size to be read in the more familiar units of kilobytes,
megabytes etc.  Eg. to find files more than 23Mb in size:

    find / -size +23M 


>How-To-Repeat:
	
>Fix:

	

--- function.c.diff begins here ---
--- function.c.orig	Tue Apr 15 14:03:08 2003
+++ function.c	Tue Apr 15 14:33:08 2003
@@ -130,7 +130,7 @@
 	value = strtoq(str, &endchar, 10);
 	if (value == 0 && endchar == str)
 		errx(1, "%s: %s: illegal numeric value", option, vp);
-	if (endchar[0] && (endch == NULL || endchar[0] != *endch))
+	if (endchar[0] && endch == NULL)
 		errx(1, "%s: %s: illegal trailing character", option, vp);
 	if (endch)
 		*endch = endchar[0];
@@ -1280,7 +1280,8 @@
  *
  *	True if the file size in bytes, divided by an implementation defined
  *	value and rounded up to the next integer, is n.  If n is followed by
- *	a c, the size is in bytes.
+ *	one of c k M G T P, the size is in bytes, kilobytes, megabytes,
+ *	gigabytes, terabytes or petabytes respectively.
  */
 #define	FIND_SIZE	512
 static int divsize = 1;
@@ -1305,15 +1306,46 @@
 	char *size_str;
 	PLAN *new;
 	char endch;
-
+	off_t scale;
+	
 	size_str = nextarg(option, argvp);
 	ftsoptions &= ~FTS_NOSTAT;
 
 	new = palloc(option);
 	endch = 'c';
 	new->o_data = find_parsenum(new, option->name, size_str, &endch);
-	if (endch == 'c')
+	if (endch != '\0') {
 		divsize = 0;
+
+		switch (endch) {
+		case 'c':			/* characters */
+			scale = 0x1LL;
+			break;
+		case 'k':			/* kilobytes 1<<10 */
+			scale = 0x400LL;
+			break;
+		case 'M':			/* megabytes 1<<20 */
+			scale = 0x100000LL;
+			break;
+		case 'G':			/* gigabytes 1<<30 */
+			scale = 0x4000000LL;
+			break;
+		case 'T':			/* terabytes 1<<40 */
+			scale = 0x1000000000LL;
+			break;
+		case 'P':			/* petabytes 1<<50 */
+			scale = 0x40000000000LL;
+			break;
+		default:
+			errx(1, "%s: %s: illegal trailing character",
+			     option->name, size_str);
+			break;
+		}
+		if (new->o_data > QUAD_MAX / scale)
+			errx(1, "%s: %s: value too large",
+			     option->name, size_str);
+		new->o_data *= scale;
+	}
 	return new;
 }
 
--- function.c.diff ends here ---

--- find.1.diff begins here ---
--- find.1.orig	Tue Apr 15 14:40:26 2003
+++ find.1	Tue Apr 15 15:04:17 2003
@@ -595,7 +595,7 @@
 .Dq Li xyzzy
 or
 .Dq Li /foo/ .
-.It Ic -size Ar n Ns Op Cm c
+.It Ic -size Ar n Ns Op Cm ckMGTP
 True if the file's size, rounded up, in 512\-byte blocks is
 .Ar n .
 If
@@ -606,6 +606,25 @@
 file's size is
 .Ar n
 bytes (characters).
+Similarly if
+.Ar n
+is followed by a scale indicator then the file's size is compared to
+.Ar n
+scaled as:
+.Pp
+.Bl -tag -width indent -compact
+.It Cm k
+kilobytes (1024 bytes)
+.It Cm M
+megabytes (1024 kilobytes)
+.It Cm G
+gigabytes (1024 megabytes)
+.It Cm T
+terabytes (1024 gigabytes)
+.It Cm P
+petabytes (1024 terabytes)
+.El
+.Pp
 .It Ic -type Ar t
 True if the file is of the specified type.
 Possible file types are as follows:
--- find.1.diff ends here ---
>Release-Note:
>Audit-Trail:

From: Matthew Seaman <m.seaman@infracaninophile.co.uk>
To: FreeBSD-gnats-submit@FreeBSD.org
Cc:  
Subject: Re: bin/50988: [Patch] find -size -- express argument in kilo, mega... bytes
Date: Tue, 15 Apr 2003 15:45:00 +0100

 --17pEHd4RhPHOinZp
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 Content-Transfer-Encoding: quoted-printable
 
 Sheesh.  This patch has the correct values for giga and peta byte scaling:
 
 --- function.c.orig	Tue Apr 15 11:59:51 2003
 +++ function.c	Tue Apr 15 15:39:31 2003
 @@ -136,7 +136,7 @@
  	value =3D strtoq(str, &endchar, 10);
  	if (value =3D=3D 0 && endchar =3D=3D str)
  		errx(1, "%s: %s: illegal numeric value", option, vp);
 -	if (endchar[0] && (endch =3D=3D NULL || endchar[0] !=3D *endch))
 +	if (endchar[0] && endch =3D=3D NULL)
  		errx(1, "%s: %s: illegal trailing character", option, vp);
  	if (endch)
  		*endch =3D endchar[0];
 @@ -1349,7 +1349,8 @@
   *
   *	True if the file size in bytes, divided by an implementation defined
   *	value and rounded up to the next integer, is n.  If n is followed by
 - *	a c, the size is in bytes.
 + *	one of c k M G T P, the size is in bytes, kilobytes, megabytes,
 + *	gigabytes, terabytes or petabytes respectively.
   */
  #define	FIND_SIZE	512
  static int divsize =3D 1;
 @@ -1374,15 +1375,46 @@
  	char *size_str;
  	PLAN *new;
  	char endch;
 -
 +	off_t scale;
 +=09
  	size_str =3D nextarg(option, argvp);
  	ftsoptions &=3D ~FTS_NOSTAT;
 =20
  	new =3D palloc(option);
  	endch =3D 'c';
  	new->o_data =3D find_parsenum(new, option->name, size_str, &endch);
 -	if (endch =3D=3D 'c')
 +	if (endch !=3D '\0') {
  		divsize =3D 0;
 +
 +		switch (endch) {
 +		case 'c':			/* characters */
 +			scale =3D 0x1LL;
 +			break;
 +		case 'k':			/* kilobytes 1<<10 */
 +			scale =3D 0x400LL;
 +			break;
 +		case 'M':			/* megabytes 1<<20 */
 +			scale =3D 0x100000LL;
 +			break;
 +		case 'G':			/* gigabytes 1<<30 */
 +			scale =3D 0x40000000LL;
 +			break;
 +		case 'T':			/* terabytes 1<<40 */
 +			scale =3D 0x1000000000LL;
 +			break;
 +		case 'P':			/* petabytes 1<<50 */
 +			scale =3D 0x4000000000000LL;
 +			break;
 +		default:
 +			errx(1, "%s: %s: illegal trailing character",
 +			     option->name, size_str);
 +			break;
 +		}
 +		if (new->o_data > QUAD_MAX / scale)
 +			errx(1, "%s: %s: value too large",
 +			     option->name, size_str);
 +		new->o_data *=3D scale;
 +	}
  	return new;
  }
 =20
 
 	Cheers,
 
 	Matthew
 
 --=20
 Dr Matthew J Seaman MA, D.Phil.                       26 The Paddocks
                                                       Savill Way
 PGP: http://www.infracaninophile.co.uk/pgpkey         Marlow
 Tel: +44 1628 476614                                  Bucks., SL7 1TH UK
 
 --17pEHd4RhPHOinZp
 Content-Type: application/pgp-signature
 Content-Disposition: inline
 
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.2.1 (FreeBSD)
 
 iD8DBQE+nBrsdtESqEQa7a0RAlK7AJ9IgPzgbpZ5tGsX2e04v7ldY6tTvgCcCV3K
 BTk/8r7qkub9dWCOfPDYwu0=
 =7XFv
 -----END PGP SIGNATURE-----
 
 --17pEHd4RhPHOinZp--
State-Changed-From-To: open->patched 
State-Changed-By: krion 
State-Changed-When: Sun May 28 06:36:39 UTC 2006 
State-Changed-Why:  
Committed into HEAD. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=50988 
State-Changed-From-To: patched->closed 
State-Changed-By: krion 
State-Changed-When: Thu Jun 1 20:37:05 UTC 2006 
State-Changed-Why:  
Committed. Thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=50988 
>Unformatted:
