/
mac_list.c
146 lines (90 loc) · 1.97 KB
/
mac_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
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
142
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//typedef int LIST_ITEM;
#define MAC_LIST_TAIL NULL
//#define check_mac_list
struct mac_list *list_top;
struct mac_list p;
struct mac_list{
char mac_add[18];
struct mac_list *mac_list_next;
};
//init
void mac_list_init(){
while(list_top != NULL){
struct mac_list *delete_pos = list_top;
list_top = list_top->mac_list_next;
free(delete_pos);
}
}
//next
struct mac_list * mac_list_next(struct mac_list * pos){
if(pos != NULL){
return pos->mac_list_next;
}else{
return MAC_LIST_TAIL;
}
}
//top
struct mac_list * mac_list_top(void){
return list_top;
}
//add
void mac_list_add(char item[18]){
if(list_top == MAC_LIST_TAIL){
list_top = (struct mac_list *)malloc(sizeof(struct mac_list));
strcpy(list_top->mac_add, item);
list_top->mac_list_next = MAC_LIST_TAIL;
}else{
struct mac_list *p = list_top;
while(p->mac_list_next != MAC_LIST_TAIL){
p = p->mac_list_next;
}
p->mac_list_next = (struct mac_list *)malloc(sizeof(struct mac_list));
strcpy(p->mac_list_next->mac_add, item);
p->mac_list_next->mac_list_next = MAC_LIST_TAIL;
}
}
int mac_list_count(void){
int cnt=0;
struct mac_list *p;
p = list_top;
while(p != MAC_LIST_TAIL){
cnt++;
p = p->mac_list_next;
}
return cnt;
}
struct mac_list * mac_list_check(char item[18]){
if(list_top == MAC_LIST_TAIL){
mac_list_add(item);
return list_top;
}else{
struct mac_list *p;
p = list_top;
while(p!= MAC_LIST_TAIL){
if(strcmp(p->mac_add, item) == 0){
return p;
}
p = mac_list_next(p);
}
mac_list_add(item);
printf("mac add | %s\n", item);
return p;
}
}
#ifdef check_mac_list
int main(int argc, const char * argv[]){
// system("pwd");
// system("ls -al");
struct mac_list p;
mac_list_init();
mac_list_add("XX.XX.XX.A1");
mac_list_add("XX.XX.XX.A2");
mac_list_add("XX.XX.XX.A3");
mac_list_add("XX.XX.XX.A4");
printf("count %d\n", mac_list_count());
return 0;
}
#endif