Imported Upstream version 2.7
[ossec-hids.git] / src / os_regex / os_regex_str.c
1 /*   $OSSEC, os_regex_str.c, v0.1, 2005/12/29, Daniel B. Cid$   */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All right 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 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include "os_regex_internal.h"
17
18
19 /** int OS_StrIsNum(char *str) v0.1
20  * Checks if a specific string is numeric (like "129544")
21  */
22 int OS_StrIsNum(char *str)
23 {
24     if(str == NULL)
25         return(FALSE);
26
27     while(*str != '\0')
28     {
29         if(!_IsD(*str))
30             return(FALSE); /* 0 */
31         str++;
32     }
33
34     return(TRUE);
35 }
36
37
38 /** int OS_StrHowClosedMatch(char *str1, char *str2) v0.1
39  * Returns the number of characters that both strings
40  * have in similar.
41  */
42 int OS_StrHowClosedMatch(char *str1, char *str2)
43 {
44     int count = 0;
45
46     /* They don't match if any of them is null */
47     if(!str1 || !str2)
48     {
49         return(0);
50     }
51
52     do
53     {
54         if(str1[count] != str2[count])
55         {
56             break;
57         }
58
59         count++;
60     }while((str1[count] != '\0') && (str2[count] != '\0'));
61
62     return(count);
63 }
64
65
66
67 /** int OS_StrStartsWith(char *str, char *pattern) v0.1
68  * Verifies if a string starts with the provided pattern.
69  * Returns 1 on success or 0 on failure.
70  */
71 #define startswith(x,y) (strncmp(x,y,strlen(y)) == 0?1:0)
72 #define OS_StrStartsWith startswith
73
74 /* EOF */