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
16 #include "os_regex_internal.h"
19 /** char **OS_StrBreak(char match, char *str, int size) v0.2
20 * Split a string into multiples pieces, divided by a char "match".
21 * Returns a NULL terminated array on success or NULL on error.
23 char **OS_StrBreak(char match, char *str, int size)
32 /* We can't do anything if str is null or size <= 0 */
33 if((str == NULL)||(size <= 0))
36 ret = (char **)calloc(size+1, sizeof(char *));
40 /* Memory error. Should provice a better way to detect it */
44 /* Allocating memory to null */
56 if((count < size-1)&&(*str == match))
58 ret[count] = (char *)calloc(i,sizeof(char));
60 if(ret[count] == NULL)
65 /* Copying the string */
66 ret[count][i-1] = '\0';
67 strncpy(ret[count],tmp_str,i-1);
76 } /* leave from here when *str == \0 */
79 /* Just do it if count < size */
82 ret[count] = (char *)calloc(i+1,sizeof(char));
84 if(ret[count] == NULL)
89 /* Copying the string */
91 strncpy(ret[count],tmp_str,i);
95 /* Making sure it is null terminated */
101 /* We shouldn't get to this point
102 * Just let "error" handle that
108 /* Deallocating the memory whe can */