Imported Upstream version 2.4.3
[platform/upstream/audit.git] / audisp / audispd-llist.c
1 /*
2 * audispd-llist.c - Minimal linked list library
3 * Copyright (c) 2007,2013 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 "audispd-llist.h"
28
29 void plist_create(conf_llist *l)
30 {
31         l->head = NULL;
32         l->cur = NULL;
33         l->cnt = 0;
34 }
35
36 void plist_last(conf_llist *l)
37 {
38         register lnode* window;
39         
40         if (l->head == NULL)
41                 return;
42
43         window = l->head;
44         while (window->next)
45                 window = window->next;
46         l->cur = window;
47 }
48
49 lnode *plist_next(conf_llist *l)
50 {
51         if (l->cur == NULL)
52                 return NULL;
53         l->cur = l->cur->next;
54         return l->cur;
55 }
56
57 unsigned int plist_count_active(const conf_llist *l)
58 {
59         register lnode* current;
60         unsigned int cnt = 0;
61
62         current = l->head;
63         while (current) {
64                 if (current->p && current->p->active == A_YES)
65                         cnt++;
66                 current=current->next;
67         }
68         return cnt;
69 }
70
71 void plist_append(conf_llist *l, plugin_conf_t *p)
72 {
73         lnode* newnode;
74
75         newnode = malloc(sizeof(lnode));
76
77         if (p) {
78                 void *pp = malloc(sizeof(struct plugin_conf));
79                 if (pp) 
80                         memcpy(pp, p, sizeof(struct plugin_conf));
81                 newnode->p = pp;
82         } else
83                 newnode->p = NULL;
84
85         newnode->next = 0;
86
87         // if we are at top, fix this up
88         if (l->head == NULL)
89                 l->head = newnode;
90         else    // Otherwise add pointer to newnode
91                 l->cur->next = newnode;
92
93         // make newnode current
94         l->cur = newnode;
95         l->cnt++;
96 }
97
98 void plist_clear(conf_llist* l)
99 {
100         lnode* nextnode;
101         register lnode* current;
102
103         current = l->head;
104         while (current) {
105                 nextnode=current->next;
106                 free(current->p);
107                 free(current);
108                 current=nextnode;
109         }
110         l->head = NULL;
111         l->cur = NULL;
112         l->cnt = 0;
113 }
114
115 void plist_mark_all_unchecked(conf_llist* l)
116 {
117         register lnode* current;
118
119         current = l->head;
120         while (current) {
121                 if (current->p)
122                         current->p->checked = 0;
123                 current=current->next;
124         }
125 }
126
127 lnode *plist_find_unchecked(conf_llist* l)
128 {
129         register lnode* current;
130
131         current = l->head;
132         while (current) {
133                 if (current->p && current->p->checked == 0)
134                         return current;
135                 current=current->next;
136         }
137         return NULL;
138 }
139
140 lnode *plist_find_name(conf_llist* l, const char *name)
141 {
142         register lnode* current;
143
144         if (name == NULL)
145                 return NULL;
146
147         current = l->head;
148         while (current) {
149                 if (current->p && current->p->name) {
150                         if (strcmp(current->p->name, name) == 0)
151                                 return current;
152                 }
153                 current=current->next;
154         }
155         return NULL;
156 }
157