From f20b10ee7b0568ff800588579c7f74ca74f0c23f Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 26 Jul 2016 13:35:56 -0500 Subject: Move regexec0 into lib (regexec that takes length and matches after NUL). --- lib/lib.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'lib/lib.c') diff --git a/lib/lib.c b/lib/lib.c index 09264c95..5c7696d0 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -1149,3 +1149,38 @@ int readlink0(char *path, char *buf, int len) { return readlinkat0(AT_FDCWD, path, buf, len); } + +// Do regex matching handling embedded NUL bytes in string (hence extra len +// argument). Note that neither the pattern nor the match can currently include +// NUL bytes (even with wildcards) and string must be null terminated at +// string[len]. But this can find a match after the first NUL. +int regexec0(regex_t *preg, char *string, long len, int nmatch, + regmatch_t pmatch[], int eflags) +{ + char *s = string; + + for (;;) { + long ll = 0; + int rc; + + while (len && !*s) { + s++; + len--; + } + while (s[ll] && ll