提交 16319a5d 编写于 作者: R Rich Felker

make setlocale return a single name for LC_ALL if all categories match

when called for LC_ALL, setlocale has to return a string representing
the state of all locale categories. the simplest way to do this was to
always return a delimited list of values for each category, but that's
not friendly in the fairly common case where all categories have the
same setting. He X proposed a patch to check for this case and return
a single name; this patch is a simplified approach to do the same.
上级 0c53178e
...@@ -48,10 +48,13 @@ char *setlocale(int cat, const char *name) ...@@ -48,10 +48,13 @@ char *setlocale(int cat, const char *name)
} }
} }
char *s = buf; char *s = buf;
const char *part;
int same = 0;
for (i=0; i<LC_ALL; i++) { for (i=0; i<LC_ALL; i++) {
const struct __locale_map *lm = const struct __locale_map *lm =
libc.global_locale.cat[i]; 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); size_t l = strlen(part);
memcpy(s, part, l); memcpy(s, part, l);
s[l] = ';'; s[l] = ';';
...@@ -59,7 +62,7 @@ char *setlocale(int cat, const char *name) ...@@ -59,7 +62,7 @@ char *setlocale(int cat, const char *name)
} }
*--s = 0; *--s = 0;
UNLOCK(lock); UNLOCK(lock);
return buf; return same==LC_ALL ? (char *)part : buf;
} }
char *ret = setlocale_one_unlocked(cat, name); char *ret = setlocale_one_unlocked(cat, name);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册