ce426f
commit 585367266923156ac6fb789939a923641ba5aaf4
ce426f
Author: Florian Weimer <fweimer@redhat.com>
ce426f
Date:   Wed May 28 14:05:03 2014 +0200
ce426f
ce426f
    manual: Update the locale documentation
ce426f
ce426f
commit 4e8f95a0df7c2300b830ec12c0ae1e161bc8a8a3
ce426f
Author: Florian Weimer <fweimer@redhat.com>
ce426f
Date:   Mon May 12 15:24:12 2014 +0200
ce426f
ce426f
    _nl_find_locale: Improve handling of crafted locale names [BZ #17137]
ce426f
    
ce426f
    Prevent directory traversal in locale-related environment variables
ce426f
    (CVE-2014-0475).
ce426f
ce426f
commit d183645616b0533b3acee28f1a95570bffbdf50f
ce426f
Author: Florian Weimer <fweimer@redhat.com>
ce426f
Date:   Wed May 28 14:41:52 2014 +0200
ce426f
ce426f
    setlocale: Use the heap for the copy of the locale argument
ce426f
    
ce426f
    This avoids alloca calls with potentially large arguments.
ce426f
ce426f
diff -pruN glibc-2.17-c758a686/locale/findlocale.c glibc-2.17-c758a686/locale/findlocale.c
ce426f
--- glibc-2.17-c758a686/locale/findlocale.c	2013-08-11 04:22:55.000000000 +0530
ce426f
+++ glibc-2.17-c758a686/locale/findlocale.c	2014-08-26 16:14:50.403253778 +0530
ce426f
@@ -17,6 +17,7 @@
ce426f
    <http://www.gnu.org/licenses/>.  */
ce426f
 
ce426f
 #include <assert.h>
ce426f
+#include <errno.h>
ce426f
 #include <locale.h>
ce426f
 #include <stdlib.h>
ce426f
 #include <string.h>
ce426f
@@ -57,6 +58,45 @@ struct loaded_l10nfile *_nl_locale_file_
ce426f
 
ce426f
 const char _nl_default_locale_path[] attribute_hidden = LOCALEDIR;
ce426f
 
ce426f
+/* Checks if the name is actually present, that is, not NULL and not
ce426f
+   empty.  */
ce426f
+static inline int
ce426f
+name_present (const char *name)
ce426f
+{
ce426f
+  return name != NULL && name[0] != '\0';
ce426f
+}
ce426f
+
ce426f
+/* Checks that the locale name neither extremely long, nor contains a
ce426f
+   ".." path component (to prevent directory traversal).  */
ce426f
+static inline int
ce426f
+valid_locale_name (const char *name)
ce426f
+{
ce426f
+  /* Not set.  */
ce426f
+  size_t namelen = strlen (name);
ce426f
+  /* Name too long.  The limit is arbitrary and prevents stack overflow
ce426f
+     issues later.  */
ce426f
+  if (__glibc_unlikely (namelen > 255))
ce426f
+    return 0;
ce426f
+  /* Directory traversal attempt.  */
ce426f
+  static const char slashdot[4] = {'/', '.', '.', '/'};
ce426f
+  if (__glibc_unlikely (memmem (name, namelen,
ce426f
+				slashdot, sizeof (slashdot)) != NULL))
ce426f
+    return 0;
ce426f
+  if (namelen == 2 && __glibc_unlikely (name[0] == '.' && name [1] == '.'))
ce426f
+    return 0;
ce426f
+  if (namelen >= 3
ce426f
+      && __glibc_unlikely (((name[0] == '.'
ce426f
+			     && name[1] == '.'
ce426f
+			     && name[2] == '/')
ce426f
+			    || (name[namelen - 3] == '/'
ce426f
+				&& name[namelen - 2] == '.'
ce426f
+				&& name[namelen - 1] == '.'))))
ce426f
+    return 0;
ce426f
+  /* If there is a slash in the name, it must start with one.  */
ce426f
+  if (__glibc_unlikely (memchr (name, '/', namelen) != NULL) && name[0] != '/')
ce426f
+    return 0;
ce426f
+  return 1;
ce426f
+}
ce426f
 
ce426f
 struct __locale_data *
ce426f
 internal_function
ce426f
@@ -65,7 +105,7 @@ _nl_find_locale (const char *locale_path
ce426f
 {
ce426f
   int mask;
ce426f
   /* Name of the locale for this category.  */
ce426f
-  char *loc_name;
ce426f
+  char *loc_name = (char *) *name;
ce426f
   const char *language;
ce426f
   const char *modifier;
ce426f
   const char *territory;
ce426f
@@ -73,31 +113,39 @@ _nl_find_locale (const char *locale_path
ce426f
   const char *normalized_codeset;
ce426f
   struct loaded_l10nfile *locale_file;
ce426f
 
ce426f
-  if ((*name)[0] == '\0')
ce426f
+  if (loc_name[0] == '\0')
ce426f
     {
ce426f
       /* The user decides which locale to use by setting environment
ce426f
 	 variables.  */
ce426f
-      *name = getenv ("LC_ALL");
ce426f
-      if (*name == NULL || (*name)[0] == '\0')
ce426f
-	*name = getenv (_nl_category_names.str
ce426f
+      loc_name = getenv ("LC_ALL");
ce426f
+      if (!name_present (loc_name))
ce426f
+	loc_name = getenv (_nl_category_names.str
ce426f
 			+ _nl_category_name_idxs[category]);
ce426f
-      if (*name == NULL || (*name)[0] == '\0')
ce426f
-	*name = getenv ("LANG");
ce426f
+      if (!name_present (loc_name))
ce426f
+	loc_name = getenv ("LANG");
ce426f
+      if (!name_present (loc_name))
ce426f
+	loc_name = (char *) _nl_C_name;
ce426f
     }
ce426f
 
ce426f
-  if (*name == NULL || (*name)[0] == '\0'
ce426f
-      || (__builtin_expect (__libc_enable_secure, 0)
ce426f
-	  && strchr (*name, '/') != NULL))
ce426f
-    *name = (char *) _nl_C_name;
ce426f
+  /* We used to fall back to the C locale if the name contains a slash
ce426f
+     character '/', but we now check for directory traversal in
ce426f
+     valid_locale_name, so this is no longer necessary.  */
ce426f
 
ce426f
-  if (__builtin_expect (strcmp (*name, _nl_C_name), 1) == 0
ce426f
-      || __builtin_expect (strcmp (*name, _nl_POSIX_name), 1) == 0)
ce426f
+  if (__builtin_expect (strcmp (loc_name, _nl_C_name), 1) == 0
ce426f
+      || __builtin_expect (strcmp (loc_name, _nl_POSIX_name), 1) == 0)
ce426f
     {
ce426f
       /* We need not load anything.  The needed data is contained in
ce426f
 	 the library itself.  */
ce426f
       *name = (char *) _nl_C_name;
ce426f
       return _nl_C[category];
ce426f
     }
ce426f
+  else if (!valid_locale_name (loc_name))
ce426f
+    {
ce426f
+      __set_errno (EINVAL);
ce426f
+      return NULL;
ce426f
+    }
ce426f
+
ce426f
+  *name = loc_name;
ce426f
 
ce426f
   /* We really have to load some data.  First we try the archive,
ce426f
      but only if there was no LOCPATH environment variable specified.  */
ce426f
diff -pruN glibc-2.17-c758a686/locale/setlocale.c glibc-2.17-c758a686/locale/setlocale.c
ce426f
--- glibc-2.17-c758a686/locale/setlocale.c	2013-08-11 04:22:55.000000000 +0530
ce426f
+++ glibc-2.17-c758a686/locale/setlocale.c	2014-08-26 16:14:50.401253764 +0530
ce426f
@@ -272,6 +272,8 @@ setlocale (int category, const char *loc
ce426f
 	 of entries of the form `CATEGORY=VALUE'.  */
ce426f
       const char *newnames[__LC_LAST];
ce426f
       struct __locale_data *newdata[__LC_LAST];
ce426f
+      /* Copy of the locale argument, for in-place splitting.  */
ce426f
+      char *locale_copy = NULL;
ce426f
 
ce426f
       /* Set all name pointers to the argument name.  */
ce426f
       for (category = 0; category < __LC_LAST; ++category)
ce426f
@@ -281,7 +283,13 @@ setlocale (int category, const char *loc
ce426f
       if (__builtin_expect (strchr (locale, ';') != NULL, 0))
ce426f
 	{
ce426f
 	  /* This is a composite name.  Make a copy and split it up.  */
ce426f
-	  char *np = strdupa (locale);
ce426f
+	  locale_copy = strdup (locale);
ce426f
+	  if (__glibc_unlikely (locale_copy == NULL))
ce426f
+	    {
ce426f
+	      __libc_rwlock_unlock (__libc_setlocale_lock);
ce426f
+	      return NULL;
ce426f
+	    }
ce426f
+	  char *np = locale_copy;
ce426f
 	  char *cp;
ce426f
 	  int cnt;
ce426f
 
ce426f
@@ -299,6 +307,7 @@ setlocale (int category, const char *loc
ce426f
 		{
ce426f
 		error_return:
ce426f
 		  __libc_rwlock_unlock (__libc_setlocale_lock);
ce426f
+		  free (locale_copy);
ce426f
 
ce426f
 		  /* Bogus category name.  */
ce426f
 		  ERROR_RETURN;
ce426f
@@ -391,8 +400,9 @@ setlocale (int category, const char *loc
ce426f
       /* Critical section left.  */
ce426f
       __libc_rwlock_unlock (__libc_setlocale_lock);
ce426f
 
ce426f
-      /* Free the resources (the locale path variable).  */
ce426f
+      /* Free the resources.  */
ce426f
       free (locale_path);
ce426f
+      free (locale_copy);
ce426f
 
ce426f
       return composite;
ce426f
     }
ce426f
diff -pruN glibc-2.17-c758a686/localedata/Makefile glibc-2.17-c758a686/localedata/Makefile
ce426f
--- glibc-2.17-c758a686/localedata/Makefile	2014-08-26 16:15:22.656474571 +0530
ce426f
+++ glibc-2.17-c758a686/localedata/Makefile	2014-08-26 16:14:50.403253778 +0530
ce426f
@@ -77,7 +77,7 @@ locale_test_suite := tst_iswalnum tst_is
ce426f
 
ce426f
 tests = $(locale_test_suite) tst-digits tst-setlocale bug-iconv-trans \
ce426f
 	tst-leaks tst-mbswcs6 tst-xlocale1 tst-xlocale2 bug-usesetlocale \
ce426f
-	tst-strfmon1 tst-sscanf bug-setlocale1 tst-setlocale2
ce426f
+	tst-strfmon1 tst-sscanf bug-setlocale1 tst-setlocale2 tst-setlocale3
ce426f
 ifeq (yes,$(build-shared))
ce426f
 ifneq (no,$(PERL))
ce426f
 tests: $(objpfx)mtrace-tst-leaks
ce426f
diff -pruN glibc-2.17-c758a686/localedata/tst-setlocale3.c glibc-2.17-c758a686/localedata/tst-setlocale3.c
ce426f
--- glibc-2.17-c758a686/localedata/tst-setlocale3.c	1970-01-01 05:30:00.000000000 +0530
ce426f
+++ glibc-2.17-c758a686/localedata/tst-setlocale3.c	2014-08-26 16:14:50.403253778 +0530
ce426f
@@ -0,0 +1,203 @@
ce426f
+/* Regression test for setlocale invalid environment variable handling.
ce426f
+   Copyright (C) 2014 Free Software Foundation, Inc.
ce426f
+   This file is part of the GNU C Library.
ce426f
+
ce426f
+   The GNU C Library is free software; you can redistribute it and/or
ce426f
+   modify it under the terms of the GNU Lesser General Public
ce426f
+   License as published by the Free Software Foundation; either
ce426f
+   version 2.1 of the License, or (at your option) any later version.
ce426f
+
ce426f
+   The GNU C Library is distributed in the hope that it will be useful,
ce426f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ce426f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ce426f
+   Lesser General Public License for more details.
ce426f
+
ce426f
+   You should have received a copy of the GNU Lesser General Public
ce426f
+   License along with the GNU C Library; if not, see
ce426f
+   <http://www.gnu.org/licenses/>.  */
ce426f
+
ce426f
+#include <locale.h>
ce426f
+#include <stdio.h>
ce426f
+#include <stdlib.h>
ce426f
+#include <string.h>
ce426f
+
ce426f
+/* The result of setlocale may be overwritten by subsequent calls, so
ce426f
+   this wrapper makes a copy.  */
ce426f
+static char *
ce426f
+setlocale_copy (int category, const char *locale)
ce426f
+{
ce426f
+  const char *result = setlocale (category, locale);
ce426f
+  if (result == NULL)
ce426f
+    return NULL;
ce426f
+  return strdup (result);
ce426f
+}
ce426f
+
ce426f
+static char *de_locale;
ce426f
+
ce426f
+static void
ce426f
+setlocale_fail (const char *envstring)
ce426f
+{
ce426f
+  setenv ("LC_CTYPE", envstring, 1);
ce426f
+  if (setlocale (LC_CTYPE, "") != NULL)
ce426f
+    {
ce426f
+      printf ("unexpected setlocale success for \"%s\" locale\n", envstring);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  const char *newloc = setlocale (LC_CTYPE, NULL);
ce426f
+  if (strcmp (newloc, de_locale) != 0)
ce426f
+    {
ce426f
+      printf ("failed setlocale call \"%s\" changed locale to \"%s\"\n",
ce426f
+	      envstring, newloc);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+}
ce426f
+
ce426f
+static void
ce426f
+setlocale_success (const char *envstring)
ce426f
+{
ce426f
+  setenv ("LC_CTYPE", envstring, 1);
ce426f
+  char *newloc = setlocale_copy (LC_CTYPE, "");
ce426f
+  if (newloc == NULL)
ce426f
+    {
ce426f
+      printf ("setlocale for \"%s\": %m\n", envstring);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  if (strcmp (newloc, de_locale) == 0)
ce426f
+    {
ce426f
+      printf ("setlocale with LC_CTYPE=\"%s\" left locale at \"%s\"\n",
ce426f
+	      envstring, de_locale);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  if (setlocale (LC_CTYPE, de_locale) == NULL)
ce426f
+    {
ce426f
+      printf ("restoring locale \"%s\" with LC_CTYPE=\"%s\": %m\n",
ce426f
+	      de_locale, envstring);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  char *newloc2 = setlocale_copy (LC_CTYPE, newloc);
ce426f
+  if (newloc2 == NULL)
ce426f
+    {
ce426f
+      printf ("restoring locale \"%s\" following \"%s\": %m\n",
ce426f
+	      newloc, envstring);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  if (strcmp (newloc, newloc2) != 0)
ce426f
+    {
ce426f
+      printf ("representation of locale \"%s\" changed from \"%s\" to \"%s\"",
ce426f
+	      envstring, newloc, newloc2);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  free (newloc);
ce426f
+  free (newloc2);
ce426f
+
ce426f
+  if (setlocale (LC_CTYPE, de_locale) == NULL)
ce426f
+    {
ce426f
+      printf ("restoring locale \"%s\" with LC_CTYPE=\"%s\": %m\n",
ce426f
+	      de_locale, envstring);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+}
ce426f
+
ce426f
+/* Checks that a known-good locale still works if LC_ALL contains a
ce426f
+   value which should be ignored.  */
ce426f
+static void
ce426f
+setlocale_ignore (const char *to_ignore)
ce426f
+{
ce426f
+  const char *fr_locale = "fr_FR.UTF-8";
ce426f
+  setenv ("LC_CTYPE", fr_locale, 1);
ce426f
+  char *expected_locale = setlocale_copy (LC_CTYPE, "");
ce426f
+  if (expected_locale == NULL)
ce426f
+    {
ce426f
+      printf ("setlocale with LC_CTYPE=\"%s\" failed: %m\n", fr_locale);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  if (setlocale (LC_CTYPE, de_locale) == NULL)
ce426f
+    {
ce426f
+      printf ("failed to restore locale: %m\n");
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  unsetenv ("LC_CTYPE");
ce426f
+
ce426f
+  setenv ("LC_ALL", to_ignore, 1);
ce426f
+  setenv ("LC_CTYPE", fr_locale, 1);
ce426f
+  const char *actual_locale = setlocale (LC_CTYPE, "");
ce426f
+  if (actual_locale == NULL)
ce426f
+    {
ce426f
+      printf ("setlocale with LC_ALL, LC_CTYPE=\"%s\" failed: %m\n",
ce426f
+	      fr_locale);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  if (strcmp (actual_locale, expected_locale) != 0)
ce426f
+    {
ce426f
+      printf ("setlocale under LC_ALL failed: got \"%s\", expected \"%s\"\n",
ce426f
+	      actual_locale, expected_locale);
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  unsetenv ("LC_CTYPE");
ce426f
+  setlocale_success (fr_locale);
ce426f
+  unsetenv ("LC_ALL");
ce426f
+  free (expected_locale);
ce426f
+}
ce426f
+
ce426f
+static int
ce426f
+do_test (void)
ce426f
+{
ce426f
+  /* The glibc test harness sets this environment variable
ce426f
+     uncondionally.  */
ce426f
+  unsetenv ("LC_ALL");
ce426f
+
ce426f
+  de_locale = setlocale_copy (LC_CTYPE, "de_DE.UTF-8");
ce426f
+  if (de_locale == NULL)
ce426f
+    {
ce426f
+      printf ("setlocale (LC_CTYPE, \"de_DE.UTF-8\"): %m\n");
ce426f
+      return 1;
ce426f
+    }
ce426f
+  setlocale_success ("C");
ce426f
+  setlocale_success ("en_US.UTF-8");
ce426f
+  setlocale_success ("/en_US.UTF-8");
ce426f
+  setlocale_success ("//en_US.UTF-8");
ce426f
+  setlocale_ignore ("");
ce426f
+
ce426f
+  setlocale_fail ("does-not-exist");
ce426f
+  setlocale_fail ("/");
ce426f
+  setlocale_fail ("/../localedata/en_US.UTF-8");
ce426f
+  setlocale_fail ("en_US.UTF-8/");
ce426f
+  setlocale_fail ("en_US.UTF-8/..");
ce426f
+  setlocale_fail ("en_US.UTF-8/../en_US.UTF-8");
ce426f
+  setlocale_fail ("../localedata/en_US.UTF-8");
ce426f
+  {
ce426f
+    size_t large_length = 1024;
ce426f
+    char *large_name = malloc (large_length + 1);
ce426f
+    if (large_name == NULL)
ce426f
+      {
ce426f
+	puts ("malloc failure");
ce426f
+	return 1;
ce426f
+      }
ce426f
+    memset (large_name, '/', large_length);
ce426f
+    const char *suffix = "en_US.UTF-8";
ce426f
+    strcpy (large_name + large_length - strlen (suffix), suffix);
ce426f
+    setlocale_fail (large_name);
ce426f
+    free (large_name);
ce426f
+  }
ce426f
+  {
ce426f
+    size_t huge_length = 64 * 1024 * 1024;
ce426f
+    char *huge_name = malloc (huge_length + 1);
ce426f
+    if (huge_name == NULL)
ce426f
+      {
ce426f
+	puts ("malloc failure");
ce426f
+	return 1;
ce426f
+      }
ce426f
+    memset (huge_name, 'X', huge_length);
ce426f
+    huge_name[huge_length] = '\0';
ce426f
+    /* Construct a composite locale specification. */
ce426f
+    const char *prefix = "LC_CTYPE=de_DE.UTF-8;LC_TIME=";
ce426f
+    memcpy (huge_name, prefix, strlen (prefix));
ce426f
+    setlocale_fail (huge_name);
ce426f
+    free (huge_name);
ce426f
+  }
ce426f
+
ce426f
+  return 0;
ce426f
+}
ce426f
+
ce426f
+#define TEST_FUNCTION do_test ()
ce426f
+#include "../test-skeleton.c"
ce426f
diff -pruN glibc-2.17-c758a686/manual/locale.texi glibc-2.17-c758a686/manual/locale.texi
ce426f
--- glibc-2.17-c758a686/manual/locale.texi	2013-08-11 04:22:55.000000000 +0530
ce426f
+++ glibc-2.17-c758a686/manual/locale.texi	2014-08-26 16:14:50.404253785 +0530
ce426f
@@ -29,6 +29,7 @@ will follow the conventions preferred by
ce426f
 * Setting the Locale::          How a program specifies the locale
ce426f
                                  with library functions.
ce426f
 * Standard Locales::            Locale names available on all systems.
ce426f
+* Locale Names::                Format of system-specific locale names.
ce426f
 * Locale Information::          How to access the information for the locale.
ce426f
 * Formatting Numbers::          A dedicated function to format numbers.
ce426f
 * Yes-or-No Questions::         Check a Response against the locale.
ce426f
@@ -99,14 +100,16 @@ locale named @samp{espana-castellano} to
ce426f
 most of Spain.
ce426f
 
ce426f
 The set of locales supported depends on the operating system you are
ce426f
-using, and so do their names.  We can't make any promises about what
ce426f
-locales will exist, except for one standard locale called @samp{C} or
ce426f
-@samp{POSIX}.  Later we will describe how to construct locales.
ce426f
-@comment (@pxref{Building Locale Files}).
ce426f
+using, and so do their names, except that the standard locale called
ce426f
+@samp{C} or @samp{POSIX} always exist.  @xref{Locale Names}.
ce426f
+
ce426f
+In order to force the system to always use the default locale, the
ce426f
+user can set the @code{LC_ALL} environment variable to @samp{C}.
ce426f
 
ce426f
 @cindex combining locales
ce426f
-A user also has the option of specifying different locales for different
ce426f
-purposes---in effect, choosing a mixture of multiple locales.
ce426f
+A user also has the option of specifying different locales for
ce426f
+different purposes---in effect, choosing a mixture of multiple
ce426f
+locales.  @xref{Locale Categories}.
ce426f
 
ce426f
 For example, the user might specify the locale @samp{espana-castellano}
ce426f
 for most purposes, but specify the locale @samp{usa-english} for
ce426f
@@ -120,7 +123,7 @@ which locales apply.  However, the user
ce426f
 for a particular subset of those purposes.
ce426f
 
ce426f
 @node Locale Categories, Setting the Locale, Choosing Locale, Locales
ce426f
-@section Categories of Activities that Locales Affect
ce426f
+@section Locale Categories
ce426f
 @cindex categories for locales
ce426f
 @cindex locale categories
ce426f
 
ce426f
@@ -128,7 +131,11 @@ The purposes that locales serve are grou
ce426f
 that a user or a program can choose the locale for each category
ce426f
 independently.  Here is a table of categories; each name is both an
ce426f
 environment variable that a user can set, and a macro name that you can
ce426f
-use as an argument to @code{setlocale}.
ce426f
+use as the first argument to @code{setlocale}.
ce426f
+
ce426f
+The contents of the environment variable (or the string in the second
ce426f
+argument to @code{setlocale}) has to be a valid locale name.
ce426f
+@xref{Locale Names}.
ce426f
 
ce426f
 @vtable @code
ce426f
 @comment locale.h
ce426f
@@ -172,7 +179,7 @@ for affirmative and negative responses.
ce426f
 @comment locale.h
ce426f
 @comment ISO
ce426f
 @item LC_ALL
ce426f
-This is not an environment variable; it is only a macro that you can use
ce426f
+This is not a category; it is only a macro that you can use
ce426f
 with @code{setlocale} to set a single locale for all purposes.  Setting
ce426f
 this environment variable overwrites all selections by the other
ce426f
 @code{LC_*} variables or @code{LANG}.
ce426f
@@ -225,13 +232,7 @@ The symbols in this section are defined
ce426f
 @comment ISO
ce426f
 @deftypefun {char *} setlocale (int @var{category}, const char *@var{locale})
ce426f
 The function @code{setlocale} sets the current locale for category
ce426f
-@var{category} to @var{locale}.  A list of all the locales the system
ce426f
-provides can be created by running
ce426f
-
ce426f
-@pindex locale
ce426f
-@smallexample
ce426f
-  locale -a
ce426f
-@end smallexample
ce426f
+@var{category} to @var{locale}.
ce426f
 
ce426f
 If @var{category} is @code{LC_ALL}, this specifies the locale for all
ce426f
 purposes.  The other possible values of @var{category} specify an
ce426f
@@ -256,10 +257,9 @@ is passed in as @var{locale} parameter.
ce426f
 
ce426f
 When you read the current locale for category @code{LC_ALL}, the value
ce426f
 encodes the entire combination of selected locales for all categories.
ce426f
-In this case, the value is not just a single locale name.  In fact, we
ce426f
-don't make any promises about what it looks like.  But if you specify
ce426f
-the same ``locale name'' with @code{LC_ALL} in a subsequent call to
ce426f
-@code{setlocale}, it restores the same combination of locale selections.
ce426f
+If you specify the same ``locale name'' with @code{LC_ALL} in a
ce426f
+subsequent call to @code{setlocale}, it restores the same combination
ce426f
+of locale selections.
ce426f
 
ce426f
 To be sure you can use the returned string encoding the currently selected
ce426f
 locale at a later time, you must make a copy of the string.  It is not
ce426f
@@ -275,20 +275,15 @@ for @var{category}.
ce426f
 If a nonempty string is given for @var{locale}, then the locale of that
ce426f
 name is used if possible.
ce426f
 
ce426f
+The effective locale name (either the second argument to
ce426f
+@code{setlocale}, or if the argument is an empty string, the name
ce426f
+obtained from the process environment) must be valid locale name.
ce426f
+@xref{Locale Names}.
ce426f
+
ce426f
 If you specify an invalid locale name, @code{setlocale} returns a null
ce426f
 pointer and leaves the current locale unchanged.
ce426f
 @end deftypefun
ce426f
 
ce426f
-The path used for finding locale data can be set using the
ce426f
-@code{LOCPATH} environment variable. The default path for finding
ce426f
-locale data is system specific.  It is computed from the value given
ce426f
-as the prefix while configuring the C library.  This value normally is
ce426f
-@file{/usr} or @file{/}.  For the former the complete path is:
ce426f
-
ce426f
-@smallexample
ce426f
-/usr/lib/locale
ce426f
-@end smallexample
ce426f
-
ce426f
 Here is an example showing how you might use @code{setlocale} to
ce426f
 temporarily switch to a new locale.
ce426f
 
ce426f
@@ -328,7 +323,7 @@ locale categories, and future versions o
ce426f
 portability, assume that any symbol beginning with @samp{LC_} might be
ce426f
 defined in @file{locale.h}.
ce426f
 
ce426f
-@node Standard Locales, Locale Information, Setting the Locale, Locales
ce426f
+@node Standard Locales, Locale Names, Setting the Locale, Locales
ce426f
 @section Standard Locales
ce426f
 
ce426f
 The only locale names you can count on finding on all operating systems
ce426f
@@ -362,7 +357,94 @@ with the environment, rather than trying
ce426f
 locale explicitly by name.  Remember, different machines might have
ce426f
 different sets of locales installed.
ce426f
 
ce426f
-@node Locale Information, Formatting Numbers, Standard Locales, Locales
ce426f
+@node Locale Names, Locale Information, Standard Locales, Locales
ce426f
+@section Locale Names
ce426f
+
ce426f
+The following command prints a list of locales supported by the
ce426f
+system:
ce426f
+
ce426f
+@pindex locale
ce426f
+@smallexample
ce426f
+  locale -a
ce426f
+@end smallexample
ce426f
+
ce426f
+@strong{Portability Note:} With the notable exception of the standard
ce426f
+locale names @samp{C} and @samp{POSIX}, locale names are
ce426f
+system-specific.
ce426f
+
ce426f
+Most locale names follow XPG syntax and consist of up to four parts:
ce426f
+
ce426f
+@smallexample
ce426f
+@var{language}[_@var{territory}[.@var{codeset}]][@@@var{modifier}]
ce426f
+@end smallexample
ce426f
+
ce426f
+Beside the first part, all of them are allowed to be missing.  If the
ce426f
+full specified locale is not found, less specific ones are looked for.
ce426f
+The various parts will be stripped off, in the following order:
ce426f
+
ce426f
+@enumerate
ce426f
+@item
ce426f
+codeset
ce426f
+@item
ce426f
+normalized codeset
ce426f
+@item
ce426f
+territory
ce426f
+@item
ce426f
+modifier
ce426f
+@end enumerate
ce426f
+
ce426f
+For example, the locale name @samp{de_AT.iso885915@@euro} denotes a
ce426f
+German-language locale for use in Austria, using the ISO-8859-15
ce426f
+(Latin-9) character set, and with the Euro as the currency symbol.
ce426f
+
ce426f
+In addition to locale names which follow XPG syntax, systems may
ce426f
+provide aliases such as @samp{german}.  Both categories of names must
ce426f
+not contain the slash character @samp{/}.
ce426f
+
ce426f
+If the locale name starts with a slash @samp{/}, it is treated as a
ce426f
+path relative to the configured locale directories; see @code{LOCPATH}
ce426f
+below.  The specified path must not contain a component @samp{..}, or
ce426f
+the name is invalid, and @code{setlocale} will fail.
ce426f
+
ce426f
+@strong{Portability Note:} POSIX suggests that if a locale name starts
ce426f
+with a slash @samp{/}, it is resolved as an absolute path.  However,
ce426f
+@theglibc{} treats it as a relative path under the directories listed
ce426f
+in @code{LOCPATH} (or the default locale directory if @code{LOCPATH}
ce426f
+is unset).
ce426f
+
ce426f
+Locale names which are longer than an implementation-defined limit are
ce426f
+invalid and cause @code{setlocale} to fail.
ce426f
+
ce426f
+As a special case, locale names used with @code{LC_ALL} can combine
ce426f
+several locales, reflecting different locale settings for different
ce426f
+categories.  For example, you might want to use a U.S. locale with ISO
ce426f
+A4 paper format, so you set @code{LANG} to @samp{en_US.UTF-8}, and
ce426f
+@code{LC_PAPER} to @samp{de_DE.UTF-8}.  In this case, the
ce426f
+@code{LC_ALL}-style combined locale name is
ce426f
+
ce426f
+@smallexample
ce426f
+LC_CTYPE=en_US.UTF-8;LC_TIME=en_US.UTF-8;LC_PAPER=de_DE.UTF-8;@dots{}
ce426f
+@end smallexample
ce426f
+
ce426f
+followed by other category settings not shown here.
ce426f
+
ce426f
+@vindex LOCPATH
ce426f
+The path used for finding locale data can be set using the
ce426f
+@code{LOCPATH} environment variable.  This variable lists the
ce426f
+directories in which to search for locale definitions, separated by a
ce426f
+colon @samp{:}.
ce426f
+
ce426f
+The default path for finding locale data is system specific.  A typical
ce426f
+value for the @code{LOCPATH} default is:
ce426f
+
ce426f
+@smallexample
ce426f
+/usr/share/locale
ce426f
+@end smallexample
ce426f
+
ce426f
+The value of @code{LOCPATH} is ignored by privileged programs for
ce426f
+security reasons, and only the default directory is used.
ce426f
+
ce426f
+@node Locale Information, Formatting Numbers, Locale Names, Locales
ce426f
 @section Accessing Locale Information
ce426f
 
ce426f
 There are several ways to access locale information.  The simplest