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