[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[ossec-cvs] ossec-hids: dirtree_op.c (NEW) list_op.c (HEAD) store_op.c (HEAD) [dcid]



Module name:	ossec-hids
Changes by:	dcid	07/08/09 21:56:23

Modified files:
	list_op.c store_op.c
Added files:
	dirtree_op.c

Log message:
Description: Adding a directory tree lib, fixing CONTRIB entry and a few more license tweaks..
Reviewed by: dcid
Bug:

--- NEW FILE: dirtree_op.c ---
/* @(#) $Id: dirtree_op.c,v 1.1 2007/08/10 00:56:23 dcid Exp $ */

/* Copyright (C) 2003-2007 Daniel B. Cid <dcid@xxxxxxxxx>
 * All rights reserved.
 *
 * This program is a free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public
 * License (version 3) as published by the FSF - Free Software
 * Foundation.
 *
 * License details at the LICENSE file included with OSSEC or
 * online at: http://www.ossec.net/en/licensing.html
 */
          

/* Common API for dealing with directory trees */ 


#include "shared.h"


/* Create the tree 
 * Return NULL on error
 */
OSDirTree *OSDirTree_Create()
{
    OSDirTree *my_tree;

    my_tree = calloc(1, sizeof(OSDirTree));
    if(!my_tree)
    {
        return(NULL);
    }
    
    my_tree->first_node = NULL;
    my_tree->last_node = NULL;
    
    return(my_tree);
}



/* Get first node from tree (starting from parent) 
 * Returns null on invalid tree (not initialized)
 */
OSTreeNode *OSDirTree_GetFirstNode(OSDirTree *tree)
{
    return(tree->first_node);
}



/** OSDirTree *_OSTreeNode_Add
 * Internal call, looks up for an entry in the middle of the tree.
 * Should not be called directly.
 */
OSDirTree *_OSTreeNode_Add(OSDirTree *tree, char *str, 
                           void *data, char sep)
{
    char *tmp_str;
    OSTreeNode *newnode;
    OSTreeNode *curnode;


    /* Looking for a next entry */
    tmp_str = strchr(str, sep);
    if(tmp_str)
    {
        *tmp_str = '\0';
    }


    /* Creating new tree */
    if(!tree)
    {
        tree = calloc(1, sizeof(OSDirTree));
        if(!tree)
        {
            return(NULL);
        }

        tree->first_node = NULL;
        tree->last_node = NULL;
    }
    

    curnode = tree->first_node;

    /* Looping on all nodes */
    while(curnode)
    {
        if(strcmp(curnode->value, str) == 0)
        {
            /* If we have other elements, keep going */
            if(tmp_str)
            {
                curnode->child = _OSTreeNode_Add(curnode->child,
                                                 tmp_str +1, data, sep);
            }
            break;
        }
        curnode = curnode->next;
    }


    /* Add a new entry, if not found. */
    if(!curnode)
    {
        os_calloc(1, sizeof(OSTreeNode), newnode);
        printf("XXXX Adding node: %s\n", str);
        

        if(!tree->first_node && !tree->last_node)
        {
            tree->last_node = newnode;
            tree->first_node = newnode;
        }
        else
        {
            if(!tree->first_node)
            {
                tree->first_node = newnode;
            }
            tree->last_node->next = newnode;
        }

        newnode->next = NULL;
        tree->last_node = newnode;
        os_strdup(str, newnode->value);


        /* If we have other elements, keep going */
        if(tmp_str)
        {
            newnode->child = _OSTreeNode_Add(newnode->child,
                    tmp_str +1, data, sep);
            newnode->data = NULL;
        }
        /* Otherwise, set the data in here */
        else
        {
            newnode->data = data;
            newnode->child = NULL;
        }
    }


    /* Fixing the string back */
    if(tmp_str)
    {
        *tmp_str = sep;
    }
                                

    return(tree);
}
        


/** void *OSDirTree_AddToTree
 * Adds a new string to the tree, setting the data at the final leaf.
 * The tree will be divided by the "separator", where each token
 * will delimiter the child.
 * For example, /etc/my/name.conf will become:
 *              /etc/
 *                   -> /my
 *                        -> /name.conf
 * Str must not be NULL.
 */
void OSDirTree_AddToTree(OSDirTree *tree, char *str, void *data, char sep)
{
    char *tmp_str;
    OSTreeNode *newnode;
    OSTreeNode *curnode;
    
    
    /* First character doesn't count as a separator */
    tmp_str = strchr(str +1, sep);
    if(tmp_str)
    {
        *tmp_str = '\0';
    }
    
    
    /* If our tree is not empty, look for the main entry */
    curnode = tree->first_node;
    while(curnode)
    {
        if(strcmp(str, curnode->value) == 0)
        {
            /* If we have other elements, keep going */
            if(tmp_str)
            {
                curnode->child = _OSTreeNode_Add(curnode->child, 
                                                 tmp_str +1, data, sep);
            }
            break;
        }

        curnode = curnode->next;
    }


    /* If we didn't find an entry, create one. */
    if(!curnode)
    {
        os_calloc(1, sizeof(OSTreeNode), newnode);
        printf("XX Adding MAIN node: %s\n", str);

        if(!tree->first_node && !tree->last_node)
        {
            tree->last_node = newnode;
            tree->first_node = newnode;
        }
        else
        {
            if(!tree->first_node)
            {
                tree->first_node = newnode;
            }
            tree->last_node->next = newnode;
        }
        
        newnode->next = NULL;
        tree->last_node = newnode;
        os_strdup(str, newnode->value);


        /* If we have other elements, keep going */
        if(tmp_str)
        {
            newnode->child = _OSTreeNode_Add(newnode->child, 
                                             tmp_str +1, data, sep);
            newnode->data = NULL;
        }
        /* Otherwise, set the data in here */
        else
        {
            newnode->data = data;
            newnode->child = NULL;
        }
    }

    /* Fixing the string back */
    if(tmp_str)
    {
        *tmp_str = sep;
    }
}



void *OSDirTree_SearchTree(OSDirTree *tree, char *str, char sep)
{
    void *ret = NULL;
    char *tmp_str;
    OSTreeNode *curnode;


    /* First character doesn't count as a separator */
    tmp_str = strchr(str +1, sep);
    if(tmp_str)
    {
        *tmp_str = '\0';
    }

    printf("looking for: %s\n", str);

    /* If our tree is not empty, look for the main entry */
    curnode = tree->first_node;
    while(curnode)
    {
        printf("comparing: '%s' and '%s'\n", str, curnode->value);
        if(strcmp(str, curnode->value) == 0)
        {
            printf("found node: %s\n", str);

            /* If we have other elements, keep going */
            if(tmp_str)
            {
                ret = OSDirTree_SearchTree(curnode->child, tmp_str +1, sep);
            }
            else
            {
                ret = curnode->data;
            }
            break;
        }

        curnode = curnode->next;
    }


    /* Fixing the string back */
    if(tmp_str)
    {
        *tmp_str = sep;
    }
                                

    return(ret);
}

/* EOF */

Index: list_op.c
===================================================================
RCS file: /usr/cvsroot/ossec-hids/src/shared/list_op.c,v
diff -u -r1.12 -r1.13
--- list_op.c	22 Apr 2007 23:05:33 -0000	1.12
+++ list_op.c	10 Aug 2007 00:56:23 -0000	1.13
@@ -1,6 +1,6 @@
-/*   $OSSEC, list_op.c, v0.1, 2005/10/28, Daniel B. Cid$   */
+/* @(#) $Id$ */
 
-/* Copyright (C) 2005 Daniel B. Cid <dcid@xxxxxxxxx>
+/* Copyright (C) 2005-2007 Daniel B. Cid <dcid@xxxxxxxxx>
  * All right reserved.
  *
  * This program is a free software; you can redistribute it

Index: store_op.c
===================================================================
RCS file: /usr/cvsroot/ossec-hids/src/shared/store_op.c,v
diff -u -r1.3 -r1.4
--- store_op.c	3 Apr 2007 22:16:01 -0000	1.3
+++ store_op.c	10 Aug 2007 00:56:23 -0000	1.4
@@ -1,4 +1,4 @@
-/*   $OSSEC, list_op.c, v0.1, 2005/10/28, Daniel B. Cid$   */
+/* @(#) $Id$ */
 
 /* Copyright (C) 2007 Daniel B. Cid <dcid@xxxxxxxxx>
  * All right reserved.


OSSEC home | Main Index | Thread Index


OSSEC project: www.ossec.net.
Mailling list information: http://www.ossec.net/en/mailing_lists.html.