[commits] r1625 - trunk/batctl

postmaster at open-mesh.net postmaster at open-mesh.net
Tue Apr 6 07:37:17 CEST 2010


Author: marek
Date: 2010-04-06 07:36:59 +0200 (Tue, 06 Apr 2010)
New Revision: 1625

Modified:
   trunk/batctl/functions.c
   trunk/batctl/functions.h
   trunk/batctl/sys.c
Log:
batctl: Parse allowed settings for sysfs without whitespace before newline

The current settings parser assumes that a newline is at the end of each
line starting with "commands:". Luis de Bethencourt reported that this
is a bad coding style and should be avoided.

To parse the new settings lines we must also check for \n and not only
for single whitespaces. After each check it must be ensured that the old
character is placed again at the old place and the line seems to be
unmodified to the user.

For old versions with extra whitespace before a newline an extra check
must be applied to not allow commands with empty parameter like in
 batctl vis_mode ''

Signed-off-by: Sven Eckelmann <sven.eckelmann at gmx.de>

Modified: trunk/batctl/functions.c
===================================================================
--- trunk/batctl/functions.c	2010-04-06 05:36:53 UTC (rev 1624)
+++ trunk/batctl/functions.c	2010-04-06 05:36:59 UTC (rev 1625)
@@ -311,3 +311,21 @@
 		close(fd);
 	return res;
 }
+
+char *strchr_anyof(const char *s, const char *n)
+{
+	char *cur, *first = NULL;
+	size_t i, len;
+
+	if (s == NULL || n == NULL)
+		return first;
+
+	len = strlen(n);
+	for (i = 0; i < len; i++) {
+		cur = strchr(s, n[i]);
+		if (cur != NULL && (cur < first || first == NULL))
+			first = cur;
+	}
+
+	return first;
+}

Modified: trunk/batctl/functions.h
===================================================================
--- trunk/batctl/functions.h	2010-04-06 05:36:53 UTC (rev 1624)
+++ trunk/batctl/functions.h	2010-04-06 05:36:59 UTC (rev 1625)
@@ -37,6 +37,7 @@
 int read_file(char *dir, char *path, int read_opt);
 int write_file(char *dir, char *fname, char *arg1, char *arg2);
 int check_proc_dir(char *dir);
+char *strchr_anyof(const char *s, const char *n);
 
 extern char *line_ptr;
 

Modified: trunk/batctl/sys.c
===================================================================
--- trunk/batctl/sys.c	2010-04-06 05:36:53 UTC (rev 1624)
+++ trunk/batctl/sys.c	2010-04-06 05:36:59 UTC (rev 1625)
@@ -340,6 +340,7 @@
 int handle_sys_setting(int argc, char **argv, char *file_path, void setting_usage(void))
 {
 	int optchar, res;
+	char space_char;
 	char *space_ptr, *comma_char, *cmds = NULL;
 
 	while ((optchar = getopt(argc, argv, "h")) != -1) {
@@ -360,15 +361,19 @@
 	if (res != EXIT_SUCCESS)
 		return res;
 
-	while ((space_ptr = strchr(line_ptr, ' ')) != NULL) {
+	while ((space_ptr = strchr_anyof(line_ptr, " \n")) != NULL) {
+		space_char = *space_ptr;
 		*space_ptr = '\0';
+		comma_char = NULL;
 
 		if (strncmp(line_ptr, SEARCH_ARGS_TAG, strlen(SEARCH_ARGS_TAG)) == 0) {
 			cmds = space_ptr + 1;
 			goto next;
 		}
 
-		comma_char = NULL;
+		if (strlen(line_ptr) == 0)
+			goto next;
+
 		if (line_ptr[strlen(line_ptr) - 1] == ',') {
 			comma_char = line_ptr + strlen(line_ptr) - 1;
 			*comma_char = '\0';
@@ -377,11 +382,11 @@
 		if (strcmp(line_ptr, argv[1]) == 0)
 			goto write_file;
 
-		*space_ptr = ' ';
+next:
+		*space_ptr = space_char;
 		if (comma_char)
 			*comma_char = ',';
 
-next:
 		line_ptr = space_ptr + 1;
 	}
 



More information about the commits mailing list