Author: marek Date: 2010-04-07 13:15:34 +0200 (Wed, 07 Apr 2010) New Revision: 1630
Modified: branches/batctl-0.2.x/functions.c branches/batctl-0.2.x/functions.h branches/batctl-0.2.x/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@gmx.de
Modified: branches/batctl-0.2.x/functions.c =================================================================== --- branches/batctl-0.2.x/functions.c 2010-04-07 11:15:28 UTC (rev 1629) +++ branches/batctl-0.2.x/functions.c 2010-04-07 11:15:34 UTC (rev 1630) @@ -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: branches/batctl-0.2.x/functions.h =================================================================== --- branches/batctl-0.2.x/functions.h 2010-04-07 11:15:28 UTC (rev 1629) +++ branches/batctl-0.2.x/functions.h 2010-04-07 11:15:34 UTC (rev 1630) @@ -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: branches/batctl-0.2.x/sys.c =================================================================== --- branches/batctl-0.2.x/sys.c 2010-04-07 11:15:28 UTC (rev 1629) +++ branches/batctl-0.2.x/sys.c 2010-04-07 11:15:34 UTC (rev 1630) @@ -317,6 +317,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) { @@ -337,15 +338,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'; @@ -354,11 +359,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; }