Changeset 1566

Show
Ignore:
Timestamp:
01/25/10 10:25:47 (6 months ago)
Author:
marek
Message:

batctl: extend bat-hosts parser to ignore empty lines and comments

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/batctl-0.2.x/bat-hosts.c

    r1463 r1566  
    1 /*  
     1/* 
    22 * Copyright (C) 2007-2009 B.A.T.M.A.N. contributors: 
    33 * 
     
    6464{ 
    6565        FILE *fd; 
     66        char *line_ptr = NULL; 
    6667        char name[HOST_NAME_MAX_LEN], mac_str[18]; 
    6768        struct ether_addr *mac_addr; 
    6869        struct bat_host *bat_host; 
    6970        struct hashtable_t *swaphash; 
     71        size_t len = 0; 
    7072 
    7173        name[0] = mac_str[0] = '\0'; 
     
    7577                return; 
    7678 
    77         while (fscanf(fd,"%[^ \t]%s\n", mac_str, name) != EOF) { 
     79        while (getline(&line_ptr, &len, fd) != -1) { 
     80                /* ignore empty lines and comments */ 
     81                if ((line_ptr[0] == '\n') || (line_ptr[0] == '#')) 
     82                        continue; 
     83 
     84                if (sscanf(line_ptr, "%[^ \t]%s\n", mac_str, name) != 2) { 
     85                        fprintf(stderr, "Warning - unrecognized bat-host definition: %s", line_ptr); 
     86                        continue; 
     87                } 
    7888 
    7989                mac_addr = ether_aton(mac_str); 
    8090                if (!mac_addr) { 
    81                         printf("Warning - invalid mac address in '%s' detected: %s\n", path, mac_str); 
     91                        fprintf(stderr, "Warning - invalid mac address in '%s' detected: %s\n", path, mac_str); 
    8292                        continue; 
    8393                } 
     
    8797                /* mac entry already exists - we found a new name for it */ 
    8898                if (bat_host) { 
    89                         printf("Warning - mac already known (changing name from '%s' to '%s'): %s\n", 
     99                        fprintf(stderr, "Warning - mac already known (changing name from '%s' to '%s'): %s\n", 
    90100                                        bat_host->name, name, mac_str); 
    91101                        strncpy(bat_host->name, name, HOST_NAME_MAX_LEN - 1); 
     
    97107                /* name entry already exists - we found a new mac address for it */ 
    98108                if (bat_host) { 
    99                         printf("Warning - name already known (changing mac from '%s' to '%s'): %s\n", 
     109                        fprintf(stderr, "Warning - name already known (changing mac from '%s' to '%s'): %s\n", 
    100110                                        ether_ntoa(&bat_host->mac_addr), mac_str, name); 
    101111                        hash_remove(*hash, bat_host); 
     
    106116 
    107117                if (!bat_host) { 
    108                         printf("Error - could not allocate memory: %s\n", strerror(errno)); 
     118                        fprintf(stderr, "Error - could not allocate memory: %s\n", strerror(errno)); 
    109119                        goto out; 
    110120                } 
     
    119129 
    120130                        if (swaphash == NULL) 
    121                                 printf("Warning - couldn't resize bat hosts hash table\n"); 
     131                                fprintf(stderr, "Warning - couldn't resize bat hosts hash table\n"); 
    122132                        else 
    123133                                *hash = swaphash; 
    124134                } 
    125  
    126135        } 
    127136 
     
    129138        if (fd) 
    130139                fclose(fd); 
     140        if (line_ptr) 
     141                free(line_ptr); 
    131142        return; 
    132143}