[systemd-devel] [PATCH 1/2] continuation support for load_env_file

Michal Vyskocil mvyskocil at suse.cz
Fri Jan 18 01:05:10 PST 2013


Variable definition can be written on more than one line - if each ends
with a backslash, then is concatenated with a previous one. Only
backslash and unix end of line (\n) are treated as a continuation. And
backslash on the last line of the file is ignored.

Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=58083
---
 src/shared/util.c |   43 ++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/src/shared/util.c b/src/shared/util.c
index f75a81c..08c0c2b 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -780,6 +780,8 @@ int load_env_file(
                 char ***rl) {
 
         FILE *f;
+        char *b;
+        char *c = NULL;
         char **m = NULL;
         int r;
 
@@ -790,18 +792,45 @@ int load_env_file(
                 return -errno;
 
         while (!feof(f)) {
-                char l[LINE_MAX], *p, *u;
+                char l[LINE_MAX], *p, *u, *cs;
                 char **t;
 
                 if (!fgets(l, sizeof(l), f)) {
-                        if (feof(f))
+                        if(!feof(f)) {
+                                r = -errno;
+                                goto finish;
+                        }
+                        else if (!c)
                                 break;
 
-                        r = -errno;
-                        goto finish;
                 }
 
-                p = strstrip(l);
+                cs = endswith(l, "\\\n");
+                if (cs) {
+
+                        *cs = '\0';
+                        b = strappend(c, l);
+                        if (!b) {
+                               r = log_oom();
+                                goto finish;
+                        }
+                        free(c);
+                        c = b;
+                        *l = '\0';
+                        continue;
+                }
+
+                if (c) {
+                        b = strappend(c, l);
+                        if (!b) {
+                                r = log_oom();
+                                goto finish;
+                        }
+                        free(c);
+                        c = b;
+                }
+
+                p = strstrip(c ? c : l);
 
                 if (!*p)
                         continue;
@@ -813,6 +842,8 @@ int load_env_file(
                         r = log_oom();
                         goto finish;
                 }
+                free(c);
+                c = NULL;
 
                 t = strv_append(m, u);
                 free(u);
@@ -835,6 +866,8 @@ finish:
         if (f)
                 fclose(f);
 
+        free(c);
+
         strv_free(m);
 
         return r;
-- 
1.7.7

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/systemd-devel/attachments/20130118/f1fbfd54/attachment.pgp>


More information about the systemd-devel mailing list