summaryrefslogtreecommitdiff
path: root/networking/ifupdown.c
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2003-01-14 23:26:57 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2003-01-14 23:26:57 +0000
commit8573704097440769c232f3edb6f347f016353dfb (patch)
tree3c35495b3ec8cfa50f7a384f6af5813fd47d4952 /networking/ifupdown.c
parent147a3ca1a40df4a97e9a12f18eadb537bea73b9a (diff)
downloadbusybox-w32-8573704097440769c232f3edb6f347f016353dfb.tar.gz
busybox-w32-8573704097440769c232f3edb6f347f016353dfb.tar.bz2
busybox-w32-8573704097440769c232f3edb6f347f016353dfb.zip
Fix leading spaces bug, free line buffer, redo next_word, save some
space.
Diffstat (limited to 'networking/ifupdown.c')
-rw-r--r--networking/ifupdown.c99
1 files changed, 43 insertions, 56 deletions
diff --git a/networking/ifupdown.c b/networking/ifupdown.c
index 665e527cc..be09ea6e7 100644
--- a/networking/ifupdown.c
+++ b/networking/ifupdown.c
@@ -604,27 +604,28 @@ address_family_t addr_inet = {
604 604
605#endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */ 605#endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
606 606
607static char *next_word(char *buf, char *word, int maxlen) 607static char *next_word(char **buf)
608{ 608{
609 if (!buf) 609 unsigned short length;
610 return NULL; 610 char *word;
611 if (!*buf)
612 return NULL;
613 611
614 while (!isspace(*buf) && *buf) { 612 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
615 if (maxlen-- > 1) 613 return NULL;
616 *word++ = *buf;
617 buf++;
618 }
619 if (maxlen > 0) {
620 *word = '\0';
621 } 614 }
622 615
623 while (isspace(*buf) && *buf) { 616 /* Skip over leading whitespace */
624 buf++; 617 word = *buf + strspn(*buf, " \t\n");
618
619 /* Find the length of this word */
620 length = strcspn(word, " \t\n");
621 if (length == 0) {
622 return(NULL);
625 } 623 }
624 *buf = word + length;
625 **buf = '\0';
626 (*buf)++;
626 627
627 return buf; 628 return word;
628} 629}
629 630
630static address_family_t *get_address_family(address_family_t *af[], char *name) 631static address_family_t *get_address_family(address_family_t *af[], char *name)
@@ -675,44 +676,34 @@ static const llist_t *find_list_string(const llist_t *list, const char *string)
675 676
676static interfaces_file_t *read_interfaces(char *filename) 677static interfaces_file_t *read_interfaces(char *filename)
677{ 678{
678 interface_defn_t *currif = NULL;
679 interfaces_file_t *defn;
680#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING 679#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
681 mapping_defn_t *currmap = NULL; 680 mapping_defn_t *currmap = NULL;
682#endif 681#endif
682 interface_defn_t *currif = NULL;
683 interfaces_file_t *defn;
683 FILE *f; 684 FILE *f;
684 char firstword[80]; 685 char *firstword;
685 char *buf = NULL; 686 char *buf;
686 char *rest;
687// int line;
688 687
689 enum { NONE, IFACE, MAPPING } currently_processing = NONE; 688 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
690 689
691 defn = xmalloc(sizeof(interfaces_file_t)); 690 defn = xmalloc(sizeof(interfaces_file_t));
692// defn->max_autointerfaces = defn->n_autointerfaces = 0;
693 defn->autointerfaces = NULL; 691 defn->autointerfaces = NULL;
694 defn->mappings = NULL; 692 defn->mappings = NULL;
695 defn->ifaces = NULL; 693 defn->ifaces = NULL;
696 f = fopen(filename, "r"); 694
697 if (f == NULL) { 695 f = xfopen(filename, "r");
698 return NULL;
699 }
700 696
701 while ((buf = get_line_from_file(f)) != NULL) { 697 while ((buf = get_line_from_file(f)) != NULL) {
702 char *end; 698 char *buf_ptr = buf;
703 699
700 /* Ignore comments */
704 if (buf[0] == '#') { 701 if (buf[0] == '#') {
705 continue; 702 continue;
706 } 703 }
707 end = last_char_is(buf, '\n'); 704
708 if (end) { 705 firstword = next_word(&buf_ptr);
709 *end = '\0'; 706 if (firstword == NULL) {
710 }
711 while ((end = last_char_is(buf, ' ')) != NULL) {
712 *end = '\0';
713 }
714 rest = next_word(buf, firstword, 80);
715 if (rest == NULL) {
716 continue; /* blank line */ 707 continue; /* blank line */
717 } 708 }
718 709
@@ -723,7 +714,7 @@ static interfaces_file_t *read_interfaces(char *filename)
723 currmap->n_matches = 0; 714 currmap->n_matches = 0;
724 currmap->match = NULL; 715 currmap->match = NULL;
725 716
726 while ((rest = next_word(rest, firstword, 80))) { 717 while ((firstword = next_word(&buf_ptr)) != NULL) {
727 if (currmap->max_matches == currmap->n_matches) { 718 if (currmap->max_matches == currmap->n_matches) {
728 currmap->max_matches = currmap->max_matches * 2 + 1; 719 currmap->max_matches = currmap->max_matches * 2 + 1;
729 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches); 720 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
@@ -747,9 +738,9 @@ static interfaces_file_t *read_interfaces(char *filename)
747 currently_processing = MAPPING; 738 currently_processing = MAPPING;
748 } else if (strcmp(firstword, "iface") == 0) { 739 } else if (strcmp(firstword, "iface") == 0) {
749 { 740 {
750 char iface_name[80]; 741 char *iface_name;
751 char address_family_name[80]; 742 char *address_family_name;
752 char method_name[80]; 743 char *method_name;
753 address_family_t *addr_fams[] = { 744 address_family_t *addr_fams[] = {
754#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 745#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
755 &addr_inet, 746 &addr_inet,
@@ -764,17 +755,16 @@ static interfaces_file_t *read_interfaces(char *filename)
764 }; 755 };
765 756
766 currif = xmalloc(sizeof(interface_defn_t)); 757 currif = xmalloc(sizeof(interface_defn_t));
758 iface_name = next_word(&buf_ptr);
759 address_family_name = next_word(&buf_ptr);
760 method_name = next_word(&buf_ptr);
767 761
768 rest = next_word(rest, iface_name, 80); 762 if (buf_ptr == NULL) {
769 rest = next_word(rest, address_family_name, 80);
770 rest = next_word(rest, method_name, 80);
771
772 if (rest == NULL) {
773 error_msg("too few parameters for line \"%s\"", buf); 763 error_msg("too few parameters for line \"%s\"", buf);
774 return NULL; 764 return NULL;
775 } 765 }
776 766
777 if (rest[0] != '\0') { 767 if (buf_ptr[0] != '\0') {
778 error_msg("too many parameters \"%s\"", buf); 768 error_msg("too many parameters \"%s\"", buf);
779 return NULL; 769 return NULL;
780 } 770 }
@@ -816,7 +806,7 @@ static interfaces_file_t *read_interfaces(char *filename)
816 } 806 }
817 currently_processing = IFACE; 807 currently_processing = IFACE;
818 } else if (strcmp(firstword, "auto") == 0) { 808 } else if (strcmp(firstword, "auto") == 0) {
819 while ((rest = next_word(rest, firstword, 80))) { 809 while ((firstword = next_word(&buf_ptr)) != NULL) {
820 810
821 /* Check the interface isnt already listed */ 811 /* Check the interface isnt already listed */
822 if (find_list_string(defn->autointerfaces, firstword)) { 812 if (find_list_string(defn->autointerfaces, firstword)) {
@@ -833,7 +823,7 @@ static interfaces_file_t *read_interfaces(char *filename)
833 { 823 {
834 int i; 824 int i;
835 825
836 if (xstrlen(rest) == 0) { 826 if (xstrlen(buf_ptr) == 0) {
837 error_msg("option with empty value \"%s\"", buf); 827 error_msg("option with empty value \"%s\"", buf);
838 return NULL; 828 return NULL;
839 } 829 }
@@ -858,7 +848,7 @@ static interfaces_file_t *read_interfaces(char *filename)
858 currif->option = opt; 848 currif->option = opt;
859 } 849 }
860 currif->option[currif->n_options].name = xstrdup(firstword); 850 currif->option[currif->n_options].name = xstrdup(firstword);
861 currif->option[currif->n_options].value = xstrdup(rest); 851 currif->option[currif->n_options].value = xstrdup(next_word(&buf_ptr));
862 if (!currif->option[currif->n_options].name) { 852 if (!currif->option[currif->n_options].name) {
863 perror(filename); 853 perror(filename);
864 return NULL; 854 return NULL;
@@ -876,14 +866,14 @@ static interfaces_file_t *read_interfaces(char *filename)
876 error_msg("duplicate script in mapping \"%s\"", buf); 866 error_msg("duplicate script in mapping \"%s\"", buf);
877 return NULL; 867 return NULL;
878 } else { 868 } else {
879 currmap->script = xstrdup(rest); 869 currmap->script = xstrdup(next_word(&buf_ptr));
880 } 870 }
881 } else if (strcmp(firstword, "map") == 0) { 871 } else if (strcmp(firstword, "map") == 0) {
882 if (currmap->max_mappings == currmap->n_mappings) { 872 if (currmap->max_mappings == currmap->n_mappings) {
883 currmap->max_mappings = currmap->max_mappings * 2 + 1; 873 currmap->max_mappings = currmap->max_mappings * 2 + 1;
884 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings); 874 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
885 } 875 }
886 currmap->mapping[currmap->n_mappings] = xstrdup(rest); 876 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
887 currmap->n_mappings++; 877 currmap->n_mappings++;
888 } else { 878 } else {
889 error_msg("misplaced option \"%s\"", buf); 879 error_msg("misplaced option \"%s\"", buf);
@@ -897,10 +887,10 @@ static interfaces_file_t *read_interfaces(char *filename)
897 return NULL; 887 return NULL;
898 } 888 }
899 } 889 }
890 free(buf);
900 } 891 }
901 if (ferror(f) != 0) { 892 if (ferror(f) != 0) {
902 perror_msg("%s", filename); 893 perror_msg_and_die("%s", filename);
903 return NULL;
904 } 894 }
905 fclose(f); 895 fclose(f);
906 896
@@ -1230,9 +1220,6 @@ extern int ifupdown_main(int argc, char **argv)
1230 } 1220 }
1231 1221
1232 defn = read_interfaces(interfaces); 1222 defn = read_interfaces(interfaces);
1233 if (!defn) {
1234 error_msg_and_die("couldn't read interfaces file \"%s\"", interfaces);
1235 }
1236 1223
1237 if (no_act) { 1224 if (no_act) {
1238 state_fp = fopen(statefile, "r"); 1225 state_fp = fopen(statefile, "r");