public inbox for b.a.t.m.a.n@lists.open-mesh.org
 help / color / mirror / Atom feed
From: Daniel Seither <post@tiwoc.de>
To: The list for a Better Approach To Mobile Ad-hoc Networking
	<b.a.t.m.a.n@lists.open-mesh.org>
Subject: [B.A.T.M.A.N.] [PATCH] batctl: avoid parsing bat-hosts multiple times
Date: Thu, 04 Mar 2010 13:23:21 +0100	[thread overview]
Message-ID: <4B8FA639.5000502@tiwoc.de> (raw)

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@tiwoc.de>
---
Index: batctl/bat-hosts.c
===================================================================
--- batctl/bat-hosts.c	(revision 1579)
+++ batctl/bat-hosts.c	(working copy)
@@ -23,6 +23,7 @@

 #include <stdio.h>
 #include <stdint.h>
+#include <limits.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <string.h>
@@ -144,9 +145,11 @@

 void bat_hosts_init(void)
 {
-	unsigned int i;
+	unsigned int i, j, parse;
 	char confdir[CONF_DIR_LEN];
 	char *homedir;
+	size_t locations = sizeof(bat_hosts_path) / sizeof(char *);
+	char normalized[locations][PATH_MAX];

 	host_hash = hash_new(64, compare_mac, choose_mac);

@@ -157,7 +160,7 @@

 	homedir = getenv("HOME");

-	for (i = 0; i < sizeof(bat_hosts_path) / sizeof(char *); i++) {
+	for (i = 0; i < locations; i++) {
 		strcpy(confdir, "");

 		if (strlen(bat_hosts_path[i]) >= 2
@@ -169,8 +172,22 @@
 			strncpy(confdir, bat_hosts_path[i], CONF_DIR_LEN);
 			confdir[CONF_DIR_LEN - 1] = '\0';
 		}
-
-		parse_hosts_file(&host_hash, confdir);
+		
+		if (realpath(confdir, normalized[i]) == NULL) {
+			normalized[i][0] = '\0';
+			continue;
+		}
+		
+		/* check for duplicates: don't parse the same file twice */	
+		parse = 1;
+		for (j = 0; j < i; j++)
+			if (strncmp(normalized[i], normalized[j], CONF_DIR_LEN) == 0) {
+				parse = 0;
+				break;
+			}
+	
+		if (parse && (normalized[i][0] != '\0'))
+			parse_hosts_file(&host_hash, normalized[i]);
 	}
 }


             reply	other threads:[~2010-03-04 12:23 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-04 12:23 Daniel Seither [this message]
2010-03-04 12:49 ` [B.A.T.M.A.N.] [PATCH] batctl: avoid parsing bat-hosts multiple times Marek Lindner
2010-03-04 13:48   ` Daniel Seither

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4B8FA639.5000502@tiwoc.de \
    --to=post@tiwoc.de \
    --cc=b.a.t.m.a.n@lists.open-mesh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox