Fix regression in command line parsing
[project/procd.git] / utils / utils.c
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 *
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
8 *
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.
13 */
14
15 #include <libubox/avl.h>
16 #include <libubox/avl-cmp.h>
17 #include "utils.h"
18 #include <asm-generic/setup.h>
19 #include <regex.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24
25 void
26 __blobmsg_list_init(struct blobmsg_list *list, int offset, int len, blobmsg_list_cmp cmp)
27 {
28 avl_init(&list->avl, avl_strcmp, false, NULL);
29 list->node_offset = offset;
30 list->node_len = len;
31 list->cmp = cmp;
32 }
33
34 int
35 blobmsg_list_fill(struct blobmsg_list *list, void *data, int len, bool array)
36 {
37 struct avl_tree *tree = &list->avl;
38 struct blobmsg_list_node *node;
39 struct blob_attr *cur;
40 void *ptr;
41 int count = 0;
42 int rem = len;
43
44 __blob_for_each_attr(cur, data, rem) {
45 if (!blobmsg_check_attr(cur, !array))
46 continue;
47
48 ptr = calloc(1, list->node_len);
49 if (!ptr)
50 return -1;
51
52 node = (void *) ((char *)ptr + list->node_offset);
53 if (array)
54 node->avl.key = blobmsg_data(cur);
55 else
56 node->avl.key = blobmsg_name(cur);
57 node->data = cur;
58 if (avl_insert(tree, &node->avl)) {
59 free(ptr);
60 continue;
61 }
62
63 count++;
64 }
65
66 return count;
67 }
68
69 void
70 blobmsg_list_move(struct blobmsg_list *list, struct blobmsg_list *src)
71 {
72 struct blobmsg_list_node *node, *tmp;
73 void *ptr;
74
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);
78 free(ptr);
79 }
80 }
81 }
82
83 void
84 blobmsg_list_free(struct blobmsg_list *list)
85 {
86 struct blobmsg_list_node *node, *tmp;
87 void *ptr;
88
89 avl_remove_all_elements(&list->avl, node, avl, tmp) {
90 ptr = ((char *) node - list->node_offset);
91 free(ptr);
92 }
93 }
94
95 bool
96 blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2)
97 {
98 struct blobmsg_list_node *n1, *n2;
99 int count = l1->avl.count;
100
101 if (count != l2->avl.count)
102 return false;
103
104 n1 = avl_first_element(&l1->avl, n1, avl);
105 n2 = avl_first_element(&l2->avl, n2, avl);
106
107 while (count-- > 0) {
108 int len;
109
110 len = blob_len(n1->data);
111 if (len != blob_len(n2->data))
112 return false;
113
114 if (memcmp(n1->data, n2->data, len) != 0)
115 return false;
116
117 if (l1->cmp && !l1->cmp(n1, n2))
118 return false;
119
120 if (!count)
121 break;
122
123 n1 = avl_next_element(n1, avl);
124 n2 = avl_next_element(n2, avl);
125 }
126
127 return true;
128 }
129
130 char* get_cmdline_val(const char* name, char* out, int len)
131 {
132 char pattern[CMDLINE_SIZE + 1];
133 char line[CMDLINE_SIZE + 1];
134 char *res = NULL, *tty;
135 int r, fd;
136 regex_t pat_cmdline;
137 regmatch_t matches[2];
138
139 fd = open("/proc/cmdline", O_RDONLY);
140 if (fd < 0)
141 return NULL;
142
143 r = read(fd, line, CMDLINE_SIZE);
144 if ( r <= 0 ) {
145 close(fd);
146 return NULL;
147 }
148 line[r] = '\0';
149 close(fd);
150
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);
157 tty[len-1] = '\0';
158 res = out;
159 }
160
161 regfree(&pat_cmdline);
162
163 return res;
164 }