From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/11158 Path: news.gmane.org!.POSTED!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: [setlocale]: return only one copy if all six parts of locale are same Date: Sat, 18 Mar 2017 10:39:54 -0400 Message-ID: <20170318143954.GP1693@brightrain.aerifal.cx> References: <20170318123335.GO1693@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="rz+pwK2yUstbofK6" X-Trace: blaine.gmane.org 1489848010 32069 195.159.176.226 (18 Mar 2017 14:40:10 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 18 Mar 2017 14:40:10 +0000 (UTC) User-Agent: Mutt/1.5.21 (2010-09-15) To: musl@lists.openwall.com Original-X-From: musl-return-11173-gllmg-musl=m.gmane.org@lists.openwall.com Sat Mar 18 15:40:06 2017 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1cpFWU-0007ni-Mt for gllmg-musl@m.gmane.org; Sat, 18 Mar 2017 15:40:02 +0100 Original-Received: (qmail 28293 invoked by uid 550); 18 Mar 2017 14:40:07 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 28275 invoked from network); 18 Mar 2017 14:40:06 -0000 Content-Disposition: inline In-Reply-To: Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:11158 Archived-At: --rz+pwK2yUstbofK6 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 --rz+pwK2yUstbofK6 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="setlocale.diff" 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; iname : "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); --rz+pwK2yUstbofK6--