[systemd-devel] [PATCH] fileio: make parse_env_file() return number of parsed items

Michal Sekletar msekleta at redhat.com
Thu Jun 19 09:58:32 PDT 2014


On Mon, Jun 02, 2014 at 06:03:27PM +0200, Michal Sekletar wrote:
> This should make logic in locale_setup() work as intended, hence don't
> parse /etc/locale.conf if admin passed to us explicit locale settings on
> kernel cmdline.
> ---
>  src/shared/fileio.c | 28 ++++++++++++++++++----------
>  1 file changed, 18 insertions(+), 10 deletions(-)
> 
> diff --git a/src/shared/fileio.c b/src/shared/fileio.c
> index c7b2cd8..c77afd9 100644
> --- a/src/shared/fileio.c
> +++ b/src/shared/fileio.c
> @@ -251,7 +251,8 @@ int read_full_file(const char *fn, char **contents, size_t *size) {
>  static int parse_env_file_internal(
>                  const char *fname,
>                  const char *newline,
> -                int (*push) (const char *filename, unsigned line,
> +                int *n_pushed,
> +                int (*push) (const char *filename, unsigned line, int *n_pushed,
>                               const char *key, char *value, void *userdata),
>                  void *userdata) {
>  
> @@ -340,7 +341,7 @@ static int parse_env_file_internal(
>                                  if (last_key_whitespace != (size_t) -1)
>                                          key[last_key_whitespace] = 0;
>  
> -                                r = push(fname, line, key, value, userdata);
> +                                r = push(fname, line, n_pushed, key, value, userdata);
>                                  if (r < 0)
>                                          goto fail;
>  
> @@ -385,7 +386,7 @@ static int parse_env_file_internal(
>                                  if (last_key_whitespace != (size_t) -1)
>                                          key[last_key_whitespace] = 0;
>  
> -                                r = push(fname, line, key, value, userdata);
> +                                r = push(fname, line, n_pushed, key, value, userdata);
>                                  if (r < 0)
>                                          goto fail;
>  
> @@ -520,7 +521,7 @@ static int parse_env_file_internal(
>                  if (last_key_whitespace != (size_t) -1)
>                          key[last_key_whitespace] = 0;
>  
> -                r = push(fname, line, key, value, userdata);
> +                r = push(fname, line, n_pushed, key, value, userdata);
>                  if (r < 0)
>                          goto fail;
>          }
> @@ -532,7 +533,7 @@ fail:
>          return r;
>  }
>  
> -static int parse_env_file_push(const char *filename, unsigned line,
> +static int parse_env_file_push(const char *filename, unsigned line, int *n_pushed,
>                                 const char *key, char *value, void *userdata) {
>  
>          const char *k;
> @@ -565,6 +566,10 @@ static int parse_env_file_push(const char *filename, unsigned line,
>                          va_end(aq);
>                          free(*v);
>                          *v = value;
> +
> +                        if (n_pushed)
> +                                (*n_pushed)++;
> +
>                          return 1;
>                  }
>          }
> @@ -579,19 +584,19 @@ int parse_env_file(
>                  const char *newline, ...) {
>  
>          va_list ap;
> -        int r;
> +        int r, n_pushed = 0;
>  
>          if (!newline)
>                  newline = NEWLINE;
>  
>          va_start(ap, newline);
> -        r = parse_env_file_internal(fname, newline, parse_env_file_push, &ap);
> +        r = parse_env_file_internal(fname, newline, &n_pushed, parse_env_file_push, &ap);
>          va_end(ap);
>  
> -        return r;
> +        return r < 0 ? r : n_pushed;
>  }
>  
> -static int load_env_file_push(const char *filename, unsigned line,
> +static int load_env_file_push(const char *filename, unsigned line, int *n_pushed,
>                                const char *key, char *value, void *userdata) {
>          char ***m = userdata;
>          char *p;
> @@ -621,6 +626,9 @@ static int load_env_file_push(const char *filename, unsigned line,
>          if (r < 0)
>                  return r;
>  
> +        if (n_pushed)
> +                (*n_pushed)++;
> +
>          free(value);
>          return 0;
>  }
> @@ -632,7 +640,7 @@ int load_env_file(const char *fname, const char *newline, char ***rl) {
>          if (!newline)
>                  newline = NEWLINE;
>  
> -        r = parse_env_file_internal(fname, newline, load_env_file_push, &m);
> +        r = parse_env_file_internal(fname, newline, NULL, load_env_file_push, &m);
>          if (r < 0) {
>                  strv_free(m);
>                  return r;
> -- 
> 1.9.0

Probably got lost in the backlog. Let me know if there is anything wrong with
this submission.

Michal
> 


More information about the systemd-devel mailing list