ifupdown: code shrink
function old new delta next_word 78 63 -15 ifupdown_main 2381 2170 -211 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-226) Total: -226 bytes
This commit is contained in:
parent
753f42ab8d
commit
cd5c61cd3b
@ -584,32 +584,24 @@ static const struct address_family_t addr_inet = {
|
|||||||
|
|
||||||
static char *next_word(char **buf)
|
static char *next_word(char **buf)
|
||||||
{
|
{
|
||||||
unsigned short length;
|
unsigned length;
|
||||||
char *word;
|
char *word;
|
||||||
|
|
||||||
if (!buf || !*buf || !**buf) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Skip over leading whitespace */
|
/* Skip over leading whitespace */
|
||||||
word = skip_whitespace(*buf);
|
word = skip_whitespace(*buf);
|
||||||
|
|
||||||
/* Skip over comments */
|
/* Stop on EOL/comments */
|
||||||
if (*word == '#') {
|
if (*word == '#' || *word == '\0')
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
/* Find the length of this word */
|
/* Find the length of this word (can't be 0) */
|
||||||
length = strcspn(word, " \t\n");
|
length = strcspn(word, " \t\n");
|
||||||
if (length == 0) {
|
|
||||||
return NULL;
|
/* Unless we are already at NUL, store NUL and advance */
|
||||||
}
|
if (word[length] != '\0')
|
||||||
|
word[length++] = '\0';
|
||||||
|
|
||||||
*buf = word + length;
|
*buf = word + length;
|
||||||
/*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
|
|
||||||
if (**buf) {
|
|
||||||
**buf = '\0';
|
|
||||||
(*buf)++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return word;
|
return word;
|
||||||
}
|
}
|
||||||
@ -686,27 +678,26 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
|
|||||||
|
|
||||||
if (strcmp(firstword, "mapping") == 0) {
|
if (strcmp(firstword, "mapping") == 0) {
|
||||||
#if ENABLE_FEATURE_IFUPDOWN_MAPPING
|
#if ENABLE_FEATURE_IFUPDOWN_MAPPING
|
||||||
currmap = xzalloc(sizeof(struct mapping_defn_t));
|
currmap = xzalloc(sizeof(*currmap));
|
||||||
|
|
||||||
while ((firstword = next_word(&buf_ptr)) != NULL) {
|
while ((firstword = next_word(&buf_ptr)) != NULL) {
|
||||||
if (currmap->max_matches == currmap->n_matches) {
|
if (currmap->n_matches >= currmap->max_matches) {
|
||||||
currmap->max_matches = currmap->max_matches * 2 + 1;
|
currmap->max_matches = currmap->max_matches * 2 + 1;
|
||||||
currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
|
currmap->match = xrealloc(currmap->match, sizeof(*currmap->match) * currmap->max_matches);
|
||||||
}
|
}
|
||||||
|
|
||||||
currmap->match[currmap->n_matches++] = xstrdup(firstword);
|
currmap->match[currmap->n_matches++] = xstrdup(firstword);
|
||||||
}
|
}
|
||||||
currmap->max_mappings = 0;
|
/*currmap->max_mappings = 0; - done by xzalloc */
|
||||||
currmap->n_mappings = 0;
|
/*currmap->n_mappings = 0;*/
|
||||||
currmap->mapping = NULL;
|
/*currmap->mapping = NULL;*/
|
||||||
currmap->script = NULL;
|
/*currmap->script = NULL;*/
|
||||||
{
|
{
|
||||||
struct mapping_defn_t **where = &defn->mappings;
|
struct mapping_defn_t **where = &defn->mappings;
|
||||||
while (*where != NULL) {
|
while (*where != NULL) {
|
||||||
where = &(*where)->next;
|
where = &(*where)->next;
|
||||||
}
|
}
|
||||||
*where = currmap;
|
*where = currmap;
|
||||||
currmap->next = NULL;
|
/*currmap->next = NULL;*/
|
||||||
}
|
}
|
||||||
debug_noise("Added mapping\n");
|
debug_noise("Added mapping\n");
|
||||||
#endif
|
#endif
|
||||||
@ -727,44 +718,36 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
|
|||||||
char *method_name;
|
char *method_name;
|
||||||
llist_t *iface_list;
|
llist_t *iface_list;
|
||||||
|
|
||||||
currif = xzalloc(sizeof(struct interface_defn_t));
|
currif = xzalloc(sizeof(*currif));
|
||||||
iface_name = next_word(&buf_ptr);
|
iface_name = next_word(&buf_ptr);
|
||||||
address_family_name = next_word(&buf_ptr);
|
address_family_name = next_word(&buf_ptr);
|
||||||
method_name = next_word(&buf_ptr);
|
method_name = next_word(&buf_ptr);
|
||||||
|
|
||||||
if (buf_ptr == NULL) {
|
if (method_name == NULL)
|
||||||
bb_error_msg("too few parameters for line \"%s\"", buf);
|
bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ship any trailing whitespace */
|
/* ship any trailing whitespace */
|
||||||
buf_ptr = skip_whitespace(buf_ptr);
|
buf_ptr = skip_whitespace(buf_ptr);
|
||||||
|
|
||||||
if (buf_ptr[0] != '\0') {
|
if (buf_ptr[0] != '\0' /* && buf_ptr[0] != '#' */)
|
||||||
bb_error_msg("too many parameters \"%s\"", buf);
|
bb_error_msg_and_die("too many parameters \"%s\"", buf);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
currif->iface = xstrdup(iface_name);
|
currif->iface = xstrdup(iface_name);
|
||||||
|
|
||||||
currif->address_family = get_address_family(addr_fams, address_family_name);
|
currif->address_family = get_address_family(addr_fams, address_family_name);
|
||||||
if (!currif->address_family) {
|
if (!currif->address_family)
|
||||||
bb_error_msg("unknown address type \"%s\"", address_family_name);
|
bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
currif->method = get_method(currif->address_family, method_name);
|
currif->method = get_method(currif->address_family, method_name);
|
||||||
if (!currif->method) {
|
if (!currif->method)
|
||||||
bb_error_msg("unknown method \"%s\"", method_name);
|
bb_error_msg_and_die("unknown method \"%s\"", method_name);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
|
for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
|
||||||
struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
|
struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
|
||||||
if ((strcmp(tmp->iface, currif->iface) == 0) &&
|
if ((strcmp(tmp->iface, currif->iface) == 0)
|
||||||
(tmp->address_family == currif->address_family)) {
|
&& (tmp->address_family == currif->address_family)
|
||||||
bb_error_msg("duplicate interface \"%s\"", tmp->iface);
|
) {
|
||||||
return NULL;
|
bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
llist_add_to_end(&(defn->ifaces), (char*)currif);
|
llist_add_to_end(&(defn->ifaces), (char*)currif);
|
||||||
@ -787,73 +770,50 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
|
|||||||
} else {
|
} else {
|
||||||
switch (currently_processing) {
|
switch (currently_processing) {
|
||||||
case IFACE:
|
case IFACE:
|
||||||
{
|
if (buf_ptr[0] == '\0')
|
||||||
|
bb_error_msg_and_die("option with empty value \"%s\"", buf);
|
||||||
|
|
||||||
|
if (strcmp(firstword, "up") != 0
|
||||||
|
&& strcmp(firstword, "down") != 0
|
||||||
|
&& strcmp(firstword, "pre-up") != 0
|
||||||
|
&& strcmp(firstword, "post-down") != 0
|
||||||
|
) {
|
||||||
int i;
|
int i;
|
||||||
|
for (i = 0; i < currif->n_options; i++) {
|
||||||
if (strlen(buf_ptr) == 0) {
|
if (strcmp(currif->option[i].name, firstword) == 0)
|
||||||
bb_error_msg("option with empty value \"%s\"", buf);
|
bb_error_msg_and_die("duplicate option \"%s\"", buf);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(firstword, "up") != 0
|
|
||||||
&& strcmp(firstword, "down") != 0
|
|
||||||
&& strcmp(firstword, "pre-up") != 0
|
|
||||||
&& strcmp(firstword, "post-down") != 0) {
|
|
||||||
for (i = 0; i < currif->n_options; i++) {
|
|
||||||
if (strcmp(currif->option[i].name, firstword) == 0) {
|
|
||||||
bb_error_msg("duplicate option \"%s\"", buf);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (currif->n_options >= currif->max_options) {
|
if (currif->n_options >= currif->max_options) {
|
||||||
struct variable_t *opt;
|
currif->max_options += 10;
|
||||||
|
currif->option = xrealloc(currif->option, sizeof(*currif->option) * currif->max_options);
|
||||||
currif->max_options = currif->max_options + 10;
|
|
||||||
opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
|
|
||||||
currif->option = opt;
|
|
||||||
}
|
}
|
||||||
|
debug_noise("\t%s=%s\n", firstword, buf_ptr);
|
||||||
currif->option[currif->n_options].name = xstrdup(firstword);
|
currif->option[currif->n_options].name = xstrdup(firstword);
|
||||||
currif->option[currif->n_options].value = xstrdup(buf_ptr);
|
currif->option[currif->n_options].value = xstrdup(buf_ptr);
|
||||||
if (!currif->option[currif->n_options].name) {
|
|
||||||
perror(filename);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (!currif->option[currif->n_options].value) {
|
|
||||||
perror(filename);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
|
|
||||||
currif->option[currif->n_options].value);
|
|
||||||
currif->n_options++;
|
currif->n_options++;
|
||||||
break;
|
break;
|
||||||
case MAPPING:
|
case MAPPING:
|
||||||
#if ENABLE_FEATURE_IFUPDOWN_MAPPING
|
#if ENABLE_FEATURE_IFUPDOWN_MAPPING
|
||||||
if (strcmp(firstword, "script") == 0) {
|
if (strcmp(firstword, "script") == 0) {
|
||||||
if (currmap->script != NULL) {
|
if (currmap->script != NULL)
|
||||||
bb_error_msg("duplicate script in mapping \"%s\"", buf);
|
bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
|
||||||
return NULL;
|
currmap->script = xstrdup(next_word(&buf_ptr));
|
||||||
} else {
|
|
||||||
currmap->script = xstrdup(next_word(&buf_ptr));
|
|
||||||
}
|
|
||||||
} else if (strcmp(firstword, "map") == 0) {
|
} else if (strcmp(firstword, "map") == 0) {
|
||||||
if (currmap->max_mappings == currmap->n_mappings) {
|
if (currmap->n_mappings >= currmap->max_mappings) {
|
||||||
currmap->max_mappings = currmap->max_mappings * 2 + 1;
|
currmap->max_mappings = currmap->max_mappings * 2 + 1;
|
||||||
currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
|
currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
|
||||||
}
|
}
|
||||||
currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
|
currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
|
||||||
currmap->n_mappings++;
|
currmap->n_mappings++;
|
||||||
} else {
|
} else {
|
||||||
bb_error_msg("misplaced option \"%s\"", buf);
|
bb_error_msg_and_die("misplaced option \"%s\"", buf);
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
case NONE:
|
case NONE:
|
||||||
default:
|
default:
|
||||||
bb_error_msg("misplaced option \"%s\"", buf);
|
bb_error_msg_and_die("misplaced option \"%s\"", buf);
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(buf);
|
free(buf);
|
||||||
@ -1138,7 +1098,7 @@ static llist_t *read_iface_state(void)
|
|||||||
int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||||
int ifupdown_main(int argc, char **argv)
|
int ifupdown_main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int (*cmds)(struct interface_defn_t *) = NULL;
|
int (*cmds)(struct interface_defn_t *);
|
||||||
struct interfaces_file_t *defn;
|
struct interfaces_file_t *defn;
|
||||||
llist_t *target_list = NULL;
|
llist_t *target_list = NULL;
|
||||||
const char *interfaces = "/etc/network/interfaces";
|
const char *interfaces = "/etc/network/interfaces";
|
||||||
@ -1161,10 +1121,6 @@ int ifupdown_main(int argc, char **argv)
|
|||||||
defn = read_interfaces(interfaces);
|
defn = read_interfaces(interfaces);
|
||||||
debug_noise("\ndone reading %s\n\n", interfaces);
|
debug_noise("\ndone reading %s\n\n", interfaces);
|
||||||
|
|
||||||
if (!defn) {
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
startup_PATH = getenv("PATH");
|
startup_PATH = getenv("PATH");
|
||||||
if (!startup_PATH) startup_PATH = "";
|
if (!startup_PATH) startup_PATH = "";
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user