Imported Upstream version 2.4.3
[platform/upstream/audit.git] / src / auditctl-llist.c
1 /*
2 * ausearch-llist.c - Minimal linked list library
3 * Copyright (c) 2005 Red Hat Inc., Durham, North Carolina.
4 * All Rights Reserved. 
5 *
6 * This software may be freely redistributed and/or modified under the
7 * terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * Authors:
21 *   Steve Grubb <sgrubb@redhat.com>
22 */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include "auditctl-llist.h"
27
28 void list_create(llist *l)
29 {
30         l->head = NULL;
31         l->cur = NULL;
32         l->cnt = 0;
33 }
34
35 void list_first(llist *l)
36 {
37         l->cur = l->head;
38 }
39
40 void list_last(llist *l)
41 {
42         register lnode* window;
43         
44         if (l->head == NULL)
45                 return;
46
47         window = l->head;
48         while (window->next)
49                 window = window->next;
50         l->cur = window;
51 }
52
53 lnode *list_next(llist *l)
54 {
55         if (l->cur == NULL)
56                 return NULL;
57         l->cur = l->cur->next;
58         return l->cur;
59 }
60
61 void list_append(llist *l, struct audit_rule_data *r, size_t sz)
62 {
63         lnode* newnode;
64
65         newnode = malloc(sizeof(lnode));
66
67         if (r) {
68                 void *rr = malloc(sz);
69                 if (rr) 
70                         memcpy(rr, r, sz);
71                 newnode->r = rr;
72         } else
73                 newnode->r = NULL;
74
75         newnode->size = sz;
76         newnode->next = 0;
77
78         // if we are at top, fix this up
79         if (l->head == NULL)
80                 l->head = newnode;
81         else    // Otherwise add pointer to newnode
82                 l->cur->next = newnode;
83
84         // make newnode current
85         l->cur = newnode;
86         l->cnt++;
87 }
88
89 void list_clear(llist* l)
90 {
91         lnode* nextnode;
92         register lnode* current;
93
94         current = l->head;
95         while (current) {
96                 nextnode=current->next;
97                 free(current->r);
98                 free(current);
99                 current=nextnode;
100         }
101         l->head = NULL;
102         l->cur = NULL;
103         l->cnt = 0;
104 }
105