Imported Upstream version 2.4.3
[platform/upstream/audit.git] / src / ausearch-nvpair.c
1 /*
2 * ausearch-nvpair.c - Minimal linked list library for name-value pairs
3 * Copyright (c) 2006-08 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 "ausearch-nvpair.h"
27
28
29 void nvlist_create(nvlist *l)
30 {
31         l->head = NULL;
32         l->cur = NULL;
33         l->cnt = 0;
34 }
35
36 nvnode *nvlist_next(nvlist *l)
37 {
38         if (l->cur == NULL)
39                 return NULL;
40         l->cur = l->cur->next;
41         return l->cur;
42 }
43
44 void nvlist_append(nvlist *l, nvnode *node)
45 {
46         nvnode* newnode = malloc(sizeof(nvnode));
47
48         newnode->name = node->name;
49         newnode->val = node->val;
50         newnode->next = NULL;
51
52         // if we are at top, fix this up
53         if (l->head == NULL)
54                 l->head = newnode;
55         else {  // Add pointer to newnode and make sure we are at the end
56                 while (l->cur->next)
57                         l->cur = l->cur->next;
58                 l->cur->next = newnode;
59         }
60
61         // make newnode current
62         l->cur = newnode;
63         l->cnt++;
64 }
65
66 int nvlist_find_val(nvlist *l, long val)
67 {
68         register nvnode* window = l->head;
69
70         while (window) {
71                 if (window->val == val) {
72                         l->cur = window;
73                         return 1;
74                 }
75                 else
76                         window = window->next;
77         }
78         return 0;
79 }
80
81 void nvlist_clear(nvlist* l)
82 {
83         nvnode* nextnode;
84         register nvnode* current;
85
86         current = l->head;
87         while (current) {
88                 nextnode=current->next;
89                 free(current->name);
90                 free(current);
91                 current=nextnode;
92         }
93         l->head = NULL;
94         l->cur = NULL;
95         l->cnt = 0;
96 }
97