1 From e5221a142e8ee4509734c17584f898a1f4ac86cc Mon Sep 17 00:00:00 2001
2 From: Denys Vlasenko <vda.linux@googlemail.com>
3 Date: Wed, 19 Oct 2011 02:37:08 +0200
4 Subject: [PATCH 2/6] ifupdown: code shrink
7 keywords_up_down - 43 +43
8 set_environ 371 259 -112
9 ifupdown_main 2194 2073 -121
10 ------------------------------------------------------------------------------
11 (add/remove: 1/0 grow/shrink: 0/2 up/down: 43/-233) Total: -190 bytes
13 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
15 networking/ifupdown.c | 42 +++++++++++++++---------------------------
16 1 file changed, 15 insertions(+), 27 deletions(-)
18 diff --git a/networking/ifupdown.c b/networking/ifupdown.c
19 index abc6b58..5946323 100644
20 --- a/networking/ifupdown.c
21 +++ b/networking/ifupdown.c
22 @@ -87,7 +87,6 @@ struct mapping_defn_t {
30 @@ -102,7 +101,6 @@ struct interface_defn_t {
31 const struct method_t *method;
36 struct variable_t *option;
38 @@ -138,6 +136,16 @@ struct globals {
39 #define INIT_G() do { } while (0)
42 +static const char keywords_up_down[] ALIGN1 =
52 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
54 static void addstr(char **bufp, const char *str, size_t str_length)
55 @@ -803,7 +811,6 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
56 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
57 currmap->match[currmap->n_matches++] = xstrdup(first_word);
59 - /*currmap->max_mappings = 0; - done by xzalloc */
60 /*currmap->n_mappings = 0;*/
61 /*currmap->mapping = NULL;*/
62 /*currmap->script = NULL;*/
63 @@ -888,25 +895,16 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
64 if (rest_of_line[0] == '\0')
65 bb_error_msg_and_die("option with empty value \"%s\"", buf);
67 - if (strcmp(first_word, "up") != 0
68 - && strcmp(first_word, "down") != 0
69 - && strcmp(first_word, "pre-up") != 0
70 - && strcmp(first_word, "pre-down") != 0
71 - && strcmp(first_word, "post-up") != 0
72 - && strcmp(first_word, "post-down") != 0
74 + /* If not one of "up", "down",... words... */
75 + if (index_in_strings(keywords_up_down, first_word) < 0) {
77 for (i = 0; i < currif->n_options; i++) {
78 if (strcmp(currif->option[i].name, first_word) == 0)
79 bb_error_msg_and_die("duplicate option \"%s\"", buf);
82 - if (currif->n_options >= currif->max_options) {
83 - currif->max_options += 10;
84 - currif->option = xrealloc(currif->option,
85 - sizeof(*currif->option) * currif->max_options);
87 debug_noise("\t%s=%s\n", first_word, rest_of_line);
88 + currif->option = xrealloc_vector(currif->option, 4, currif->n_options);
89 currif->option[currif->n_options].name = xstrdup(first_word);
90 currif->option[currif->n_options].value = xstrdup(rest_of_line);
92 @@ -918,11 +916,7 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
93 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
94 currmap->script = xstrdup(next_word(&rest_of_line));
95 } else if (strcmp(first_word, "map") == 0) {
96 - if (currmap->n_mappings >= currmap->max_mappings) {
97 - currmap->max_mappings = currmap->max_mappings * 2 + 1;
98 - currmap->mapping = xrealloc(currmap->mapping,
99 - sizeof(char *) * currmap->max_mappings);
101 + currmap->mapping = xrealloc_vector(currmap->mapping, 2, currmap->n_mappings);
102 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
103 currmap->n_mappings++;
105 @@ -986,13 +980,7 @@ static void set_environ(struct interface_defn_t *iface, const char *mode)
108 for (i = 0; i < iface->n_options; i++) {
109 - if (strcmp(iface->option[i].name, "up") == 0
110 - || strcmp(iface->option[i].name, "down") == 0
111 - || strcmp(iface->option[i].name, "pre-up") == 0
112 - || strcmp(iface->option[i].name, "pre-down") == 0
113 - || strcmp(iface->option[i].name, "post-up") == 0
114 - || strcmp(iface->option[i].name, "post-down") == 0
116 + if (index_in_strings(keywords_up_down, iface->option[i].name) >= 0) {
119 *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);