-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
65 lines (60 loc) · 1.25 KB
/
list.c
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
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void bftpd_list_add(struct bftpd_list_element **list, void *data)
{
struct bftpd_list_element *new = malloc(sizeof(struct bftpd_list_element));
struct bftpd_list_element *tmp = *list;
/* make sure "new" is a valid value to avoid segfault */
if (! new)
return;
new->data = data;
new->next = NULL;
if (tmp) {
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
} else
*list = new;
}
void bftpd_list_del(struct bftpd_list_element **list, int index)
{
struct bftpd_list_element *tmp = *list;
struct bftpd_list_element *tmp2;
int i;
if (!index) {
tmp = tmp->next;
free(*list);
*list = tmp;
} else {
for (i = 0; i < index - 1; i++) {
if (!(tmp->next))
return;
tmp = tmp->next;
}
tmp2 = tmp->next;
tmp->next = tmp->next->next;
free(tmp2);
}
}
int bftpd_list_count(struct bftpd_list_element *list)
{
int i = 1;
struct bftpd_list_element *tmp = list;
if (!tmp)
return 0;
while ((tmp = tmp->next))
i++;
return i;
}
void *bftpd_list_get(struct bftpd_list_element *list, int index)
{
struct bftpd_list_element *tmp = list;
int i;
for (i = 0; i < index; i++) {
if (!(tmp->next))
return NULL;
tmp = tmp->next;
}
return tmp->data;
}