X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Ffileio.c;h=2b1dab805347c572ed386f52691dea203aa5a840;hp=337b9e41476d2c418c14453d2f3a9f578ef072d6;hb=68fee104e630eb19f04b8196a83c14c2c9c469e7;hpb=98f59e59e0c31ffcb953d3a7dba0da5e6f2f55f7 diff --git a/src/shared/fileio.c b/src/shared/fileio.c index 337b9e414..2b1dab805 100644 --- a/src/shared/fileio.c +++ b/src/shared/fileio.c @@ -24,16 +24,7 @@ #include "util.h" #include "strv.h" -int write_string_file(const char *fn, const char *line) { - _cleanup_fclose_ FILE *f = NULL; - - assert(fn); - assert(line); - - f = fopen(fn, "we"); - if (!f) - return -errno; - +int write_string_to_file(FILE *f, const char *line) { errno = 0; fputs(line, f); if (!endswith(line, "\n")) @@ -47,6 +38,19 @@ int write_string_file(const char *fn, const char *line) { return 0; } +int write_string_file(const char *fn, const char *line) { + _cleanup_fclose_ FILE *f = NULL; + + assert(fn); + assert(line); + + f = fopen(fn, "we"); + if (!f) + return -errno; + + return write_string_to_file(f, line); +} + int write_string_file_atomic(const char *fn, const char *line) { _cleanup_fclose_ FILE *f = NULL; _cleanup_free_ char *p = NULL; @@ -177,7 +181,7 @@ static int parse_env_file_internal( void *userdata) { _cleanup_free_ char *contents = NULL, *key = NULL; - size_t key_alloc = 0, n_key = 0, value_alloc = 0, n_value = 0, last_whitespace = (size_t) -1; + size_t key_alloc = 0, n_key = 0, value_alloc = 0, n_value = 0, last_value_whitespace = (size_t) -1, last_key_whitespace = (size_t) -1; char *p, *value = NULL; int r; @@ -212,6 +216,8 @@ static int parse_env_file_internal( state = COMMENT; else if (!strchr(WHITESPACE, c)) { state = KEY; + last_key_whitespace = (size_t) -1; + if (!greedy_realloc((void**) &key, &key_alloc, n_key+2)) { r = -ENOMEM; goto fail; @@ -225,9 +231,15 @@ static int parse_env_file_internal( if (strchr(newline, c)) { state = PRE_KEY; n_key = 0; - } else if (c == '=') + } else if (c == '=') { state = PRE_VALUE; - else { + last_value_whitespace = (size_t) -1; + } else { + if (!strchr(WHITESPACE, c)) + last_key_whitespace = (size_t) -1; + else if (last_key_whitespace == (size_t) -1) + last_key_whitespace = n_key; + if (!greedy_realloc((void**) &key, &key_alloc, n_key+2)) { r = -ENOMEM; goto fail; @@ -247,8 +259,8 @@ static int parse_env_file_internal( value[n_value] = 0; /* strip trailing whitespace from key */ - while(n_key && strchr(WHITESPACE, key[--n_key])) - key[n_key]=0; + if (last_key_whitespace != (size_t) -1) + key[last_key_whitespace] = 0; r = push(key, value, userdata); if (r < 0) @@ -257,6 +269,7 @@ static int parse_env_file_internal( n_key = 0; value = NULL; value_alloc = n_value = 0; + } else if (c == '\'') state = SINGLE_QUOTE_VALUE; else if (c == '\"') @@ -285,13 +298,13 @@ static int parse_env_file_internal( if (value) value[n_value] = 0; - /* Chomp off trailing whitespace */ - if (last_whitespace != (size_t) -1) - value[last_whitespace] = 0; + /* Chomp off trailing whitespace from value */ + if (last_value_whitespace != (size_t) -1) + value[last_value_whitespace] = 0; /* strip trailing whitespace from key */ - while(n_key && strchr(WHITESPACE, key[--n_key])) - key[n_key]=0; + if (last_key_whitespace != (size_t) -1) + key[last_key_whitespace] = 0; r = push(key, value, userdata); if (r < 0) @@ -300,14 +313,15 @@ static int parse_env_file_internal( n_key = 0; value = NULL; value_alloc = n_value = 0; + } else if (c == '\\') { state = VALUE_ESCAPE; - last_whitespace = (size_t) -1; + last_value_whitespace = (size_t) -1; } else { if (!strchr(WHITESPACE, c)) - last_whitespace = (size_t) -1; - else if (last_whitespace == (size_t) -1) - last_whitespace = n_value; + last_value_whitespace = (size_t) -1; + else if (last_value_whitespace == (size_t) -1) + last_value_whitespace = n_value; if (!greedy_realloc((void**) &value, &value_alloc, n_value+2)) { r = -ENOMEM; @@ -417,9 +431,13 @@ static int parse_env_file_internal( if (value) value[n_value] = 0; + if (state == VALUE) + if (last_value_whitespace != (size_t) -1) + value[last_value_whitespace] = 0; + /* strip trailing whitespace from key */ - while(n_key && strchr(WHITESPACE, key[--n_key])) - key[n_key]=0; + if (last_key_whitespace != (size_t) -1) + key[last_key_whitespace] = 0; r = push(key, value, userdata); if (r < 0) @@ -575,3 +593,32 @@ int write_env_file(const char *fname, char **l) { return r; } + +int executable_is_script(const char *path, char **interpreter) { + int r; + char _cleanup_free_ *line = NULL; + int len; + char *ans; + + assert(path); + + r = read_one_line_file(path, &line); + if (r < 0) + return r; + + if (!startswith(line, "#!")) + return 0; + + ans = strstrip(line + 2); + len = strcspn(ans, " \t"); + + if (len == 0) + return 0; + + ans = strndup(ans, len); + if (!ans) + return -ENOMEM; + + *interpreter = ans; + return 1; +}