summaryrefslogtreecommitdiffstats
path: root/eval.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2016-12-18 12:38:19 -0500
committerTavian Barnes <tavianator@tavianator.com>2016-12-18 13:19:04 -0500
commit4b9592f93a68f88152b390898004e5e54b540cae (patch)
tree18930e6373e5e75aa2999f405971d7a29e4eebc8 /eval.c
parent2ebbe75a8d272458cd3229b6033b2a5ce19b105b (diff)
downloadbfs-4b9592f93a68f88152b390898004e5e54b540cae.tar.xz
Implement -regex, -iregex, and -regextype/-E
Diffstat (limited to 'eval.c')
-rw-r--r--eval.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/eval.c b/eval.c
index ebead3b..123c37a 100644
--- a/eval.c
+++ b/eval.c
@@ -743,6 +743,39 @@ bool eval_quit(const struct expr *expr, struct eval_state *state) {
}
/**
+ * -i?regex test.
+ */
+bool eval_regex(const struct expr *expr, struct eval_state *state) {
+ const char *path = state->ftwbuf->path;
+ size_t len = strlen(path);
+ regmatch_t match = {
+ .rm_so = 0,
+ .rm_eo = len,
+ };
+
+ int flags = 0;
+#ifdef REG_STARTEND
+ flags |= REG_STARTEND;
+#endif
+ int err = regexec(expr->regex, path, 1, &match, flags);
+ if (err == 0) {
+ return match.rm_so == 0 && match.rm_eo == len;
+ } else {
+ if (err != REG_NOMATCH) {
+ char *str = xregerror(err, expr->regex);
+ if (str) {
+ pretty_error(state->cmdline->stderr_colors,
+ "'%s': %s\n", path, str);
+ free(str);
+ } else {
+ perror("xregerror()");
+ }
+ }
+ return false;
+ }
+}
+
+/**
* -samefile test.
*/
bool eval_samefile(const struct expr *expr, struct eval_state *state) {