• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * proc-llist.c - Minimal linked list library
3 * Copyright (c) 2009 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 "proc-llist.h"
28 
list_create(llist * l)29 void list_create(llist *l)
30 {
31 	l->head = NULL;
32 	l->cur = NULL;
33 	l->cnt = 0;
34 }
35 
list_append(llist * l,lnode * node)36 void list_append(llist *l, lnode *node)
37 {
38 	lnode* newnode;
39 
40 	if (node == NULL || l == NULL)
41 		return;
42 
43 	newnode = malloc(sizeof(lnode));
44 	if (newnode == NULL)
45 		return;
46 
47 	newnode->ppid = node->ppid;
48 	newnode->pid = node->pid;
49 	newnode->uid = node->uid;
50 	newnode->inode = node->inode;
51 	// Take custody of the memory
52 	newnode->cmd = node->cmd;
53 	newnode->capabilities = node->capabilities;
54 	newnode->bounds = node->bounds;
55 	newnode->next = NULL;
56 
57 	// if we are at top, fix this up
58 	if (l->head == NULL)
59 		l->head = newnode;
60 	else	// Otherwise add pointer to newnode
61 		l->cur->next = newnode;
62 
63 	// make newnode current
64 	l->cur = newnode;
65 	l->cnt++;
66 }
67 
list_clear(llist * l)68 void list_clear(llist* l)
69 {
70 	lnode* nextnode;
71 	register lnode* cur;
72 
73 	cur = l->head;
74 	while (cur) {
75 		nextnode=cur->next;
76 		free(cur->cmd);
77 		free(cur->capabilities);
78 		free(cur->bounds);
79 		free(cur);
80 		cur=nextnode;
81 	}
82 	l->head = NULL;
83 	l->cur = NULL;
84 	l->cnt = 0;
85 }
86 
list_find_inode(llist * l,unsigned long i)87 lnode *list_find_inode(llist *l, unsigned long i)
88 {
89         register lnode* cur;
90 
91        	cur = l->head;	/* start at the beginning */
92 	while (cur) {
93 		if (cur->inode == i) {
94 			l->cur = cur;
95 			return cur;
96 		} else
97 			cur = cur->next;
98 	}
99 	return NULL;
100 }
101 
102