2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <libubox/avl.h>
16 #include <libubox/avl-cmp.h>
18 #include <asm-generic/setup.h>
21 #include <sys/types.h>
26 __blobmsg_list_init(struct blobmsg_list
*list
, int offset
, int len
, blobmsg_list_cmp cmp
)
28 avl_init(&list
->avl
, avl_strcmp
, false, NULL
);
29 list
->node_offset
= offset
;
35 blobmsg_list_fill(struct blobmsg_list
*list
, void *data
, int len
, bool array
)
37 struct avl_tree
*tree
= &list
->avl
;
38 struct blobmsg_list_node
*node
;
39 struct blob_attr
*cur
;
44 __blob_for_each_attr(cur
, data
, rem
) {
45 if (!blobmsg_check_attr(cur
, !array
))
48 ptr
= calloc(1, list
->node_len
);
52 node
= (void *) ((char *)ptr
+ list
->node_offset
);
54 node
->avl
.key
= blobmsg_data(cur
);
56 node
->avl
.key
= blobmsg_name(cur
);
58 if (avl_insert(tree
, &node
->avl
)) {
70 blobmsg_list_move(struct blobmsg_list
*list
, struct blobmsg_list
*src
)
72 struct blobmsg_list_node
*node
, *tmp
;
75 avl_remove_all_elements(&src
->avl
, node
, avl
, tmp
) {
76 if (avl_insert(&list
->avl
, &node
->avl
)) {
77 ptr
= ((char *) node
- list
->node_offset
);
84 blobmsg_list_free(struct blobmsg_list
*list
)
86 struct blobmsg_list_node
*node
, *tmp
;
89 avl_remove_all_elements(&list
->avl
, node
, avl
, tmp
) {
90 ptr
= ((char *) node
- list
->node_offset
);
96 blobmsg_list_equal(struct blobmsg_list
*l1
, struct blobmsg_list
*l2
)
98 struct blobmsg_list_node
*n1
, *n2
;
99 int count
= l1
->avl
.count
;
101 if (count
!= l2
->avl
.count
)
104 n1
= avl_first_element(&l1
->avl
, n1
, avl
);
105 n2
= avl_first_element(&l2
->avl
, n2
, avl
);
107 while (count
-- > 0) {
110 len
= blob_len(n1
->data
);
111 if (len
!= blob_len(n2
->data
))
114 if (memcmp(n1
->data
, n2
->data
, len
) != 0)
117 if (l1
->cmp
&& !l1
->cmp(n1
, n2
))
123 n1
= avl_next_element(n1
, avl
);
124 n2
= avl_next_element(n2
, avl
);
130 char* get_cmdline_val(const char* name
, char* out
, int len
)
132 char pattern
[COMMAND_LINE_SIZE
+ 1];
133 char line
[COMMAND_LINE_SIZE
+ 1];
134 char *res
= NULL
, *tty
;
137 regmatch_t matches
[2];
139 fd
= open("/proc/cmdline", O_RDONLY
);
143 r
= read(fd
, line
, COMMAND_LINE_SIZE
);
151 sprintf( pattern
, "%s=([^ \n]*)", name
);
152 regcomp(&pat_cmdline
, pattern
, REG_EXTENDED
);
153 if (!regexec(&pat_cmdline
, line
, 2, matches
, 0)) {
154 line
[matches
[1].rm_eo
] = '\0';
155 tty
= (line
+ matches
[1].rm_so
);
156 strncpy(out
, tty
, len
);
161 regfree(&pat_cmdline
);