mirror of
https://https.git.savannah.gnu.org/git/findutils.git
synced 2026-01-26 15:39:06 +00:00
pred.c: consolidate numerical comparisons
Several predicate tests perform a numerical comparison based on the comparison_type (COMP_GT, COMP_LT, COMP_EQ). Factor that out into a utility function. * find/pred.c (compare_num): Add static function. (pred_gid, pred_inum, pred_links, pred_size, pred_uid): Use it.
This commit is contained in:
parent
0c20228608
commit
d44172075a
117
find/pred.c
117
find/pred.c
@ -277,6 +277,27 @@ pred_delete (const char *pathname, struct stat *stat_buf, struct predicate *pred
|
||||
}
|
||||
}
|
||||
|
||||
static bool
|
||||
compare_num (enum comparison_type kind, uintmax_t l_val, uintmax_t val)
|
||||
{
|
||||
switch (kind)
|
||||
{
|
||||
case COMP_GT:
|
||||
if (val > l_val)
|
||||
return true;
|
||||
break;
|
||||
case COMP_LT:
|
||||
if (val < l_val)
|
||||
return true;
|
||||
break;
|
||||
case COMP_EQ:
|
||||
if (val == l_val)
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
pred_empty (const char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
|
||||
{
|
||||
@ -424,22 +445,9 @@ pred_gid (const char *pathname, struct stat *stat_buf, struct predicate *pred_pt
|
||||
{
|
||||
(void) pathname;
|
||||
|
||||
switch (pred_ptr->args.numinfo.kind)
|
||||
{
|
||||
case COMP_GT:
|
||||
if (stat_buf->st_gid > pred_ptr->args.numinfo.l_val)
|
||||
return (true);
|
||||
break;
|
||||
case COMP_LT:
|
||||
if (stat_buf->st_gid < pred_ptr->args.numinfo.l_val)
|
||||
return (true);
|
||||
break;
|
||||
case COMP_EQ:
|
||||
if (stat_buf->st_gid == pred_ptr->args.numinfo.l_val)
|
||||
return (true);
|
||||
break;
|
||||
}
|
||||
return (false);
|
||||
return compare_num (pred_ptr->args.numinfo.kind,
|
||||
pred_ptr->args.numinfo.l_val,
|
||||
stat_buf->st_gid);
|
||||
}
|
||||
|
||||
bool
|
||||
@ -496,22 +504,9 @@ pred_inum (const char *pathname, struct stat *stat_buf, struct predicate *pred_p
|
||||
{
|
||||
(void) pathname;
|
||||
|
||||
switch (pred_ptr->args.numinfo.kind)
|
||||
{
|
||||
case COMP_GT:
|
||||
if (stat_buf->st_ino > pred_ptr->args.numinfo.l_val)
|
||||
return (true);
|
||||
break;
|
||||
case COMP_LT:
|
||||
if (stat_buf->st_ino < pred_ptr->args.numinfo.l_val)
|
||||
return (true);
|
||||
break;
|
||||
case COMP_EQ:
|
||||
if (stat_buf->st_ino == pred_ptr->args.numinfo.l_val)
|
||||
return (true);
|
||||
break;
|
||||
}
|
||||
return (false);
|
||||
return compare_num (pred_ptr->args.numinfo.kind,
|
||||
pred_ptr->args.numinfo.l_val,
|
||||
stat_buf->st_ino);
|
||||
}
|
||||
|
||||
bool
|
||||
@ -529,22 +524,9 @@ pred_links (const char *pathname, struct stat *stat_buf, struct predicate *pred_
|
||||
{
|
||||
(void) pathname;
|
||||
|
||||
switch (pred_ptr->args.numinfo.kind)
|
||||
{
|
||||
case COMP_GT:
|
||||
if (stat_buf->st_nlink > pred_ptr->args.numinfo.l_val)
|
||||
return (true);
|
||||
break;
|
||||
case COMP_LT:
|
||||
if (stat_buf->st_nlink < pred_ptr->args.numinfo.l_val)
|
||||
return (true);
|
||||
break;
|
||||
case COMP_EQ:
|
||||
if (stat_buf->st_nlink == pred_ptr->args.numinfo.l_val)
|
||||
return (true);
|
||||
break;
|
||||
}
|
||||
return (false);
|
||||
return compare_num (pred_ptr->args.numinfo.kind,
|
||||
pred_ptr->args.numinfo.l_val,
|
||||
stat_buf->st_nlink);
|
||||
}
|
||||
|
||||
bool
|
||||
@ -908,22 +890,10 @@ pred_size (const char *pathname, struct stat *stat_buf, struct predicate *pred_p
|
||||
(void) pathname;
|
||||
f_val = ((stat_buf->st_size / pred_ptr->args.size.blocksize)
|
||||
+ (stat_buf->st_size % pred_ptr->args.size.blocksize != 0));
|
||||
switch (pred_ptr->args.size.kind)
|
||||
{
|
||||
case COMP_GT:
|
||||
if (f_val > pred_ptr->args.size.size)
|
||||
return (true);
|
||||
break;
|
||||
case COMP_LT:
|
||||
if (f_val < pred_ptr->args.size.size)
|
||||
return (true);
|
||||
break;
|
||||
case COMP_EQ:
|
||||
if (f_val == pred_ptr->args.size.size)
|
||||
return (true);
|
||||
break;
|
||||
}
|
||||
return (false);
|
||||
|
||||
return compare_num (pred_ptr->args.size.kind,
|
||||
pred_ptr->args.size.size,
|
||||
f_val);
|
||||
}
|
||||
|
||||
bool
|
||||
@ -1076,22 +1046,9 @@ bool
|
||||
pred_uid (const char *pathname, struct stat *stat_buf, struct predicate *pred_ptr)
|
||||
{
|
||||
(void) pathname;
|
||||
switch (pred_ptr->args.numinfo.kind)
|
||||
{
|
||||
case COMP_GT:
|
||||
if (stat_buf->st_uid > pred_ptr->args.numinfo.l_val)
|
||||
return (true);
|
||||
break;
|
||||
case COMP_LT:
|
||||
if (stat_buf->st_uid < pred_ptr->args.numinfo.l_val)
|
||||
return (true);
|
||||
break;
|
||||
case COMP_EQ:
|
||||
if (stat_buf->st_uid == pred_ptr->args.numinfo.l_val)
|
||||
return (true);
|
||||
break;
|
||||
}
|
||||
return (false);
|
||||
return compare_num (pred_ptr->args.numinfo.kind,
|
||||
pred_ptr->args.numinfo.l_val,
|
||||
stat_buf->st_uid);
|
||||
}
|
||||
|
||||
bool
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user