[systemd-commits] src/shared src/test

Harald Hoyer harald at kemper.freedesktop.org
Wed Apr 17 02:08:06 PDT 2013


 src/shared/fileio.c    |    6 ++++--
 src/test/test-fileio.c |   20 +++++++++++++++-----
 2 files changed, 19 insertions(+), 7 deletions(-)

New commits:
commit db5372091664c977a937f6bc0fe4484363be0669
Author: Harald Hoyer <harald at redhat.com>
Date:   Wed Apr 17 11:02:56 2013 +0200

    fileio:parse_env_file_internal() fix environment file parsing
    
    parse_env_file_internal() could not parse the following lines correctly:
    
    export key="val"
    key="val"#comment

diff --git a/src/shared/fileio.c b/src/shared/fileio.c
index 617afea..3f242ed 100644
--- a/src/shared/fileio.c
+++ b/src/shared/fileio.c
@@ -209,7 +209,9 @@ static int parse_env_file_internal(
                 switch (state) {
 
                 case PRE_KEY:
-                        if (strchr(COMMENTS, c))
+                        if (startswith(p, "export "))
+                                p+=6;
+                        else if (strchr(COMMENTS, c))
                                 state = COMMENT;
                         else if (!strchr(WHITESPACE, c)) {
                                 state = KEY;
@@ -255,7 +257,7 @@ static int parse_env_file_internal(
                         break;
 
                 case PRE_VALUE:
-                        if (strchr(newline, c)) {
+                        if (strchr(newline, c) || strchr(COMMENTS, c)) {
                                 state = PRE_KEY;
                                 key[n_key] = 0;
 
diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c
index 7adf2ef..994b89b 100644
--- a/src/test/test-fileio.c
+++ b/src/test/test-fileio.c
@@ -31,7 +31,7 @@ static void test_parse_env_file(void) {
         char t[] = "/tmp/test-parse-env-file-XXXXXX";
         int fd, r;
         FILE *f;
-        _cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL, *six = NULL, *seven = NULL;
+        _cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL, *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL;
         _cleanup_strv_free_ char **a = NULL, **b = NULL;
         char **i;
         unsigned k;
@@ -53,7 +53,9 @@ static void test_parse_env_file(void) {
               "five = \'55\\\'55\' \"FIVE\" cinco   \n"
               "six = seis sechs\\\n"
               " sis\n"
-              "seven=", f);
+              "export seven=\"sevenval\"#comment\n"
+              "eight=#comment\n"
+              "nine=", f);
 
         fflush(f);
         fclose(f);
@@ -67,6 +69,8 @@ static void test_parse_env_file(void) {
                        "five", &five,
                        "six", &six,
                        "seven", &seven,
+                       "eight", &eight,
+                       "nine", &nine,
                        NULL);
 
         assert_se(r >= 0);
@@ -78,6 +82,8 @@ static void test_parse_env_file(void) {
         log_info("five=[%s]", strna(five));
         log_info("six=[%s]", strna(six));
         log_info("seven=[%s]", strna(seven));
+        log_info("eight=[%s]", strna(eight));
+        log_info("nine=[%s]", strna(nine));
 
         assert_se(streq(one, "BAR"));
         assert_se(streq(two, "bar"));
@@ -85,7 +91,9 @@ static void test_parse_env_file(void) {
         assert_se(streq(four, "44\"44"));
         assert_se(streq(five, "55\'55FIVEcinco"));
         assert_se(streq(six, "seis sechs sis"));
-        assert_se(seven == NULL);
+        assert_se(streq(seven, "sevenval"));
+        assert_se(eight == NULL);
+        assert_se(nine == NULL);
 
         r = load_env_file(t, NULL, &a);
         assert_se(r >= 0);
@@ -99,8 +107,10 @@ static void test_parse_env_file(void) {
         assert_se(streq(a[3], "four=44\"44"));
         assert_se(streq(a[4], "five=55\'55FIVEcinco"));
         assert_se(streq(a[5], "six=seis sechs sis"));
-        assert_se(streq(a[6], "seven="));
-        assert_se(a[7] == NULL);
+        assert_se(streq(a[6], "seven=sevenval"));
+        assert_se(streq(a[7], "eight="));
+        assert_se(streq(a[8], "nine="));
+        assert_se(a[9] == NULL);
 
         r = write_env_file("/tmp/test-fileio", a);
         assert_se(r >= 0);



More information about the systemd-commits mailing list