new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / shared / regex_op.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #ifndef WIN32
11 #include <regex.h>
12
13 #include "shared.h"
14
15
16 /* Compile a POSIX regex, returning NULL on error
17  * Returns 1 if matches, 0 if not
18  */
19 int OS_PRegex(const char *str, const char *regex)
20 {
21     regex_t preg;
22
23     if (!str || !regex) {
24         return (0);
25     }
26
27     if (regcomp(&preg, regex, REG_EXTENDED | REG_NOSUB) != 0) {
28         merror("%s: Posix Regex compile error (%s).", __local_name, regex);
29         return (0);
30     }
31
32     if (regexec(&preg, str, strlen(str), NULL, 0) != 0) {
33         /* Didn't match */
34         regfree(&preg);
35         return (0);
36     }
37
38     regfree(&preg);
39     return (1);
40
41 }
42
43 #endif /* !WIN32 */