Imported Upstream version 2.4.3
[platform/upstream/audit.git] / tools / aulastlog / aulastlog-llist.c
1 /*
2 * aulastlog-llist.c - Minimal linked list library
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 <stdlib.h>
25 #include <string.h>
26 #include "aulastlog-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 lnode *list_next(llist *l)
36 {
37         if (l->cur == NULL)
38                 return NULL;
39         l->cur = l->cur->next;
40         return l->cur;
41 }
42
43 void list_append(llist *l, lnode *node)
44 {
45         lnode* newnode;
46
47         newnode = malloc(sizeof(lnode));
48
49         newnode->sec = node->sec;
50         newnode->uid = node->uid;
51         newnode->name = strdup(node->name);
52         if (node->host)
53                 newnode->host = strdup(node->host);
54         else
55                 newnode->host = NULL;
56         if (node->term)
57                 newnode->term = strdup(node->term);
58         else
59                 newnode->term = NULL;
60         newnode->item = l->cnt; 
61         newnode->next = NULL;
62
63         // if we are at top, fix this up
64         if (l->head == NULL)
65                 l->head = newnode;
66         else    // Otherwise add pointer to newnode
67                 l->cur->next = newnode;
68
69         // make newnode current
70         l->cur = newnode;
71         l->cnt++;
72 }
73
74 void list_clear(llist* l)
75 {
76         lnode* nextnode;
77         register lnode* current;
78
79         current = l->head;
80         while (current) {
81                 nextnode=current->next;
82                 free(current->name);
83                 free(current->host);
84                 free(current->term);
85                 free(current);
86                 current=nextnode;
87         }
88         l->head = NULL;
89         l->cur = NULL;
90         l->cnt = 0;
91 }
92
93 int list_update_login(llist* l, time_t t)
94 {
95         register lnode* cur;
96         if (l == NULL)
97                 return 0;
98
99         cur=list_get_cur(l);
100         cur->sec = t;
101         return 1;
102 }
103
104 int list_update_host(llist* l, const char *h)
105 {
106         register lnode* cur;
107         if (l == NULL)
108                 return 0;
109
110         cur=list_get_cur(l);
111         if (h) {
112                 free(cur->host);
113                 cur->host = strdup(h);
114         } else
115                 cur->host = NULL;
116         return 1;
117 }
118
119 int list_update_term(llist* l, const char *t)
120 {
121         register lnode* cur;
122         if (l == NULL)
123                 return 0;
124
125         cur=list_get_cur(l);
126         if (t) {
127                 free(cur->term);
128                 cur->term = strdup(t);
129         } else
130                 cur->term = NULL;
131         return 1;
132 }
133
134 lnode *list_find_uid(llist *l, uid_t uid)
135 {
136         register lnode* window;
137                                                                                 
138         window = l->head;       /* start at the beginning */
139         while (window) {
140                 if (window->uid == uid) {
141                         l->cur = window;
142                         return window;
143                 } else
144                         window = window->next;
145         }
146         return NULL;
147 }
148