1 /* @(#) $Id: ./src/shared/dirtree_op.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
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
12 * License details at the LICENSE file included with OSSEC or
13 * online at: http://www.ossec.net/en/licensing.html
17 /* Common API for dealing with directory trees */
24 * Return NULL on error
26 OSDirTree *OSDirTree_Create()
30 my_tree = calloc(1, sizeof(OSDirTree));
36 my_tree->first_node = NULL;
37 my_tree->last_node = NULL;
44 /* Get first node from tree (starting from parent)
45 * Returns null on invalid tree (not initialized)
47 OSTreeNode *OSDirTree_GetFirstNode(OSDirTree *tree)
49 return(tree->first_node);
54 /** OSDirTree *_OSTreeNode_Add
55 * Internal call, looks up for an entry in the middle of the tree.
56 * Should not be called directly.
58 OSDirTree *_OSTreeNode_Add(OSDirTree *tree, char *str,
66 /* Looking for a next entry */
67 tmp_str = strchr(str, sep);
74 /* Creating new tree */
77 tree = calloc(1, sizeof(OSDirTree));
83 tree->first_node = NULL;
84 tree->last_node = NULL;
88 curnode = tree->first_node;
90 /* Looping on all nodes */
93 if(strcmp(curnode->value, str) == 0)
95 /* If we have other elements, keep going */
98 curnode->child = _OSTreeNode_Add(curnode->child,
99 tmp_str +1, data, sep);
103 curnode = curnode->next;
107 /* Add a new entry, if not found. */
110 os_calloc(1, sizeof(OSTreeNode), newnode);
111 //printf("XXXX Adding node: %s\n", str);
114 if(!tree->first_node && !tree->last_node)
116 tree->last_node = newnode;
117 tree->first_node = newnode;
121 tree->last_node->next = newnode;
124 newnode->next = NULL;
125 tree->last_node = newnode;
126 os_strdup(str, newnode->value);
129 /* If we have other elements, keep going */
132 newnode->child = _OSTreeNode_Add(newnode->child,
133 tmp_str +1, data, sep);
134 newnode->data = NULL;
136 /* Otherwise, set the data in here */
139 newnode->data = data;
140 newnode->child = NULL;
145 /* Fixing the string back */
157 /** void OSDirTree_AddToTree
158 * Adds a new string to the tree, setting the data at the final leaf.
159 * The tree will be divided by the "separator", where each token
160 * will delimiter the child.
161 * For example, /etc/my/name.conf will become:
165 * Str must not be NULL.
167 void OSDirTree_AddToTree(OSDirTree *tree, char *str, void *data, char sep)
174 /* First character doesn't count as a separator */
175 tmp_str = strchr(str +1, sep);
182 curnode = tree->first_node;
185 if(strcmp(str, curnode->value) == 0)
187 /* If we have other elements, keep going */
190 curnode->child = _OSTreeNode_Add(curnode->child,
191 tmp_str +1, data, sep);
196 curnode = curnode->next;
200 /* If we didn't find an entry, create one. */
203 os_calloc(1, sizeof(OSTreeNode), newnode);
204 printf("XX Adding MAIN node: %s\n", str);
206 if(!tree->first_node && !tree->last_node)
208 tree->last_node = newnode;
209 tree->first_node = newnode;
213 printf("XXX last new node: %s\n", tree->last_node->value);
214 tree->last_node->next = newnode;
215 tree->last_node = newnode;
218 newnode->next = NULL;
219 os_strdup(str, newnode->value);
222 /* If we have other elements, keep going */
225 newnode->child = _OSTreeNode_Add(newnode->child,
226 tmp_str +1, data, sep);
227 newnode->data = NULL;
229 /* Otherwise, set the data in here */
232 newnode->data = data;
233 newnode->child = NULL;
237 /* Fixing the string back */
248 void *OSDirTree_SearchTree(OSDirTree *tree, char *str, char sep)
255 /* First character doesn't count as a separator */
256 tmp_str = strchr(str +1, sep);
262 printf("looking for: %s\n", str);
264 /* If our tree is not empty, look for the main entry */
265 curnode = tree->first_node;
268 printf("comparing: '%s' and '%s'\n", str, curnode->value);
269 if(strcmp(str, curnode->value) == 0)
271 printf("found node: %s\n", str);
273 /* If we have other elements, keep going */
276 ret = OSDirTree_SearchTree(curnode->child, tmp_str +1, sep);
285 curnode = curnode->next;
289 /* Fixing the string back */