chiark / gitweb /
nspawn: create empty /etc/resolv.conf if necessary
[elogind.git] / src / shared / fileio.c
index 96e23c5bbb69c314bdaa65c7ebdef98b81a2780a..337b9e41476d2c418c14453d2f3a9f578ef072d6 100644 (file)
@@ -35,9 +35,7 @@ int write_string_file(const char *fn, const char *line) {
                 return -errno;
 
         errno = 0;
-        if (fputs(line, f) < 0)
-                return errno ? -errno : -EIO;
-
+        fputs(line, f);
         if (!endswith(line, "\n"))
                 fputc('\n', f);
 
@@ -64,11 +62,7 @@ int write_string_file_atomic(const char *fn, const char *line) {
         fchmod_umask(fileno(f), 0644);
 
         errno = 0;
-        if (fputs(line, f) < 0) {
-                r = -errno;
-                goto finish;
-        }
-
+        fputs(line, f);
         if (!endswith(line, "\n"))
                 fputc('\n', f);
 
@@ -83,7 +77,6 @@ int write_string_file_atomic(const char *fn, const char *line) {
                         r = 0;
         }
 
-finish:
         if (r < 0)
                 unlink(p);
 
@@ -191,7 +184,6 @@ static int parse_env_file_internal(
         enum {
                 PRE_KEY,
                 KEY,
-                PRE_EQUAL,
                 PRE_VALUE,
                 VALUE,
                 VALUE_ESCAPE,
@@ -233,9 +225,7 @@ static int parse_env_file_internal(
                         if (strchr(newline, c)) {
                                 state = PRE_KEY;
                                 n_key = 0;
-                        } else if (strchr(WHITESPACE, c))
-                                state = PRE_EQUAL;
-                        else if (c == '=')
+                        } else if (c == '=')
                                 state = PRE_VALUE;
                         else {
                                 if (!greedy_realloc((void**) &key, &key_alloc, n_key+2)) {
@@ -248,19 +238,6 @@ static int parse_env_file_internal(
 
                         break;
 
-                case PRE_EQUAL:
-                        if (strchr(newline, c)) {
-                                state = PRE_KEY;
-                                n_key = 0;
-                        } else if (c == '=')
-                                state = PRE_VALUE;
-                        else if (!strchr(WHITESPACE, c)) {
-                                n_key = 0;
-                                state = COMMENT;
-                        }
-
-                        break;
-
                 case PRE_VALUE:
                         if (strchr(newline, c)) {
                                 state = PRE_KEY;
@@ -269,6 +246,10 @@ static int parse_env_file_internal(
                                 if (value)
                                         value[n_value] = 0;
 
+                                /* strip trailing whitespace from key */
+                                while(n_key && strchr(WHITESPACE, key[--n_key]))
+                                        key[n_key]=0;
+
                                 r = push(key, value, userdata);
                                 if (r < 0)
                                         goto fail;
@@ -298,6 +279,7 @@ static int parse_env_file_internal(
                 case VALUE:
                         if (strchr(newline, c)) {
                                 state = PRE_KEY;
+
                                 key[n_key] = 0;
 
                                 if (value)
@@ -307,6 +289,10 @@ static int parse_env_file_internal(
                                 if (last_whitespace != (size_t) -1)
                                         value[last_whitespace] = 0;
 
+                                /* strip trailing whitespace from key */
+                                while(n_key && strchr(WHITESPACE, key[--n_key]))
+                                        key[n_key]=0;
+
                                 r = push(key, value, userdata);
                                 if (r < 0)
                                         goto fail;
@@ -431,6 +417,10 @@ static int parse_env_file_internal(
                 if (value)
                         value[n_value] = 0;
 
+                /* strip trailing whitespace from key */
+                while(n_key && strchr(WHITESPACE, key[--n_key]))
+                        key[n_key]=0;
+
                 r = push(key, value, userdata);
                 if (r < 0)
                         goto fail;
@@ -522,10 +512,41 @@ int load_env_file(const char *fname, const char *newline, char ***rl) {
         return 0;
 }
 
+static void write_env_var(FILE *f, const char *v) {
+        const char *p;
+
+        p = strchr(v, '=');
+        if (!p) {
+                /* Fallback */
+                fputs(v, f);
+                fputc('\n', f);
+                return;
+        }
+
+        p++;
+        fwrite(v, 1, p-v, f);
+
+        if (string_has_cc(p) || chars_intersect(p, WHITESPACE "\'\"\\`$")) {
+                fputc('\"', f);
+
+                for (; *p; p++) {
+                        if (strchr("\'\"\\`$", *p))
+                                fputc('\\', f);
+
+                        fputc(*p, f);
+                }
+
+                fputc('\"', f);
+        } else
+                fputs(p, f);
+
+        fputc('\n', f);
+}
+
 int write_env_file(const char *fname, char **l) {
         char **i;
-        char _cleanup_free_ *p = NULL;
-        FILE _cleanup_fclose_ *f = NULL;
+        _cleanup_free_ char *p = NULL;
+        _cleanup_fclose_ FILE *f = NULL;
         int r;
 
         r = fopen_temporary(fname, &f, &p);
@@ -535,19 +556,14 @@ int write_env_file(const char *fname, char **l) {
         fchmod_umask(fileno(f), 0644);
 
         errno = 0;
-        STRV_FOREACH(i, l) {
-                fputs(*i, f);
-                fputc('\n', f);
-        }
+        STRV_FOREACH(i, l)
+                write_env_var(f, *i);
 
         fflush(f);
 
-        if (ferror(f)) {
-                if (errno > 0)
-                        r = -errno;
-                else
-                        r = -EIO;
-        } else {
+        if (ferror(f))
+                r = errno ? -errno : -EIO;
+        else {
                 if (rename(p, fname) < 0)
                         r = -errno;
                 else