Changeset 1584

Show
Ignore:
Timestamp:
03/04/10 16:40:56 (6 months ago)
Author:
marek
Message:

batctl: avoid parsing bat-hosts multiple times

Currently, running batctl from the home directory leads to warnings
("Warning - mac already known") if ~/bat-hosts exists. This is caused by
batctl parsing both "~/bat-hosts" and "bat-hosts" which happen to be the
same file when the working directory is ~

This patch adds duplicate file name detection to bat_hosts_init() to
avoid these warnings.

Signed-off-by: Daniel Seither <post@…>

Files:
1 modified

Legend:

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

    r1583 r1584  
    2424#include <stdio.h> 
    2525#include <stdint.h> 
     26#include <limits.h> 
    2627#include <stdlib.h> 
    2728#include <errno.h> 
     
    145146void bat_hosts_init(void) 
    146147{ 
    147         unsigned int i; 
     148        unsigned int i, j, parse; 
    148149        char confdir[CONF_DIR_LEN]; 
    149150        char *homedir; 
     151        size_t locations = sizeof(bat_hosts_path) / sizeof(char *); 
     152        char *normalized[locations]; 
    150153 
    151154        host_hash = hash_new(64, compare_mac, choose_mac); 
     
    158161        homedir = getenv("HOME"); 
    159162 
    160         for (i = 0; i < sizeof(bat_hosts_path) / sizeof(char *); i++) { 
     163        for (i = 0; i < locations; i++) { 
    161164                strcpy(confdir, ""); 
    162165 
     
    171174                } 
    172175 
    173                 parse_hosts_file(&host_hash, confdir); 
     176                normalized[i] = realpath(confdir, NULL); 
     177                if (normalized[i] == NULL) 
     178                        continue; 
     179 
     180                /* check for duplicates: don't parse the same file twice */ 
     181                parse = 1; 
     182                for (j = 0; j < i; j++) { 
     183                        if (normalized[j] == NULL) 
     184                                continue; 
     185 
     186                        if (strncmp(normalized[i], normalized[j], CONF_DIR_LEN) == 0) { 
     187                                parse = 0; 
     188                                break; 
     189                        } 
     190                } 
     191 
     192                if (parse && (normalized[i] != NULL)) 
     193                        parse_hosts_file(&host_hash, normalized[i]); 
     194        } 
     195 
     196        for (i = 0; i < locations; i++) { 
     197                if (normalized[i]) 
     198                        free(normalized[i]); 
    174199        } 
    175200}