Imported Upstream version 2.4.3
[platform/upstream/audit.git] / audisp / plugins / prelude / audisp-int.c
1 /*
2 * audisp-int.c - Minimal linked list library for integers
3 * Copyright (c) 2008 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 "config.h"
25 #include <stdlib.h>
26 #include <string.h>
27 #include "audisp-int.h"
28
29 void ilist_create(ilist *l)
30 {
31         l->head = NULL;
32         l->cur = NULL;
33         l->cnt = 0;
34 }
35
36 int_node *ilist_next(ilist *l)
37 {
38         if (l->cur == NULL)
39                 return NULL;
40         l->cur = l->cur->next;
41         return l->cur;
42 }
43
44 void ilist_append(ilist *l, int num)
45 {
46         int_node* newnode;
47
48         newnode = malloc(sizeof(int_node));
49
50         newnode->num = num;
51         newnode->next = NULL;
52
53         // if we are at top, fix this up
54         if (l->head == NULL)
55                 l->head = newnode;
56         else    // Otherwise add pointer to newnode
57                 l->cur->next = newnode;
58
59         // make newnode current
60         l->cur = newnode;
61         l->cnt++;
62 }
63
64 int ilist_find_num(ilist *l, unsigned int num)
65 {
66         register int_node* window = l->head;
67
68         while (window) {
69                 if (window->num == num) {
70                         l->cur = window;
71                         return 1;
72                 }
73                 else
74                         window = window->next;
75         }
76         return 0;
77 }
78
79 void ilist_clear(ilist* l)
80 {
81         int_node* nextnode;
82         register int_node* current;
83
84         if (l == NULL)
85                 return;
86
87         current = l->head;
88         while (current) {
89                 nextnode=current->next;
90                 free(current);
91                 current=nextnode;
92         }
93         l->head = NULL;
94         l->cur = NULL;
95         l->cnt = 0;
96 }
97
98 int ilist_add_if_uniq(ilist *l, int num)
99 {
100         register int_node* cur;
101
102         cur = l->head;
103         while (cur) {
104                 if (cur->num == num) 
105                         return 0;
106                 else
107                         cur = cur->next;
108         }
109
110         /* No matches, append to the end */
111         ilist_append(l, num);
112         return 1;
113 }
114