File: ll.c

package info (click to toggle)
l2tpns 2.1.21-1.1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze, wheezy
  • size: 820 kB
  • ctags: 1,621
  • sloc: ansic: 16,737; makefile: 160; sh: 142; perl: 139
file content (141 lines) | stat: -rw-r--r-- 2,017 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// L2TPNS Linked List Stuff

char const *cvs_id_ll = "$Id: ll.c,v 1.6 2004/11/18 08:12:55 bodea Exp $";

#include <stdio.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
#include "ll.h"

linked_list *ll_init()
{
	return (linked_list *)calloc(sizeof(linked_list), 1);
}

void ll_done(linked_list *l)
{
	li *i = l->head, *n;

	while (i)
	{
		n = i->next;
		free(i);
		i = n;
	}

	free(l);
}

li *ll_push(linked_list *l, void *data)
{
	li *i;

	if (!l) return NULL;
	if (!(i = (li *)calloc(sizeof(li), 1))) return NULL;

	i->data = data;
	i->next = NULL;
	if (l->end)
		l->end->next = i;
	else
		l->head = i;
	l->end = i;

	return i;
}

void *ll_pop(linked_list *l)
{
	li *i;
	void *data;

	if (!l) return NULL;
	if (!l->head)
		return NULL;

	data = l->head->data;
	i = l->head->next;
	free(l->head);
	l->head = i;
	return data;
}

void ll_iterate(linked_list *l, int(*func)(void *))
{
	li *i;
	if (!l || !func) return;

	for (i = l->head; i; i = i->next)
	{
		if (i->data && !func(i->data))
			break;
	}
}

void ll_reset(linked_list *l)
{
	if (!l) return;
	l->current = NULL;
}

void *ll_next(linked_list *l)
{
	if (!l) return NULL;
	if (!l->current)
		l->current = l->head;
	else
		l->current = l->current->next;
	if (!l->current)
		return NULL;
	return l->current->data;
}

void ll_delete(linked_list *l, void *data)
{
	li *i = l->head, *p = NULL;

	while (i)
	{
		if (i->data == data)
		{
			if (l->head == i) l->head = i->next;
			if (l->end == i)  l->end = p;
			if (p)            p->next = i->next;
			free(i);
			l->current = NULL;
			return;
		}
		p = i;
		i = i->next;
	}
}

int ll_size(linked_list *l)
{
	int count = 0;
	li *i;

	if (!l) return 0;

	for (i = l->head; i; i = i->next)
		if (i->data) count++;

	return count;
}

int ll_contains(linked_list *l, void *search)
{
	li *i;
	for (i = l->head; i; i = i->next)
		if (i->data == search)
			return 1;
	return 0;
}