1 /* $OSSEC, os_regex_strbreak.c, v0.3, 2005/04/05, Daniel B. Cid$ */
3 /* Copyright (C) 2009 Trend Micro Inc.
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
18 #include "os_regex_internal.h"
21 /** char **OS_StrBreak(char match, char *str, int size) v0.2
22 * Split a string into multiples pieces, divided by a char "match".
23 * Returns a NULL terminated array on success or NULL on error.
25 char **OS_StrBreak(char match, const char *str, size_t size)
30 const char *tmp_str = str;
34 /* We can't do anything if str is null */
38 ret = (char **)calloc(size+1, sizeof(char *));
42 /* Memory error. Should provice a better way to detect it */
46 /* Allocating memory to null */
58 if((count < size-1)&&(*str == match))
60 ret[count] = (char *)calloc(i,sizeof(char));
62 if(ret[count] == NULL)
67 /* Copying the string */
68 ret[count][i-1] = '\0';
69 strncpy(ret[count],tmp_str,i-1);
78 } /* leave from here when *str == \0 */
81 /* Just do it if count < size */
84 ret[count] = (char *)calloc(i+1,sizeof(char));
86 if(ret[count] == NULL)
91 /* Copying the string */
93 strncpy(ret[count],tmp_str,i);
97 /* Making sure it is null terminated */
103 /* We shouldn't get to this point
104 * Just let "error" handle that
110 /* Deallocating the memory whe can */