Imported Upstream version 2.4.3
[platform/upstream/audit.git] / auparse / nvpair.c
1 /*
2 * nvpair.c - Minimal linked list library for name-value pairs
3 * Copyright (c) 2007-08 Red Hat Inc., Durham, North Carolina.
4 * All Rights Reserved. 
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 *
20 * Authors:
21 *   Steve Grubb <sgrubb@redhat.com>
22 */
23
24 #include "config.h"
25 #include <stdlib.h>
26 #include "nvpair.h"
27
28
29 void nvpair_create(nvpair *l)
30 {
31         l->head = NULL;
32         l->cur = NULL;
33         l->cnt = 0;
34 }
35
36 void nvpair_append(nvpair *l, nvpnode *node)
37 {
38         nvpnode* newnode = malloc(sizeof(nvpnode));
39
40         newnode->name = node->name;
41         newnode->val = node->val;
42         newnode->next = NULL;
43
44         // if we are at top, fix this up
45         if (l->head == NULL)
46                 l->head = newnode;
47         else {  // Otherwise add pointer to newnode
48                 while (l->cur->next)
49                         l->cur = l->cur->next;
50                 l->cur->next = newnode;
51         }
52
53         // make newnode current
54         l->cur = newnode;
55         l->cnt++;
56 }
57
58 int nvpair_find_val(nvpair *l, long val)
59 {
60         register nvpnode* window = l->head;
61
62         while (window) {
63                 if (window->val == val) {
64                         l->cur = window;
65                         return 1;
66                 }
67                 else
68                         window = window->next;
69         }
70         return 0;
71 }
72
73 void nvpair_clear(nvpair *l)
74 {
75         nvpnode* nextnode;
76         register nvpnode* current;
77
78         current = l->head;
79         while (current) {
80                 nextnode=current->next;
81                 free(current->name);
82                 free(current);
83                 current=nextnode;
84         }
85         l->head = NULL;
86         l->cur = NULL;
87         l->cnt = 0;
88 }
89