Add the capability for a trailing scale indicator to cause the

specified size to be read in the more familiar units of kilobytes,
megabytes, gigabytes, terabytes and petabytes.

PR:		bin/50988
Submitted by:	Matthew Seaman <m.seaman@infracaninophile.co.uk>
MFC after:	7 days
This commit is contained in:
Kirill Ponomarev 2006-05-27 18:27:41 +00:00
parent 4f0289b073
commit 5a890aac57
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=158986
2 changed files with 56 additions and 5 deletions

View File

@ -35,7 +35,7 @@
.\" @(#)find.1 8.7 (Berkeley) 5/9/95
.\" $FreeBSD$
.\"
.Dd April 4, 2006
.Dd May 27, 2006
.Dt FIND 1
.Os
.Sh NAME
@ -681,7 +681,7 @@ but not
.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
@ -692,6 +692,25 @@ then the primary is true if the
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:

View File

@ -138,7 +138,7 @@ find_parsenum(PLAN *plan, const char *option, char *vp, char *endch)
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];
@ -1366,7 +1366,8 @@ c_simple(OPTION *option, char ***argvp __unused)
*
* 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;
@ -1387,6 +1388,7 @@ c_size(OPTION *option, char ***argvp)
char *size_str;
PLAN *new;
char endch;
off_t scale;
size_str = nextarg(option, argvp);
ftsoptions &= ~FTS_NOSTAT;
@ -1394,8 +1396,38 @@ c_size(OPTION *option, char ***argvp)
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 = 0x40000000LL;
break;
case 'T': /* terabytes 1<<40 */
scale = 0x1000000000LL;
break;
case 'P': /* petabytes 1<<50 */
scale = 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 *= scale;
}
return new;
}