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>
20 #include <sys/types.h>
25 __blobmsg_list_init(struct blobmsg_list *list, int offset, int len, blobmsg_list_cmp cmp)
27 avl_init(&list->avl, avl_strcmp, false, NULL);
28 list->node_offset = offset;
34 blobmsg_list_fill(struct blobmsg_list *list, void *data, int len, bool array)
36 struct avl_tree *tree = &list->avl;
37 struct blobmsg_list_node *node;
38 struct blob_attr *cur;
43 __blob_for_each_attr(cur, data, rem) {
44 if (!blobmsg_check_attr(cur, !array))
47 ptr = calloc(1, list->node_len);
51 node = (void *) ((char *)ptr + list->node_offset);
53 node->avl.key = blobmsg_data(cur);
55 node->avl.key = blobmsg_name(cur);
57 if (avl_insert(tree, &node->avl)) {
69 blobmsg_list_move(struct blobmsg_list *list, struct blobmsg_list *src)
71 struct blobmsg_list_node *node, *tmp;
74 avl_remove_all_elements(&src->avl, node, avl, tmp) {
75 if (avl_insert(&list->avl, &node->avl)) {
76 ptr = ((char *) node - list->node_offset);
83 blobmsg_list_free(struct blobmsg_list *list)
85 struct blobmsg_list_node *node, *tmp;
88 avl_remove_all_elements(&list->avl, node, avl, tmp) {
89 ptr = ((char *) node - list->node_offset);
95 blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2)
97 struct blobmsg_list_node *n1, *n2;
98 int count = l1->avl.count;
100 if (count != l2->avl.count)
103 n1 = avl_first_element(&l1->avl, n1, avl);
104 n2 = avl_first_element(&l2->avl, n2, avl);
106 while (count-- > 0) {
109 len = blob_len(n1->data);
110 if (len != blob_len(n2->data))
113 if (memcmp(n1->data, n2->data, len) != 0)
116 if (l1->cmp && !l1->cmp(n1, n2))
122 n1 = avl_next_element(n1, avl);
123 n2 = avl_next_element(n2, avl);
129 char* get_cmdline_val(const char* name, char* out, int len)
131 char line[CMDLINE_SIZE + 1], *c, *sptr;
132 int fd = open("/proc/cmdline", O_RDONLY);
133 ssize_t r = read(fd, line, sizeof(line) - 1);
141 for (c = strtok_r(line, " \t\n", &sptr); c;
142 c = strtok_r(NULL, " \t\n", &sptr)) {
143 char *sep = strchr(c, '=');
144 ssize_t klen = sep - c;
145 if (klen < 0 || strncmp(name, c, klen) || name[klen] != 0)
148 strncpy(out, &sep[1], len);