forked from hoterran/tcpcollect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.c
67 lines (52 loc) · 1.27 KB
/
user.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "utils.h"
#include "log.h"
#include "adlist.h"
/* conclude user in list */
#define USERNAMEMAXLEN 32
#define TOKEN ','
int userMatch(void *ptr, void *key) {
char *user = (char*)key;
char *listValue = (char*)ptr;
if (0 == strncmp(user, listValue, USERNAMEMAXLEN))
return 1;
else
return 0;
}
/* serperated by comma */
int initUserList(list *l, char *s) {
ASSERT(l && s && (strlen(s) > 0));
l->match = userMatch;
char *p1 = NULL;
char *head = s;
int i = 0;
while (NULL != (p1 = strchr(head, TOKEN))) {
p1[0] = '\0';
dump(L_DEBUG, "user:%s", head);
listAddNodeHead(l, head);
head = p1 + 1;
i++;
}
dump(L_DEBUG, "user:%s", head);
listAddNodeHead(l, head);
i++;
return i;
}
#ifdef _USER_TEST_
int main(int argc, char *argv[]) {
list *l = listCreate();
char u[] = "user1,user2,user3";
ASSERT(3 == initUserList(l, u));
ASSERT(listSearchKey(l, "user1"));
ASSERT(listSearchKey(l, "user2"));
ASSERT(listSearchKey(l, "user3"));
ASSERT(NULL == listSearchKey(l, "user4"));
/* wrong
ASSERT(NULL == listSearchKey(l, "user1"));
*/
return OK;
}
#endif