PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/glibc/libc/Setting-the-Locale.html

https://gitlab.com/Gentio/my-pdf
HTML | 212 lines | 148 code | 17 blank | 47 comment | 0 complexity | e5d4a3a0cd429cc3281afc49829ec734 MD5 | raw file
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- This file documents the GNU C Library.
  4. This is
  5. The GNU C Library Reference Manual, for version
  6. 2.23.
  7. Copyright (C) 1993-2016 Free Software Foundation, Inc.
  8. Permission is granted to copy, distribute and/or modify this document
  9. under the terms of the GNU Free Documentation License, Version
  10. 1.3 or any later version published by the Free
  11. Software Foundation; with the Invariant Sections being "Free Software
  12. Needs Free Documentation" and "GNU Lesser General Public License",
  13. the Front-Cover texts being "A GNU Manual", and with the Back-Cover
  14. Texts as in (a) below. A copy of the license is included in the
  15. section entitled "GNU Free Documentation License".
  16. (a) The FSF's Back-Cover Text is: "You have the freedom to
  17. copy and modify this GNU manual. Buying copies from the FSF
  18. supports it in developing GNU and promoting software freedom." -->
  19. <!-- Created by GNU Texinfo 6.0, http://www.gnu.org/software/texinfo/ -->
  20. <head>
  21. <title>The GNU C Library: Setting the Locale</title>
  22. <meta name="description" content="The GNU C Library: Setting the Locale">
  23. <meta name="keywords" content="The GNU C Library: Setting the Locale">
  24. <meta name="resource-type" content="document">
  25. <meta name="distribution" content="global">
  26. <meta name="Generator" content="makeinfo">
  27. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  28. <link href="index.html#Top" rel="start" title="Top">
  29. <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
  30. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  31. <link href="Locales.html#Locales" rel="up" title="Locales">
  32. <link href="Standard-Locales.html#Standard-Locales" rel="next" title="Standard Locales">
  33. <link href="Locale-Categories.html#Locale-Categories" rel="prev" title="Locale Categories">
  34. <style type="text/css">
  35. <!--
  36. a.summary-letter {text-decoration: none}
  37. blockquote.indentedblock {margin-right: 0em}
  38. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  39. blockquote.smallquotation {font-size: smaller}
  40. div.display {margin-left: 3.2em}
  41. div.example {margin-left: 3.2em}
  42. div.lisp {margin-left: 3.2em}
  43. div.smalldisplay {margin-left: 3.2em}
  44. div.smallexample {margin-left: 3.2em}
  45. div.smalllisp {margin-left: 3.2em}
  46. kbd {font-style: oblique}
  47. pre.display {font-family: inherit}
  48. pre.format {font-family: inherit}
  49. pre.menu-comment {font-family: serif}
  50. pre.menu-preformatted {font-family: serif}
  51. pre.smalldisplay {font-family: inherit; font-size: smaller}
  52. pre.smallexample {font-size: smaller}
  53. pre.smallformat {font-family: inherit; font-size: smaller}
  54. pre.smalllisp {font-size: smaller}
  55. span.nocodebreak {white-space: nowrap}
  56. span.nolinebreak {white-space: nowrap}
  57. span.roman {font-family: serif; font-weight: normal}
  58. span.sansserif {font-family: sans-serif; font-weight: normal}
  59. ul.no-bullet {list-style: none}
  60. -->
  61. </style>
  62. </head>
  63. <body lang="en">
  64. <a name="Setting-the-Locale"></a>
  65. <div class="header">
  66. <p>
  67. Next: <a href="Standard-Locales.html#Standard-Locales" accesskey="n" rel="next">Standard Locales</a>, Previous: <a href="Locale-Categories.html#Locale-Categories" accesskey="p" rel="prev">Locale Categories</a>, Up: <a href="Locales.html#Locales" accesskey="u" rel="up">Locales</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  68. </div>
  69. <hr>
  70. <a name="How-Programs-Set-the-Locale"></a>
  71. <h3 class="section">7.4 How Programs Set the Locale</h3>
  72. <p>A C program inherits its locale environment variables when it starts up.
  73. This happens automatically. However, these variables do not
  74. automatically control the locale used by the library functions, because
  75. ISO&nbsp;C<!-- /@w --> says that all programs start by default in the standard &lsquo;<samp>C</samp>&rsquo;
  76. locale. To use the locales specified by the environment, you must call
  77. <code>setlocale</code>. Call it as follows:
  78. </p>
  79. <div class="smallexample">
  80. <pre class="smallexample">setlocale (LC_ALL, &quot;&quot;);
  81. </pre></div>
  82. <p>to select a locale based on the user choice of the appropriate
  83. environment variables.
  84. </p>
  85. <a name="index-changing-the-locale"></a>
  86. <a name="index-locale_002c-changing"></a>
  87. <p>You can also use <code>setlocale</code> to specify a particular locale, for
  88. general use or for a specific category.
  89. </p>
  90. <a name="index-locale_002eh"></a>
  91. <p>The symbols in this section are defined in the header file <samp>locale.h</samp>.
  92. </p>
  93. <dl>
  94. <dt><a name="index-setlocale"></a>Function: <em>char *</em> <strong>setlocale</strong> <em>(int <var>category</var>, const char *<var>locale</var>)</em></dt>
  95. <dd><p>Preliminary:
  96. | MT-Unsafe const:locale env
  97. | AS-Unsafe init lock heap corrupt
  98. | AC-Unsafe init corrupt lock mem fd
  99. | See <a href="POSIX-Safety-Concepts.html#POSIX-Safety-Concepts">POSIX Safety Concepts</a>.
  100. </p>
  101. <p>The function <code>setlocale</code> sets the current locale for category
  102. <var>category</var> to <var>locale</var>.
  103. </p>
  104. <p>If <var>category</var> is <code>LC_ALL</code>, this specifies the locale for all
  105. purposes. The other possible values of <var>category</var> specify an
  106. single purpose (see <a href="Locale-Categories.html#Locale-Categories">Locale Categories</a>).
  107. </p>
  108. <p>You can also use this function to find out the current locale by passing
  109. a null pointer as the <var>locale</var> argument. In this case,
  110. <code>setlocale</code> returns a string that is the name of the locale
  111. currently selected for category <var>category</var>.
  112. </p>
  113. <p>The string returned by <code>setlocale</code> can be overwritten by subsequent
  114. calls, so you should make a copy of the string (see <a href="Copying-Strings-and-Arrays.html#Copying-Strings-and-Arrays">Copying Strings and Arrays</a>) if you want to save it past any further calls to
  115. <code>setlocale</code>. (The standard library is guaranteed never to call
  116. <code>setlocale</code> itself.)
  117. </p>
  118. <p>You should not modify the string returned by <code>setlocale</code>. It might
  119. be the same string that was passed as an argument in a previous call to
  120. <code>setlocale</code>. One requirement is that the <var>category</var> must be
  121. the same in the call the string was returned and the one when the string
  122. is passed in as <var>locale</var> parameter.
  123. </p>
  124. <p>When you read the current locale for category <code>LC_ALL</code>, the value
  125. encodes the entire combination of selected locales for all categories.
  126. If you specify the same &ldquo;locale name&rdquo; with <code>LC_ALL</code> in a
  127. subsequent call to <code>setlocale</code>, it restores the same combination
  128. of locale selections.
  129. </p>
  130. <p>To be sure you can use the returned string encoding the currently selected
  131. locale at a later time, you must make a copy of the string. It is not
  132. guaranteed that the returned pointer remains valid over time.
  133. </p>
  134. <p>When the <var>locale</var> argument is not a null pointer, the string returned
  135. by <code>setlocale</code> reflects the newly-modified locale.
  136. </p>
  137. <p>If you specify an empty string for <var>locale</var>, this means to read the
  138. appropriate environment variable and use its value to select the locale
  139. for <var>category</var>.
  140. </p>
  141. <p>If a nonempty string is given for <var>locale</var>, then the locale of that
  142. name is used if possible.
  143. </p>
  144. <p>The effective locale name (either the second argument to
  145. <code>setlocale</code>, or if the argument is an empty string, the name
  146. obtained from the process environment) must be valid locale name.
  147. See <a href="Locale-Names.html#Locale-Names">Locale Names</a>.
  148. </p>
  149. <p>If you specify an invalid locale name, <code>setlocale</code> returns a null
  150. pointer and leaves the current locale unchanged.
  151. </p></dd></dl>
  152. <p>Here is an example showing how you might use <code>setlocale</code> to
  153. temporarily switch to a new locale.
  154. </p>
  155. <div class="smallexample">
  156. <pre class="smallexample">#include &lt;stddef.h&gt;
  157. #include &lt;locale.h&gt;
  158. #include &lt;stdlib.h&gt;
  159. #include &lt;string.h&gt;
  160. void
  161. with_other_locale (char *new_locale,
  162. void (*subroutine) (int),
  163. int argument)
  164. {
  165. char *old_locale, *saved_locale;
  166. /* <span class="roman">Get the name of the current locale.</span> */
  167. old_locale = setlocale (LC_ALL, NULL);
  168. /* <span class="roman">Copy the name so it won&rsquo;t be clobbered by <code>setlocale</code>.</span> */
  169. saved_locale = strdup (old_locale);
  170. if (saved_locale == NULL)
  171. fatal (&quot;Out of memory&quot;);
  172. /* <span class="roman">Now change the locale and do some stuff with it.</span> */
  173. setlocale (LC_ALL, new_locale);
  174. (*subroutine) (argument);
  175. /* <span class="roman">Restore the original locale.</span> */
  176. setlocale (LC_ALL, saved_locale);
  177. free (saved_locale);
  178. }
  179. </pre></div>
  180. <p><strong>Portability Note:</strong> Some ISO&nbsp;C<!-- /@w --> systems may define additional
  181. locale categories, and future versions of the library will do so. For
  182. portability, assume that any symbol beginning with &lsquo;<samp>LC_</samp>&rsquo; might be
  183. defined in <samp>locale.h</samp>.
  184. </p>
  185. <hr>
  186. <div class="header">
  187. <p>
  188. Next: <a href="Standard-Locales.html#Standard-Locales" accesskey="n" rel="next">Standard Locales</a>, Previous: <a href="Locale-Categories.html#Locale-Categories" accesskey="p" rel="prev">Locale Categories</a>, Up: <a href="Locales.html#Locales" accesskey="u" rel="up">Locales</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  189. </div>
  190. </body>
  191. </html>