From nobody Fri May 3 12:36:03 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1498221426902753.2133208337099; Fri, 23 Jun 2017 05:37:06 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6DDAEC04F4A5; Fri, 23 Jun 2017 12:37:03 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 286266C52E; Fri, 23 Jun 2017 12:37:03 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 05F8A260; Fri, 23 Jun 2017 12:37:02 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v5NCWZpT015138 for ; Fri, 23 Jun 2017 08:32:35 -0400 Received: by smtp.corp.redhat.com (Postfix) id 404C253C42; Fri, 23 Jun 2017 12:32:35 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 759A452351; Fri, 23 Jun 2017 12:32:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 6DDAEC04F4A5 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 6DDAEC04F4A5 From: Peter Krempa To: libvir-list@redhat.com Date: Fri, 23 Jun 2017 14:32:39 +0200 Message-Id: <3f227002b4f620109da45dbe4f0110918f41a51a.1498221103.git.pkrempa@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Cc: Martin Kletzander Subject: [libvirt] [PATCH v2] util: Extract locale-related fixes into separate functions X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Fri, 23 Jun 2017 12:37:04 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" From: Martin Kletzander Signed-off-by: Martin Kletzander --- src/util/virstring.c | 114 +++++++++++++++++++++++++++++++++--------------= ---- 1 file changed, 75 insertions(+), 39 deletions(-) v2: - fixed Martin's laziness ... oh wait. diff --git a/src/util/virstring.c b/src/util/virstring.c index feea5be05..9b54bd6fb 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -521,19 +521,81 @@ virStrToLong_ullp(char const *s, char **end_ptr, int = base, /* In case thread-safe locales are available */ #if HAVE_NEWLOCALE -static locale_t virLocale; +typedef locale_t virLocale; +static virLocale virLocaleRaw; static int virLocaleOnceInit(void) { - virLocale =3D newlocale(LC_ALL_MASK, "C", (locale_t)0); - if (!virLocale) + virLocaleRaw =3D newlocale(LC_ALL_MASK, "C", (locale_t)0); + if (!virLocaleRaw) return -1; return 0; } VIR_ONCE_GLOBAL_INIT(virLocale); -#endif + +/** + * virLocaleSetRaw: + * + * @oldlocale: set to old locale pointer + * + * Sets the locale to 'C' to allow operating on non-localized objects. + * Returns 0 on success -1 on error. + */ +static int +virLocaleSetRaw(virLocale *oldlocale) +{ + if (virLocaleInitialize() < 0) + return -1; + *oldlocale =3D uselocale(virLocaleRaw); + return 0; +} + +static void +virLocaleRevert(virLocale *oldlocale) +{ + uselocale(*oldlocale); +} + +static void +virLocaleFixupRadix(char **strp ATTRIBUTE_UNUSED) +{ +} + +#else /* !HAVE_NEWLOCALE */ + +typedef int virLocale; + +static int +virLocaleSetRaw(virLocale *oldlocale ATTRIBUTE_UNUSED) +{ + return 0; +} + +static void +virLocaleRevert(virLocale *oldlocale ATTRIBUTE_UNUSED) +{ +} + +static void +virLocaleFixupRadix(char **strp) +{ + char *radix, *tmp; + struct lconv *lc; + + lc =3D localeconv(); + radix =3D lc->decimal_point; + tmp =3D strstr(*strp, radix); + if (tmp) { + *tmp =3D '.'; + if (strlen(radix) > 1) + memmove(tmp + 1, tmp + strlen(radix), strlen(*strp) - (tmp - *= strp)); + } +} + +#endif /* !HAVE_NEWLOCALE */ + /** * virStrToDouble @@ -547,22 +609,17 @@ virStrToDouble(char const *s, char **end_ptr, double *result) { + virLocale oldlocale; double val; char *p; int err; errno =3D 0; -#if HAVE_NEWLOCALE - locale_t old_loc; - if (virLocaleInitialize() < 0) + if (virLocaleSetRaw(&oldlocale) < 0) return -1; - - old_loc =3D uselocale(virLocale); -#endif val =3D strtod(s, &p); /* exempt from syntax-check */ -#if HAVE_NEWLOCALE - uselocale(old_loc); -#endif + virLocaleRevert(&oldlocale); + err =3D (errno || (!end_ptr && *p) || p =3D=3D s); if (end_ptr) *end_ptr =3D p; @@ -582,38 +639,17 @@ virStrToDouble(char const *s, int virDoubleToStr(char **strp, double number) { + virLocale oldlocale; int ret =3D -1; -#if HAVE_NEWLOCALE - - locale_t old_loc; - - if (virLocaleInitialize() < 0) - goto error; + if (virLocaleSetRaw(&oldlocale) < 0) + return -1; - old_loc =3D uselocale(virLocale); ret =3D virAsprintf(strp, "%lf", number); - uselocale(old_loc); - -#else - char *radix, *tmp; - struct lconv *lc; + virLocaleRevert(&oldlocale); + virLocaleFixupRadix(strp); - if ((ret =3D virAsprintf(strp, "%lf", number) < 0)) - goto error; - - lc =3D localeconv(); - radix =3D lc->decimal_point; - tmp =3D strstr(*strp, radix); - if (tmp) { - *tmp =3D '.'; - if (strlen(radix) > 1) - memmove(tmp + 1, tmp + strlen(radix), strlen(*strp) - (tmp - *= strp)); - } - -#endif /* HAVE_NEWLOCALE */ - error: return ret; } --=20 2.12.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list