c - Parse network 'interfaces' for read/write using pointer as parameter of functions -
i write small program read /etc/network/interfaces, save *ethernet_t[], change value, , write interfaces. works fine in main function. can not separate code function , pass pointer of array that. following code:
#include <stdio.h> #include <ctype.h> typedef struct { char sys_ifname[10]; char address[20]; char netmask[20]; char network[20]; char gateway[20]; protocol_e protocol; } ethernet_t; #define max_nic 4 char *removing_leading_and_trailing_whitespace(char *a); int write_to_interface (ethernet_t *interface[], const char *filename) { file *fp; /* should grant root privilege */ if ((fp = fopen(filename, "w")) == null) { perror ("fopen"); exit (-1); } printf ("file open %s writing successfully\n", filename); /* set dhcp test */ interface[2]->protocol = dhcp; fprintf (fp, "# /etc/network/interface\n"); fprintf (fp, "\nauto lo\n"); fprintf (fp, "iface lo inet loopback\n"); int i; (i=0; i<max_nic; i++) { printf ("\nauto %s\n", interface[i]->sys_ifname); fprintf (fp, "\nauto %s\n", interface[i]->sys_ifname); fprintf (fp, "iface %s inet %s\n", interface[i]->sys_ifname, interface[i]->protocol == dhcp ? "dhcp" : "static"); if (interface[i]->protocol == static) { fprintf (fp, "\taddress %s\n", interface[i]->address); fprintf (fp, "\tnetmaks %s\n", interface[i]->netmask); fprintf (fp, "\tnetwork %s\n", interface[i]->network); fprintf (fp, "\tgateway %s\n", interface[i]->gateway); } } fclose(fp); printf ("done!\n"); return 0; } int main(void) { int i; char line[256]; char *p; file *fp; ethernet_t interface[max_nic]; ethernet_t *current_interface = null; bzero(interface, sizeof(ethernet_t) * max_nic); #if yocto const char *filename = "/etc/network/interfaces"; #else const char *filename = "/etc/network/interfaces.ubuntu"; #endif if ((fp = fopen(filename, "r")) == null) { perror ("fopen"); exit (-1); } = 0; current_interface = interface; while (fgets(line, sizeof(line), fp) && <= max_nic) { p = removing_leading_and_trailing_whitespace(line); if (strlen(p) && p[0] != '#') { /* scan interface name */ if (strstr(p, "auto")) { p = removing_leading_and_trailing_whitespace(p + strlen("auto")); if (strcmp(p, "lo") != 0) { /* interface lo loopback */ current_interface = interface + i; /* interface default static */ current_interface->protocol = static; strcpy (current_interface->sys_ifname, p); i++; } } else if (strstr(p, "dhcp") && current_interface) { current_interface->protocol = dhcp; } else if (strstr(p, "address") && current_interface) { p = removing_leading_and_trailing_whitespace(p + strlen("address")); memcpy (current_interface->address, p, strlen(p)); } else if (strstr(p, "netmask") && current_interface) { p = removing_leading_and_trailing_whitespace(p + strlen("netmask")); memcpy (current_interface->netmask, p, strlen(p)); } else if (strstr(p, "network") && current_interface) { p = removing_leading_and_trailing_whitespace(p + strlen("network")); memcpy (current_interface->network, p, strlen(p)); } else if (strstr(p, "gateway") && current_interface) { p = removing_leading_and_trailing_whitespace(p + strlen("gateway")); memcpy (current_interface->gateway, p, strlen(p)); } } /* if (strlen(p) && p[0] != '#') */ } /* while */ fclose (fp); #if 1 write_to_interface ((ethernet_t **)interface, filename); #else /* should grant root privilege */ if ((fp = fopen(filename, "w")) == null) { perror ("fopen"); exit (-1); } printf ("file open %s writing successfully\n", filename); interface[2].protocol = dhcp; fprintf (fp, "# /etc/network/interface\n"); fprintf (fp, "# auto generated vicom\n"); fprintf (fp, "\nauto lo\n"); fprintf (fp, "iface lo inet loopback\n"); (i=0; i<max_nic; i++) { fprintf (fp, "\nauto %s\n", interface[i].sys_ifname); fprintf (fp, "iface %s inet %s\n", interface[i].sys_ifname, interface[i].protocol == dhcp ? "dhcp" : "static"); if (interface[i].protocol == static) { fprintf (fp, "\taddress %s\n", interface[i].address); fprintf (fp, "\tnetmaks %s\n", interface[i].netmask); fprintf (fp, "\tnetwork %s\n", interface[i].network); fprintf (fp, "\tgateway %s\n", interface[i].gateway); } } fclose(fp); printf ("done!\n"); #endif return 0; }
when run in linux, got
file open /etc/network/interfaces.ubuntu writing segmentation fault (core dumped)
my question how dereference pointer of array in function properly?
thank you, some-programmer-dude , storyteller. follow hint, rewrite code bellow:
int write_to_interface (ethernet_t *interface, const char *filename) { file *fp; /* should grant root privilege */ if ((fp = fopen(filename, "w")) == null) { perror ("fopen"); exit (-1); } printf ("file open %s writing successfully\n", filename); #if 0 (interface + 2)->protocol = dhcp; #endif fprintf (fp, "# /etc/network/interface\n"); fprintf (fp, "\nauto lo\n"); fprintf (fp, "iface lo inet loopback\n"); int i; (i=0; i<max_nic; i++) { fprintf (fp, "\nauto %s\n", (interface+i)->sys_ifname); fprintf (fp, "\nauto %s\n", (interface+i)->sys_ifname); fprintf (fp, "iface %s inet %s\n", (interface+i)->sys_ifname, (interface+i)->protocol == dhcp ? "dhcp" : "static"); if ((interface+i)->protocol == static) { fprintf (fp, "\taddress %s\n", (interface+i)->address); fprintf (fp, "\tnetmaks %s\n", (interface+i)->netmask); fprintf (fp, "\tnetwork %s\n", (interface+i)->network); fprintf (fp, "\tgateway %s\n", (interface+i)->gateway); } } fclose(fp); printf ("done!\n"); return 0; }
thank both! save life.
Comments
Post a Comment