1 /* @(#) $Id: dirtree_op.c,v 1.4 2009/06/24 18:53:08 dcid Exp $ */
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 3) as published by the FSF - Free Software
11 * License details at the LICENSE file included with OSSEC or
12 * online at: http://www.ossec.net/en/licensing.html
16 /* Common API for dealing with directory trees */
23 * Return NULL on error
25 OSDirTree *OSDirTree_Create()
29 my_tree = calloc(1, sizeof(OSDirTree));
35 my_tree->first_node = NULL;
36 my_tree->last_node = NULL;
43 /* Get first node from tree (starting from parent)
44 * Returns null on invalid tree (not initialized)
46 OSTreeNode *OSDirTree_GetFirstNode(OSDirTree *tree)
48 return(tree->first_node);
53 /** OSDirTree *_OSTreeNode_Add
54 * Internal call, looks up for an entry in the middle of the tree.
55 * Should not be called directly.
57 OSDirTree *_OSTreeNode_Add(OSDirTree *tree, char *str,
65 /* Looking for a next entry */
66 tmp_str = strchr(str, sep);
73 /* Creating new tree */
76 tree = calloc(1, sizeof(OSDirTree));
82 tree->first_node = NULL;
83 tree->last_node = NULL;
87 curnode = tree->first_node;
89 /* Looping on all nodes */
92 if(strcmp(curnode->value, str) == 0)
94 /* If we have other elements, keep going */
97 curnode->child = _OSTreeNode_Add(curnode->child,
98 tmp_str +1, data, sep);
102 curnode = curnode->next;
106 /* Add a new entry, if not found. */
109 os_calloc(1, sizeof(OSTreeNode), newnode);
110 //printf("XXXX Adding node: %s\n", str);
113 if(!tree->first_node && !tree->last_node)
115 tree->last_node = newnode;
116 tree->first_node = newnode;
120 tree->last_node->next = newnode;
123 newnode->next = NULL;
124 tree->last_node = newnode;
125 os_strdup(str, newnode->value);
128 /* If we have other elements, keep going */
131 newnode->child = _OSTreeNode_Add(newnode->child,
132 tmp_str +1, data, sep);
133 newnode->data = NULL;
135 /* Otherwise, set the data in here */
138 newnode->data = data;
139 newnode->child = NULL;
144 /* Fixing the string back */
156 /** void OSDirTree_AddToTree
157 * Adds a new string to the tree, setting the data at the final leaf.
158 * The tree will be divided by the "separator", where each token
159 * will delimiter the child.
160 * For example, /etc/my/name.conf will become:
164 * Str must not be NULL.
166 void OSDirTree_AddToTree(OSDirTree *tree, char *str, void *data, char sep)
173 /* First character doesn't count as a separator */
174 tmp_str = strchr(str +1, sep);
181 curnode = tree->first_node;
184 if(strcmp(str, curnode->value) == 0)
186 /* If we have other elements, keep going */
189 curnode->child = _OSTreeNode_Add(curnode->child,
190 tmp_str +1, data, sep);
195 curnode = curnode->next;
199 /* If we didn't find an entry, create one. */
202 os_calloc(1, sizeof(OSTreeNode), newnode);
203 printf("XX Adding MAIN node: %s\n", str);
205 if(!tree->first_node && !tree->last_node)
207 tree->last_node = newnode;
208 tree->first_node = newnode;
212 printf("XXX last new node: %s\n", tree->last_node->value);
213 tree->last_node->next = newnode;
214 tree->last_node = newnode;
217 newnode->next = NULL;
218 os_strdup(str, newnode->value);
221 /* If we have other elements, keep going */
224 newnode->child = _OSTreeNode_Add(newnode->child,
225 tmp_str +1, data, sep);
226 newnode->data = NULL;
228 /* Otherwise, set the data in here */
231 newnode->data = data;
232 newnode->child = NULL;
236 /* Fixing the string back */
247 void *OSDirTree_SearchTree(OSDirTree *tree, char *str, char sep)
254 /* First character doesn't count as a separator */
255 tmp_str = strchr(str +1, sep);
261 printf("looking for: %s\n", str);
263 /* If our tree is not empty, look for the main entry */
264 curnode = tree->first_node;
267 printf("comparing: '%s' and '%s'\n", str, curnode->value);
268 if(strcmp(str, curnode->value) == 0)
270 printf("found node: %s\n", str);
272 /* If we have other elements, keep going */
275 ret = OSDirTree_SearchTree(curnode->child, tmp_str +1, sep);
284 curnode = curnode->next;
288 /* Fixing the string back */