1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
15 #include "os_regex_internal.h"
18 /* Split a string into multiples pieces, divided by a char "match".
19 * Returns a NULL terminated array on success or NULL on error.
21 char **OS_StrBreak(char match, const char *str, size_t size)
25 const char *tmp_str = str;
28 /* We can't do anything if str is null */
33 ret = (char **)calloc(size + 1, sizeof(char *));
36 /* Memory error. Should provide a better way to detect it */
40 /* Allocate memory to null */
47 while (*str != '\0') {
49 if ((count < size - 1) && (*str == match)) {
50 ret[count] = (char *)calloc(i, sizeof(char));
52 if (ret[count] == NULL) {
57 ret[count][i - 1] = '\0';
58 strncpy(ret[count], tmp_str, i - 1);
67 } /* leave from here when *str == \0 */
70 /* Just do it if count < size */
72 ret[count] = (char *)calloc(i + 1, sizeof(char));
74 if (ret[count] == NULL) {
80 strncpy(ret[count], tmp_str, i);
84 /* Make sure it is null terminated */
90 /* We shouldn't get to this point
91 * Just let "error" handle that