[systemd-devel] [PATCH 3/3] remove RUN_WITH_LOCALE et all, use extended locale functions instead

David Herrmann dh.herrmann at gmail.com
Sun Jan 18 13:06:52 PST 2015


Hi

On Thu, Jan 15, 2015 at 6:27 AM, Cristian Rodríguez
<crrodriguez at opensuse.org> wrote:
> There were two callers, one can use strtod_l() and the other strptime_l()
> ---
>  src/import/curl-util.c | 38 +++++++++++++++++++++++---------------
>  src/shared/util.c      | 18 +++++++++++++-----
>  src/shared/util.h      | 26 --------------------------
>  3 files changed, 36 insertions(+), 46 deletions(-)

I fixed up the whitespace (8chars instead of 4) and other coding-style
issues and pushed the patch.

Thanks
David

> diff --git a/src/import/curl-util.c b/src/import/curl-util.c
> index 0c6c867..bbb68f7 100644
> --- a/src/import/curl-util.c
> +++ b/src/import/curl-util.c
> @@ -418,27 +418,35 @@ int curl_header_strdup(const void *contents, size_t sz, const char *field, char
>  int curl_parse_http_time(const char *t, usec_t *ret) {
>          struct tm tm;
>          time_t v;
> +        const char *e;
> +        locale_t loc;
>
>          assert(t);
>          assert(ret);
>
> -        RUN_WITH_LOCALE(LC_TIME, "C") {
> -                const char *e;
> -
> -                /* RFC822 */
> -                e = strptime(t, "%a, %d %b %Y %H:%M:%S %Z", &tm);
> -                if (!e || *e != 0)
> -                        /* RFC 850 */
> -                        e = strptime(t, "%A, %d-%b-%y %H:%M:%S %Z", &tm);
> -                if (!e || *e != 0)
> -                        /* ANSI C */
> -                        e = strptime(t, "%a %b %d %H:%M:%S %Y", &tm);
> -                if (!e || *e != 0)
> -                        return -EINVAL;
> -
> -                v = timegm(&tm);
> +        loc = newlocale (LC_TIME_MASK, "C", (locale_t)0);
> +
> +        if (loc == (locale_t) 0)
> +            return -errno;
> +
> +        /* RFC822 */
> +        e = strptime_l(t, "%a, %d %b %Y %H:%M:%S %Z", &tm, loc);
> +
> +        if (!e || *e != 0)
> +            /* RFC 850 */
> +            e = strptime_l(t, "%A, %d-%b-%y %H:%M:%S %Z", &tm, loc);
> +        if (!e || *e != 0)
> +            /* ANSI C */
> +            e = strptime_l(t, "%a %b %d %H:%M:%S %Y", &tm, loc);
> +        if (!e || *e != 0) {
> +            freelocale(loc);
> +            return -EINVAL;
>          }
>
> +        freelocale(loc);
> +
> +        v = timegm(&tm);
> +
>          if (v == (time_t) -1)
>                  return -EINVAL;
>
> diff --git a/src/shared/util.c b/src/shared/util.c
> index 884e782..599f3ca 100644
> --- a/src/shared/util.c
> +++ b/src/shared/util.c
> @@ -507,19 +507,27 @@ int safe_atolli(const char *s, long long int *ret_lli) {
>  int safe_atod(const char *s, double *ret_d) {
>          char *x = NULL;
>          double d = 0;
> +        locale_t loc;
>
>          assert(s);
>          assert(ret_d);
>
> -        RUN_WITH_LOCALE(LC_NUMERIC_MASK, "C") {
> -                errno = 0;
> -                d = strtod(s, &x);
> -        }
> +        loc = newlocale (LC_NUMERIC_MASK, "C", (locale_t)0);
>
> -        if (!x || x == s || *x || errno)
> +        if(loc == (locale_t)0)
> +            return -errno;
> +
> +        errno = 0;
> +        d = strtod_l(s, &x, loc);
> +
> +        if (!x || x == s || *x || errno) {
> +                freelocale(loc);
>                  return errno ? -errno : -EINVAL;
> +        }
>
>          *ret_d = (double) d;
> +        freelocale(loc);
> +
>          return 0;
>  }
>
> diff --git a/src/shared/util.h b/src/shared/util.h
> index fdb9fb6..8445371 100644
> --- a/src/shared/util.h
> +++ b/src/shared/util.h
> @@ -942,32 +942,6 @@ int unlink_noerrno(const char *path);
>                  _r_;                                                    \
>          })
>
> -struct _locale_struct_ {
> -        locale_t saved_locale;
> -        locale_t new_locale;
> -        bool quit;
> -};
> -
> -static inline void _reset_locale_(struct _locale_struct_ *s) {
> -        PROTECT_ERRNO;
> -        if (s->saved_locale != (locale_t) 0)
> -                uselocale(s->saved_locale);
> -        if (s->new_locale != (locale_t) 0)
> -                freelocale(s->new_locale);
> -}
> -
> -#define RUN_WITH_LOCALE(mask, loc) \
> -        for (_cleanup_(_reset_locale_) struct _locale_struct_ _saved_locale_ = { (locale_t) 0, (locale_t) 0, false }; \
> -             ({                                                         \
> -                     if (!_saved_locale_.quit) {                        \
> -                             PROTECT_ERRNO;                             \
> -                             _saved_locale_.new_locale = newlocale((mask), (loc), (locale_t) 0); \
> -                             if (_saved_locale_.new_locale != (locale_t) 0)     \
> -                                     _saved_locale_.saved_locale = uselocale(_saved_locale_.new_locale); \
> -                     }                                                  \
> -                     !_saved_locale_.quit; }) ;                         \
> -             _saved_locale_.quit = true)
> -
>  bool id128_is_valid(const char *s) _pure_;
>
>  int split_pair(const char *s, const char *sep, char **l, char **r);
> --
> 2.2.1
>
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/systemd-devel


More information about the systemd-devel mailing list