mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: [setlocale]: return only one copy if all six parts of locale are same
Date: Sat, 18 Mar 2017 10:39:54 -0400	[thread overview]
Message-ID: <20170318143954.GP1693@brightrain.aerifal.cx> (raw)
In-Reply-To: <CAPG2z08Z2V08wQPT0EN27Fgc97Ac14VNxQXQOjsfC2oK2+wz0Q@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 561 bytes --]

On Sat, Mar 18, 2017 at 09:48:41PM +0800, He X wrote:
> oops, i missed UNLOCK(lock).
> 
> [...]
> >>
> >> I think the results of this patch can be achieved much more simply.
> >> Just compare lm pointers (rather than string contents) for equality at
> >> each iteration in the current loop, counting how many were equal to
> >> libc.global_locale.cat[0]. If that number equals LC_ALL, return
> >> libc.global_locale.cat[0]->name (or "C" if the lm is null) rather than
> >> buf.
> >>
> >> Rich
> >>
> >
> >

By simpler I meant something like the attached.

Rich

[-- Attachment #2: setlocale.diff --]
[-- Type: text/plain, Size: 774 bytes --]

diff --git a/src/locale/setlocale.c b/src/locale/setlocale.c
index 8dae5a4..ca1646f 100644
--- a/src/locale/setlocale.c
+++ b/src/locale/setlocale.c
@@ -48,10 +48,13 @@ char *setlocale(int cat, const char *name)
 			}
 		}
 		char *s = buf;
+		const char *part;
+		int same = 0;
 		for (i=0; i<LC_ALL; i++) {
 			const struct __locale_map *lm =
 				libc.global_locale.cat[i];
-			const char *part = lm ? lm->name : "C";
+			if (lm == libc.global_locale.cat[0]) same++;
+			part = lm ? lm->name : "C";
 			size_t l = strlen(part);
 			memcpy(s, part, l);
 			s[l] = ';';
@@ -59,7 +62,7 @@ char *setlocale(int cat, const char *name)
 		}
 		*--s = 0;
 		UNLOCK(lock);
-		return buf;
+		return same==LC_ALL ? part : buf;
 	}
 
 	char *ret = setlocale_one_unlocked(cat, name);

  reply	other threads:[~2017-03-18 14:39 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-18  7:37 He X
2017-03-18 12:33 ` Rich Felker
2017-03-18 13:36   ` He X
2017-03-18 13:48     ` He X
2017-03-18 14:39       ` Rich Felker [this message]
2017-03-18 15:00         ` He X

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170318143954.GP1693@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).