aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-11-23 15:07:38 +0000
committervda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-11-23 15:07:38 +0000
commiteb8ff925d7af3665ed846f0d9a7db6193821ba58 (patch)
treebcd07b42279654081494fa8b0388743c491326b2
parent05cf9e513b59da78ad4a276c852d33625aaf848d (diff)
downloadbusybox-w32-eb8ff925d7af3665ed846f0d9a7db6193821ba58.tar.gz
busybox-w32-eb8ff925d7af3665ed846f0d9a7db6193821ba58.tar.bz2
busybox-w32-eb8ff925d7af3665ed846f0d9a7db6193821ba58.zip
ifupdown: save some 100+ bytes of code in addstr()
git-svn-id: svn://busybox.net/trunk/busybox@16646 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--networking/ifupdown.c80
1 files changed, 35 insertions, 45 deletions
diff --git a/networking/ifupdown.c b/networking/ifupdown.c
index f72e653b3..d4e49c4c1 100644
--- a/networking/ifupdown.c
+++ b/networking/ifupdown.c
@@ -125,6 +125,7 @@ static int count_netmask_bits(char *dotted_quad)
125 /* Found a netmask... Check if it is dotted quad */ 125 /* Found a netmask... Check if it is dotted quad */
126 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4) 126 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
127 return -1; 127 return -1;
128 // FIXME: will be confused by e.g. 255.0.255.0
128 result = count_bits(a); 129 result = count_bits(a);
129 result += count_bits(b); 130 result += count_bits(b);
130 result += count_bits(c); 131 result += count_bits(c);
@@ -133,32 +134,27 @@ static int count_netmask_bits(char *dotted_quad)
133} 134}
134#endif 135#endif
135 136
136static void addstr(char **buf, size_t *len, size_t *pos, const char *str, size_t str_length) 137static void addstr(char **bufp, const char *str, size_t str_length)
137{ 138{
138 if (*pos + str_length >= *len) { 139 /* xasprintf trick will be smaller, but we are often
139 char *newbuf; 140 * called with str_length == 1 - don't want to have
140 141 * THAT much of malloc/freeing! */
141 newbuf = xrealloc(*buf, *len * 2 + str_length + 1); 142 char *buf = *bufp;
142 *buf = newbuf; 143 int len = (buf ? strlen(buf) : 0);
143 *len = *len * 2 + str_length + 1; 144 str_length++;
144 } 145 buf = xrealloc(buf, len + str_length);
145 146 /* copies at most str_length-1 chars! */
146 while (str_length-- >= 1) { 147 safe_strncpy(buf + len, str, str_length);
147 (*buf)[(*pos)++] = *str; 148 *bufp = buf;
148 str++;
149 }
150 (*buf)[*pos] = '\0';
151} 149}
152 150
153static int strncmpz(const char *l, const char *r, size_t llen) 151static int strncmpz(const char *l, const char *r, size_t llen)
154{ 152{
155 int i = strncmp(l, r, llen); 153 int i = strncmp(l, r, llen);
156 154
157 if (i == 0) { 155 if (i == 0)
158 return -r[llen]; 156 return -r[llen];
159 } else { 157 return i;
160 return i;
161 }
162} 158}
163 159
164static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd) 160static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
@@ -168,31 +164,27 @@ static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
168 if (strncmpz(id, "iface", idlen) == 0) { 164 if (strncmpz(id, "iface", idlen) == 0) {
169 char *result; 165 char *result;
170 static char label_buf[20]; 166 static char label_buf[20];
171 strncpy(label_buf, ifd->iface, 19); 167 safe_strncpy(label_buf, ifd->iface, sizeof(label_buf));
172 label_buf[19]=0;
173 result = strchr(label_buf, ':'); 168 result = strchr(label_buf, ':');
174 if (result) { 169 if (result) {
175 *result=0; 170 *result = '\0';
176 } 171 }
177 return label_buf; 172 return label_buf;
178 } else if (strncmpz(id, "label", idlen) == 0) { 173 }
174 if (strncmpz(id, "label", idlen) == 0) {
179 return ifd->iface; 175 return ifd->iface;
180 } else { 176 }
181 for (i = 0; i < ifd->n_options; i++) { 177 for (i = 0; i < ifd->n_options; i++) {
182 if (strncmpz(id, ifd->option[i].name, idlen) == 0) { 178 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
183 return ifd->option[i].value; 179 return ifd->option[i].value;
184 }
185 } 180 }
186 } 181 }
187
188 return NULL; 182 return NULL;
189} 183}
190 184
191static char *parse(const char *command, struct interface_defn_t *ifd) 185static char *parse(const char *command, struct interface_defn_t *ifd)
192{ 186{
193
194 char *result = NULL; 187 char *result = NULL;
195 size_t pos = 0, len = 0;
196 size_t old_pos[MAX_OPT_DEPTH] = { 0 }; 188 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
197 int okay[MAX_OPT_DEPTH] = { 1 }; 189 int okay[MAX_OPT_DEPTH] = { 1 };
198 int opt_depth = 1; 190 int opt_depth = 1;
@@ -200,26 +192,26 @@ static char *parse(const char *command, struct interface_defn_t *ifd)
200 while (*command) { 192 while (*command) {
201 switch (*command) { 193 switch (*command) {
202 default: 194 default:
203 addstr(&result, &len, &pos, command, 1); 195 addstr(&result, command, 1);
204 command++; 196 command++;
205 break; 197 break;
206 case '\\': 198 case '\\':
207 if (command[1]) { 199 if (command[1]) {
208 addstr(&result, &len, &pos, command + 1, 1); 200 addstr(&result, command + 1, 1);
209 command += 2; 201 command += 2;
210 } else { 202 } else {
211 addstr(&result, &len, &pos, command, 1); 203 addstr(&result, command, 1);
212 command++; 204 command++;
213 } 205 }
214 break; 206 break;
215 case '[': 207 case '[':
216 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) { 208 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
217 old_pos[opt_depth] = pos; 209 old_pos[opt_depth] = strlen(result);
218 okay[opt_depth] = 1; 210 okay[opt_depth] = 1;
219 opt_depth++; 211 opt_depth++;
220 command += 2; 212 command += 2;
221 } else { 213 } else {
222 addstr(&result, &len, &pos, "[", 1); 214 addstr(&result, "[", 1);
223 command++; 215 command++;
224 } 216 }
225 break; 217 break;
@@ -227,12 +219,11 @@ static char *parse(const char *command, struct interface_defn_t *ifd)
227 if (command[1] == ']' && opt_depth > 1) { 219 if (command[1] == ']' && opt_depth > 1) {
228 opt_depth--; 220 opt_depth--;
229 if (!okay[opt_depth]) { 221 if (!okay[opt_depth]) {
230 pos = old_pos[opt_depth]; 222 result[old_pos[opt_depth]] = '\0';
231 result[pos] = '\0';
232 } 223 }
233 command += 2; 224 command += 2;
234 } else { 225 } else {
235 addstr(&result, &len, &pos, "]", 1); 226 addstr(&result, "]", 1);
236 command++; 227 command++;
237 } 228 }
238 break; 229 break;
@@ -252,18 +243,17 @@ static char *parse(const char *command, struct interface_defn_t *ifd)
252 varvalue = get_var(command, nextpercent - command, ifd); 243 varvalue = get_var(command, nextpercent - command, ifd);
253 244
254 if (varvalue) { 245 if (varvalue) {
255 addstr(&result, &len, &pos, varvalue, strlen(varvalue)); 246 addstr(&result, varvalue, strlen(varvalue));
256 } else { 247 } else {
257#ifdef CONFIG_FEATURE_IFUPDOWN_IP 248#ifdef CONFIG_FEATURE_IFUPDOWN_IP
258 /* Sigh... Add a special case for 'ip' to convert from 249 /* Sigh... Add a special case for 'ip' to convert from
259 * dotted quad to bit count style netmasks. */ 250 * dotted quad to bit count style netmasks. */
260 if (strncmp(command, "bnmask", 6)==0) { 251 if (strncmp(command, "bnmask", 6)==0) {
261 int res; 252 unsigned res;
262 varvalue = get_var("netmask", 7, ifd); 253 varvalue = get_var("netmask", 7, ifd);
263 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) { 254 if (varvalue && (res = count_netmask_bits(varvalue)) > 0) {
264 char argument[255]; 255 const char *argument = utoa(res);
265 sprintf(argument, "%d", res); 256 addstr(&result, argument, strlen(argument));
266 addstr(&result, &len, &pos, argument, strlen(argument));
267 command = nextpercent + 1; 257 command = nextpercent + 1;
268 break; 258 break;
269 } 259 }
@@ -567,7 +557,7 @@ static char *next_word(char **buf)
567 unsigned short length; 557 unsigned short length;
568 char *word; 558 char *word;
569 559
570 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) { 560 if (!buf || !*buf || !**buf) {
571 return NULL; 561 return NULL;
572 } 562 }
573 563