diff options
author | krion <krion@FreeBSD.org> | 2006-05-27 18:27:41 +0000 |
---|---|---|
committer | krion <krion@FreeBSD.org> | 2006-05-27 18:27:41 +0000 |
commit | 6b57f065966c0f0801af6e04177d20713f95edbb (patch) | |
tree | c4698faa54e7449c66dfcad1a14420aa18cbe3b9 | |
parent | efa20627226a6768196d5119eb965ff8d6cdfbe3 (diff) | |
download | FreeBSD-src-6b57f065966c0f0801af6e04177d20713f95edbb.zip FreeBSD-src-6b57f065966c0f0801af6e04177d20713f95edbb.tar.gz |
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
-rw-r--r-- | usr.bin/find/find.1 | 23 | ||||
-rw-r--r-- | usr.bin/find/function.c | 38 |
2 files changed, 56 insertions, 5 deletions
diff --git a/usr.bin/find/find.1 b/usr.bin/find/find.1 index dd390e9..4af3eea 100644 --- a/usr.bin/find/find.1 +++ b/usr.bin/find/find.1 @@ -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: diff --git a/usr.bin/find/function.c b/usr.bin/find/function.c index 152512d..5de9db0 100644 --- a/usr.bin/find/function.c +++ b/usr.bin/find/function.c @@ -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; } |