1
1
/* aide, Advanced Intrusion Detection Environment
3
* Copyright (C) 1999,2000,2001,2002 Rami Lehti,Pablo Virolainen
3
* Copyright (C) 1999-2002,2005,2006,2010 Rami Lehti,Pablo Virolainen,
4
* Richard van den Berg, Hannes von Haugwitz
6
7
* This program is free software; you can redistribute it and/or
42
/* list_sorted_insert()
43
* Adds an item in a sorted list:
44
* - The first argument is the head of the list
45
* - The second argument is the data to be added
46
* - The third argument is the function pointer to the compare function to use
47
* - Returns the head of the list
49
list* list_sorted_insert(list* listp, void* data, int (*compare) (const void*, const void*)) {
52
newitem=(list*)malloc(sizeof(list));
54
error(0,"Not enough memory to add a new item to list.\n");
58
list_header* header=(list_header*)malloc(sizeof(list_header));
60
error(0,"Not enough memory for list header allocation\n");
64
newitem->header=header;
71
/* add element in sorted, non-empty list (use insertion sort) */
72
curitem = listp->header->head;
73
newitem->header=listp->header;
75
if (compare(newitem->data,curitem->data) <= 0) {
76
/* new element is the new head */
77
listp->header->head=newitem;
78
curitem->prev=newitem;
79
newitem->next=curitem;
83
/* find position for new element */
84
while(compare(newitem->data, curitem->data) > 0 && curitem->next != NULL) {
85
curitem=curitem->next;
87
if (curitem->next == NULL && compare(newitem->data, curitem->data) > 0) {
88
/* new element is the new tail */
89
listp->header->tail=newitem;
90
curitem->next=newitem;
91
newitem->prev=curitem;
94
/* new element is an inner element */
95
curitem->prev->next=newitem;
96
newitem->prev=curitem->prev;
97
curitem->prev=newitem;
98
newitem->next=curitem;
41
106
* append an item to list
42
107
* returns the head