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