# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / ftpd / list.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "list.h"
5
6 void bftpd_list_add(struct bftpd_list_element **list, void *data)
7 {
8         struct bftpd_list_element *new = malloc(sizeof(struct bftpd_list_element));
9         struct bftpd_list_element *tmp = *list;
10         new->data = data;
11         new->next = NULL;
12         if (tmp) {
13                 while (tmp->next)
14                         tmp = tmp->next;
15                 tmp->next = new;
16         } else
17                 *list = new;
18 }
19
20 void bftpd_list_del(struct bftpd_list_element **list, int index)
21 {
22         struct bftpd_list_element *tmp = *list;
23         struct bftpd_list_element *tmp2;
24         int i;
25         if (!index) {
26                 tmp = tmp->next;
27                 free(*list);
28                 *list = tmp;
29         } else {
30                 for (i = 0; i < index - 1; i++) {
31                         if (!(tmp->next))
32                                 return;
33                         tmp = tmp->next;
34                 }
35                 tmp2 = tmp->next;
36                 tmp->next = tmp->next->next;
37                 free(tmp2);
38         }
39 }
40
41 int bftpd_list_count(struct bftpd_list_element *list)
42 {
43         int i = 1;
44         struct bftpd_list_element *tmp = list;
45         if (!tmp)
46                 return 0;
47         while ((tmp = tmp->next))
48                 i++;
49         return i;
50 }
51
52 void *bftpd_list_get(struct bftpd_list_element *list, int index)
53 {
54         struct bftpd_list_element *tmp = list;
55         int i;
56         for (i = 0; i < index; i++) {
57                 if (!(tmp->next))
58                         return NULL;
59                 tmp = tmp->next;
60         }
61         return tmp->data;
62 }