]> git.itanic.dy.fi Git - log-plotter/commitdiff
config.c: Handle multiple variables correcly
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Thu, 16 Jan 2014 20:11:47 +0000 (22:11 +0200)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Thu, 16 Jan 2014 20:12:03 +0000 (22:12 +0200)
If we happen to have multiple variables in the string we are handling,
we must continue handling the string always at the end of the previous
replaced variable. Otherwise we may end up skipping the beginning of
the next variable.

Signed-off-by: Timo Kokkonen <timo.t.kokkonen@iki.fi>
config.c

index 1dcc9c8..8b84d58 100644 (file)
--- a/config.c
+++ b/config.c
@@ -247,7 +247,7 @@ static char *get_value_for_variable(char *variable,
 
 static int replace_variable_with_value(char *str, size_t len,
                                char *variable_start, char *variable_end,
-                               const char *value)
+                               const char *value, char **endptr)
 {
        int value_len = strlen(value);
        char *from, *to;
@@ -265,6 +265,8 @@ static int replace_variable_with_value(char *str, size_t len,
                from = variable_end + 1;
                to = variable_start + value_len;
 
+               *endptr = to;
+
                while (*from) {
                        *to = *from;
                        from++;
@@ -301,6 +303,8 @@ static int replace_variable_with_value(char *str, size_t len,
                        to--;
                }
 
+               *endptr = from;
+
                /*
                 * use memcpy to avoid NULL terminating prematurely
                 * the target string
@@ -343,7 +347,6 @@ int replace_variables_with_values(char *str, size_t len,
 
                variable_end = ptr;
                *ptr = 0;
-               ptr++;
 
                value = get_value_for_variable(variable_start, cfg);
                if (!value) {
@@ -358,7 +361,7 @@ int replace_variables_with_values(char *str, size_t len,
                        continue;
                }
                overflow = replace_variable_with_value(str, len, replace_start,
-                                       variable_end, value);
+                                       variable_end, value, &ptr);
        }
 
        if (overflow)