2017-02-07 8 views
0

*/etc/network/interfacesを読み、* ethernet_t []に保存し、値を変更してインタフェースに書き戻すための小さなプログラムを書く。主な機能はすべて正常に動作します。しかし、関数としてコードを分けることはできず、配列のポインタを渡すことはできません。以下の私のコードです:関数のパラメータとしてポインタを使用して読み書きするネットワークを解析する

#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 for writing successfully\n", filename); 

    /* set DHCP for 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; 
    for (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); 
    } 

    i = 0; 
    current_interface = interface; 

    while (fgets(line, sizeof(line), fp) && i <= MAX_NIC) { 
    p = removing_leading_and_trailing_whitespace(line); 
    if (strlen(p) && p[0] != '#') { 
     /* scan for interface name */ 
     if (strstr(p, "auto")) { 
      p = removing_leading_and_trailing_whitespace(p + strlen("auto")); 
      if (strcmp(p, "lo") != 0) { /* interface lo is 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 for writing successfully\n", filename); 

    interface[2].protocol = DHCP; 

    fprintf (fp, "# /etc/network/interface\n"); 
    fprintf (fp, "# was auto generated by Vicom\n"); 

    fprintf (fp, "\nauto lo\n"); 
    fprintf (fp, "iface lo inet loopback\n"); 

    for (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; 
} 

私はLinux上でそれを実行すると、私は私の質問が適切に機能してどのように配列のポインタデリファレンスです

File open /etc/network/interfaces.ubuntu for writing successfully Segmentation fault (core dumped)

を得ましたか。

+3

しかし、 'interface'は構造体の配列であり、構造体へのポインタの配列ではありませんか?では、なぜそれをポインタの配列として渡そうとしていますか?それは意味をなさない。 –

+1

あなたの関数は単に 'ethernet_t * interface'とsizeパラメータを受け入れます。 – StoryTeller

+0

ありがとう、私はできるだけ早く試してみます。 –

答えて

0

ありがとう、some-programmer-dudeおよびStoryTeller あなたのヒントに従ってください、私は怒鳴るように、コードを書き換える:

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 for 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; 
    for (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; 
} 

はあなたの両方をありがとう!あなたは私の人生を救う。

関連する問題