elf: Make glibc.rtld.enable_secure ignore alias environment variables
[glibc.git] / manual / locale.texi
blob1b3f97839bb5d06899ab55db7e3047f1863ba5a1
1 @node Locales, Message Translation, Character Set Handling, Top
2 @c %MENU% The country and language can affect the behavior of library functions
3 @chapter Locales and Internationalization
5 Different countries and cultures have varying conventions for how to
6 communicate.  These conventions range from very simple ones, such as the
7 format for representing dates and times, to very complex ones, such as
8 the language spoken.
10 @cindex internationalization
11 @cindex locales
12 @dfn{Internationalization} of software means programming it to be able
13 to adapt to the user's favorite conventions.  In @w{ISO C},
14 internationalization works by means of @dfn{locales}.  Each locale
15 specifies a collection of conventions, one convention for each purpose.
16 The user chooses a set of conventions by specifying a locale (via
17 environment variables).
19 All programs inherit the chosen locale as part of their environment.
20 Provided the programs are written to obey the choice of locale, they
21 will follow the conventions preferred by the user.
23 @menu
24 * Effects of Locale::           Actions affected by the choice of
25                                  locale.
26 * Choosing Locale::             How the user specifies a locale.
27 * Locale Categories::           Different purposes for which you can
28                                  select a locale.
29 * Setting the Locale::          How a program specifies the locale
30                                  with library functions.
31 * Standard Locales::            Locale names available on all systems.
32 * Locale Names::                Format of system-specific locale names.
33 * Locale Information::          How to access the information for the locale.
34 * Formatting Numbers::          A dedicated function to format numbers.
35 * Yes-or-No Questions::         Check a Response against the locale.
36 @end menu
38 @node Effects of Locale, Choosing Locale,  , Locales
39 @section What Effects a Locale Has
41 Each locale specifies conventions for several purposes, including the
42 following:
44 @itemize @bullet
45 @item
46 What multibyte character sequences are valid, and how they are
47 interpreted (@pxref{Character Set Handling}).
49 @item
50 Classification of which characters in the local character set are
51 considered alphabetic, and upper- and lower-case conversion conventions
52 (@pxref{Character Handling}).
54 @item
55 The collating sequence for the local language and character set
56 (@pxref{Collation Functions}).
58 @item
59 Formatting of numbers and currency amounts (@pxref{General Numeric}).
61 @item
62 Formatting of dates and times (@pxref{Formatting Calendar Time}).
64 @item
65 What language to use for output, including error messages
66 (@pxref{Message Translation}).
68 @item
69 What language to use for user answers to yes-or-no questions
70 (@pxref{Yes-or-No Questions}).
72 @item
73 What language to use for more complex user input.
74 (The C library doesn't yet help you implement this.)
75 @end itemize
77 Some aspects of adapting to the specified locale are handled
78 automatically by the library subroutines.  For example, all your program
79 needs to do in order to use the collating sequence of the chosen locale
80 is to use @code{strcoll} or @code{strxfrm} to compare strings.
82 Other aspects of locales are beyond the comprehension of the library.
83 For example, the library can't automatically translate your program's
84 output messages into other languages.  The only way you can support
85 output in the user's favorite language is to program this more or less
86 by hand.  The C library provides functions to handle translations for
87 multiple languages easily.
89 This chapter discusses the mechanism by which you can modify the current
90 locale.  The effects of the current locale on specific library functions
91 are discussed in more detail in the descriptions of those functions.
93 @node Choosing Locale, Locale Categories, Effects of Locale, Locales
94 @section Choosing a Locale
96 The simplest way for the user to choose a locale is to set the
97 environment variable @code{LANG}.  This specifies a single locale to use
98 for all purposes.  For example, a user could specify a hypothetical
99 locale named @samp{espana-castellano} to use the standard conventions of
100 most of Spain.
102 The set of locales supported depends on the operating system you are
103 using, and so do their names, except that the standard locale called
104 @samp{C} or @samp{POSIX} always exist.  @xref{Locale Names}.
106 In order to force the system to always use the default locale, the
107 user can set the @code{LC_ALL} environment variable to @samp{C}.
109 @cindex combining locales
110 A user also has the option of specifying different locales for
111 different purposes---in effect, choosing a mixture of multiple
112 locales.  @xref{Locale Categories}.
114 For example, the user might specify the locale @samp{espana-castellano}
115 for most purposes, but specify the locale @samp{usa-english} for
116 currency formatting.  This might make sense if the user is a
117 Spanish-speaking American, working in Spanish, but representing monetary
118 amounts in US dollars.
120 Note that both locales @samp{espana-castellano} and @samp{usa-english},
121 like all locales, would include conventions for all of the purposes to
122 which locales apply.  However, the user can choose to use each locale
123 for a particular subset of those purposes.
125 @node Locale Categories, Setting the Locale, Choosing Locale, Locales
126 @section Locale Categories
127 @cindex categories for locales
128 @cindex locale categories
130 The purposes that locales serve are grouped into @dfn{categories}, so
131 that a user or a program can choose the locale for each category
132 independently.  Here is a table of categories; each name is both an
133 environment variable that a user can set, and a macro name that you can
134 use as the first argument to @code{setlocale}.
136 The contents of the environment variable (or the string in the second
137 argument to @code{setlocale}) has to be a valid locale name.
138 @xref{Locale Names}.
140 @vtable @code
141 @item LC_COLLATE
142 @standards{ISO, locale.h}
143 This category applies to collation of strings (functions @code{strcoll}
144 and @code{strxfrm}); see @ref{Collation Functions}.
146 @item LC_CTYPE
147 @standards{ISO, locale.h}
148 This category applies to classification and conversion of characters,
149 and to multibyte and wide characters;
150 see @ref{Character Handling}, and @ref{Character Set Handling}.
152 @item LC_MONETARY
153 @standards{ISO, locale.h}
154 This category applies to formatting monetary values; see @ref{General Numeric}.
156 @item LC_NUMERIC
157 @standards{ISO, locale.h}
158 This category applies to formatting numeric values that are not
159 monetary; see @ref{General Numeric}.
161 @item LC_TIME
162 @standards{ISO, locale.h}
163 This category applies to formatting date and time values; see
164 @ref{Formatting Calendar Time}.
166 @item LC_MESSAGES
167 @standards{XOPEN, locale.h}
168 This category applies to selecting the language used in the user
169 interface for message translation (@pxref{The Uniforum approach};
170 @pxref{Message catalogs a la X/Open})  and contains regular expressions
171 for affirmative and negative responses.
173 @item LC_ALL
174 @standards{ISO, locale.h}
175 This is not a category; it is only a macro that you can use
176 with @code{setlocale} to set a single locale for all purposes.  Setting
177 this environment variable overwrites all selections by the other
178 @code{LC_*} variables or @code{LANG}.
180 @item LANG
181 @standards{ISO, locale.h}
182 If this environment variable is defined, its value specifies the locale
183 to use for all purposes except as overridden by the variables above.
184 @end vtable
186 @vindex LANGUAGE
187 When developing the message translation functions it was felt that the
188 functionality provided by the variables above is not sufficient.  For
189 example, it should be possible to specify more than one locale name.
190 Take a Swedish user who better speaks German than English, and a program
191 whose messages are output in English by default.  It should be possible
192 to specify that the first choice of language is Swedish, the second
193 German, and if this also fails to use English.  This is
194 possible with the variable @code{LANGUAGE}.  For further description of
195 this GNU extension see @ref{Using gettextized software}.
197 @node Setting the Locale, Standard Locales, Locale Categories, Locales
198 @section How Programs Set the Locale
200 A C program inherits its locale environment variables when it starts up.
201 This happens automatically.  However, these variables do not
202 automatically control the locale used by the library functions, because
203 @w{ISO C} says that all programs start by default in the standard @samp{C}
204 locale.  To use the locales specified by the environment, you must call
205 @code{setlocale}.  Call it as follows:
207 @smallexample
208 setlocale (LC_ALL, "");
209 @end smallexample
211 @noindent
212 to select a locale based on the user choice of the appropriate
213 environment variables.
215 @cindex changing the locale
216 @cindex locale, changing
217 You can also use @code{setlocale} to specify a particular locale, for
218 general use or for a specific category.
220 @pindex locale.h
221 The symbols in this section are defined in the header file @file{locale.h}.
223 @deftypefun {char *} setlocale (int @var{category}, const char *@var{locale})
224 @standards{ISO, locale.h}
225 @safety{@prelim{}@mtunsafe{@mtasuconst{:@mtslocale{}} @mtsenv{}}@asunsafe{@asuinit{} @asulock{} @ascuheap{} @asucorrupt{}}@acunsafe{@acuinit{} @acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
226 @c Uses of the global locale object are unguarded in functions that
227 @c ought to be MT-Safe, so we're ruling out the use of this function
228 @c once threads are started.  It takes a write lock itself, but it may
229 @c return a pointer loaded from the global locale object after releasing
230 @c the lock, or before taking it.
231 @c setlocale @mtasuconst:@mtslocale @mtsenv @asuinit @ascuheap @asulock @asucorrupt @acucorrupt @acsmem @acsfd @aculock
232 @c  libc_rwlock_wrlock @asulock @aculock
233 @c  libc_rwlock_unlock @aculock
234 @c  getenv LOCPATH @mtsenv
235 @c  malloc @ascuheap @acsmem
236 @c  free @ascuheap @acsmem
237 @c  new_composite_name ok
238 @c  setdata ok
239 @c  setname ok
240 @c  _nl_find_locale @mtsenv @asuinit @ascuheap @asulock @asucorrupt @acucorrupt @acsmem @acsfd @aculock
241 @c   getenv LC_ALL and LANG @mtsenv
242 @c   _nl_load_locale_from_archive @ascuheap @acucorrupt @acsmem @acsfd
243 @c    sysconf _SC_PAGE_SIZE ok
244 @c    _nl_normalize_codeset @ascuheap @acsmem
245 @c     isalnum_l ok (C locale)
246 @c     isdigit_l ok (C locale)
247 @c     malloc @ascuheap @acsmem
248 @c     tolower_l ok (C locale)
249 @c    open_not_cancel_2 @acsfd
250 @c    fxstat64 ok
251 @c    close_not_cancel_no_status ok
252 @c    __mmap64 @acsmem
253 @c    calculate_head_size ok
254 @c    __munmap ok
255 @c    compute_hashval ok
256 @c    qsort dup @acucorrupt
257 @c     rangecmp ok
258 @c    malloc @ascuheap @acsmem
259 @c    strdup @ascuheap @acsmem
260 @c    _nl_intern_locale_data @ascuheap @acsmem
261 @c     malloc @ascuheap @acsmem
262 @c     free @ascuheap @acsmem
263 @c   _nl_expand_alias @ascuheap @asulock @acsmem @acsfd @aculock
264 @c    libc_lock_lock @asulock @aculock
265 @c    bsearch ok
266 @c     alias_compare ok
267 @c      strcasecmp ok
268 @c    read_alias_file @ascuheap @asulock @acsmem @acsfd @aculock
269 @c     fopen @ascuheap @asulock @acsmem @acsfd @aculock
270 @c     fsetlocking ok
271 @c     feof_unlocked ok
272 @c     fgets_unlocked ok
273 @c     isspace ok (locale mutex is locked)
274 @c     extend_alias_table @ascuheap @acsmem
275 @c      realloc @ascuheap @acsmem
276 @c     realloc @ascuheap @acsmem
277 @c     fclose @ascuheap @asulock @acsmem @acsfd @aculock
278 @c      alias_compare dup
279 @c    libc_lock_unlock @aculock
280 @c   _nl_explode_name @ascuheap @acsmem
281 @c    _nl_find_language ok
282 @c    _nl_normalize_codeset dup @ascuheap @acsmem
283 @c   _nl_make_l10nflist @ascuheap @acsmem
284 @c    malloc @ascuheap @acsmem
285 @c    free @ascuheap @acsmem
286 @c    __argz_stringify ok
287 @c    __argz_count ok
288 @c    __argz_next ok
289 @c   _nl_load_locale @ascuheap @acsmem @acsfd
290 @c    open_not_cancel_2 @acsfd
291 @c    __fxstat64 ok
292 @c    close_not_cancel_no_status ok
293 @c    mmap @acsmem
294 @c    malloc @ascuheap @acsmem
295 @c    read_not_cancel ok
296 @c    free @ascuheap @acsmem
297 @c    _nl_intern_locale_data dup @ascuheap @acsmem
298 @c    munmap ok
299 @c   __gconv_compare_alias @asuinit @ascuheap @asucorrupt @asulock @acsmem@acucorrupt @acsfd @aculock
300 @c    __gconv_read_conf @asuinit @ascuheap @asucorrupt @asulock @acsmem@acucorrupt @acsfd @aculock
301 @c     (libc_once-initializes gconv_cache and gconv_path_envvar; they're
302 @c      never modified afterwards)
303 @c     __gconv_load_cache @ascuheap @acsmem @acsfd
304 @c      getenv GCONV_PATH @mtsenv
305 @c      open_not_cancel @acsfd
306 @c      __fxstat64 ok
307 @c      close_not_cancel_no_status ok
308 @c      mmap @acsmem
309 @c      malloc @ascuheap @acsmem
310 @c      __read ok
311 @c      free @ascuheap @acsmem
312 @c      munmap ok
313 @c     __gconv_get_path @asulock @ascuheap @aculock @acsmem @acsfd
314 @c      getcwd @ascuheap @acsmem @acsfd
315 @c      libc_lock_lock @asulock @aculock
316 @c      malloc @ascuheap @acsmem
317 @c      strtok_r ok
318 @c      libc_lock_unlock @aculock
319 @c     read_conf_file @ascuheap @asucorrupt @asulock @acsmem @acucorrupt @acsfd @aculock
320 @c      fopen @ascuheap @asulock @acsmem @acsfd @aculock
321 @c      fsetlocking ok
322 @c      feof_unlocked ok
323 @c      getdelim @ascuheap @asucorrupt @acsmem @acucorrupt
324 @c      isspace_l ok (C locale)
325 @c      add_alias
326 @c       isspace_l ok (C locale)
327 @c       toupper_l ok (C locale)
328 @c       add_alias2 dup @ascuheap @acucorrupt @acsmem
329 @c      add_module @ascuheap @acsmem
330 @c       isspace_l ok (C locale)
331 @c       toupper_l ok (C locale)
332 @c       strtol ok (@mtslocale but we hold the locale lock)
333 @c       tfind __gconv_alias_db ok
334 @c        __gconv_alias_compare dup ok
335 @c       calloc @ascuheap @acsmem
336 @c       insert_module dup @ascuheap
337 @c     __tfind ok (because the tree is read only by then)
338 @c      __gconv_alias_compare dup ok
339 @c     insert_module @ascuheap
340 @c      free @ascuheap
341 @c     add_alias2 @ascuheap @acucorrupt @acsmem
342 @c      detect_conflict ok, reads __gconv_modules_db
343 @c      malloc @ascuheap @acsmem
344 @c      tsearch __gconv_alias_db @ascuheap @acucorrupt @acsmem [exclusive tree, no @mtsrace]
345 @c       __gconv_alias_compare ok
346 @c      free @ascuheap
347 @c    __gconv_compare_alias_cache ok
348 @c     find_module_idx ok
349 @c    do_lookup_alias ok
350 @c     __tfind ok (because the tree is read only by then)
351 @c      __gconv_alias_compare ok
352 @c   strndup @ascuheap @acsmem
353 @c   strcasecmp_l ok (C locale)
354 The function @code{setlocale} sets the current locale for category
355 @var{category} to @var{locale}.
357 If @var{category} is @code{LC_ALL}, this specifies the locale for all
358 purposes.  The other possible values of @var{category} specify a
359 single purpose (@pxref{Locale Categories}).
361 You can also use this function to find out the current locale by passing
362 a null pointer as the @var{locale} argument.  In this case,
363 @code{setlocale} returns a string that is the name of the locale
364 currently selected for category @var{category}.
366 The string returned by @code{setlocale} can be overwritten by subsequent
367 calls, so you should make a copy of the string (@pxref{Copying Strings
368 and Arrays}) if you want to save it past any further calls to
369 @code{setlocale}.  (The standard library is guaranteed never to call
370 @code{setlocale} itself.)
372 You should not modify the string returned by @code{setlocale}.  It might
373 be the same string that was passed as an argument in a previous call to
374 @code{setlocale}.  One requirement is that the @var{category} must be
375 the same in the call the string was returned and the one when the string
376 is passed in as @var{locale} parameter.
378 When you read the current locale for category @code{LC_ALL}, the value
379 encodes the entire combination of selected locales for all categories.
380 If you specify the same ``locale name'' with @code{LC_ALL} in a
381 subsequent call to @code{setlocale}, it restores the same combination
382 of locale selections.
384 To be sure you can use the returned string encoding the currently selected
385 locale at a later time, you must make a copy of the string.  It is not
386 guaranteed that the returned pointer remains valid over time.
388 When the @var{locale} argument is not a null pointer, the string returned
389 by @code{setlocale} reflects the newly-modified locale.
391 If you specify an empty string for @var{locale}, this means to read the
392 appropriate environment variable and use its value to select the locale
393 for @var{category}.
395 If a nonempty string is given for @var{locale}, then the locale of that
396 name is used if possible.
398 The effective locale name (either the second argument to
399 @code{setlocale}, or if the argument is an empty string, the name
400 obtained from the process environment) must be a valid locale name.
401 @xref{Locale Names}.
403 If you specify an invalid locale name, @code{setlocale} returns a null
404 pointer and leaves the current locale unchanged.
405 @end deftypefun
407 Here is an example showing how you might use @code{setlocale} to
408 temporarily switch to a new locale.
410 @smallexample
411 #include <stddef.h>
412 #include <locale.h>
413 #include <stdlib.h>
414 #include <string.h>
416 void
417 with_other_locale (char *new_locale,
418                    void (*subroutine) (int),
419                    int argument)
421   char *old_locale, *saved_locale;
423   /* @r{Get the name of the current locale.}  */
424   old_locale = setlocale (LC_ALL, NULL);
426   /* @r{Copy the name so it won't be clobbered by @code{setlocale}.} */
427   saved_locale = strdup (old_locale);
428   if (saved_locale == NULL)
429     fatal ("Out of memory");
431   /* @r{Now change the locale and do some stuff with it.} */
432   setlocale (LC_ALL, new_locale);
433   (*subroutine) (argument);
435   /* @r{Restore the original locale.} */
436   setlocale (LC_ALL, saved_locale);
437   free (saved_locale);
439 @end smallexample
441 @strong{Portability Note:} Some @w{ISO C} systems may define additional
442 locale categories, and future versions of the library will do so.  For
443 portability, assume that any symbol beginning with @samp{LC_} might be
444 defined in @file{locale.h}.
446 @node Standard Locales, Locale Names, Setting the Locale, Locales
447 @section Standard Locales
449 The only locale names you can count on finding on all operating systems
450 are these three standard ones:
452 @table @code
453 @item "C"
454 This is the standard C locale.  The attributes and behavior it provides
455 are specified in the @w{ISO C} standard.  When your program starts up, it
456 initially uses this locale by default.
458 @item "POSIX"
459 This is the standard POSIX locale.  Currently, it is an alias for the
460 standard C locale.
462 @item ""
463 The empty name says to select a locale based on environment variables.
464 @xref{Locale Categories}.
465 @end table
467 Defining and installing named locales is normally a responsibility of
468 the system administrator at your site (or the person who installed
469 @theglibc{}).  It is also possible for the user to create private
470 locales.  All this will be discussed later when describing the tool to
471 do so.
472 @comment (@pxref{Building Locale Files}).
474 If your program needs to use something other than the @samp{C} locale,
475 it will be more portable if you use whatever locale the user specifies
476 with the environment, rather than trying to specify some non-standard
477 locale explicitly by name.  Remember, different machines might have
478 different sets of locales installed.
480 @node Locale Names, Locale Information, Standard Locales, Locales
481 @section Locale Names
483 The following command prints a list of locales supported by the
484 system:
486 @pindex locale
487 @smallexample
488   locale -a
489 @end smallexample
491 @strong{Portability Note:} With the notable exception of the standard
492 locale names @samp{C} and @samp{POSIX}, locale names are
493 system-specific.
495 Most locale names follow XPG syntax and consist of up to four parts:
497 @smallexample
498 @var{language}[_@var{territory}[.@var{codeset}]][@@@var{modifier}]
499 @end smallexample
501 Beside the first part, all of them are allowed to be missing.  If the
502 full specified locale is not found, less specific ones are looked for.
503 The various parts will be stripped off, in the following order:
505 @enumerate
506 @item
507 codeset
508 @item
509 normalized codeset
510 @item
511 territory
512 @item
513 modifier
514 @end enumerate
516 For example, the locale name @samp{de_AT.iso885915@@euro} denotes a
517 German-language locale for use in Austria, using the ISO-8859-15
518 (Latin-9) character set, and with the Euro as the currency symbol.
520 In addition to locale names which follow XPG syntax, systems may
521 provide aliases such as @samp{german}.  Both categories of names must
522 not contain the slash character @samp{/}.
524 If the locale name starts with a slash @samp{/}, it is treated as a
525 path relative to the configured locale directories; see @code{LOCPATH}
526 below.  The specified path must not contain a component @samp{..}, or
527 the name is invalid, and @code{setlocale} will fail.
529 @strong{Portability Note:} POSIX suggests that if a locale name starts
530 with a slash @samp{/}, it is resolved as an absolute path.  However,
531 @theglibc{} treats it as a relative path under the directories listed
532 in @code{LOCPATH} (or the default locale directory if @code{LOCPATH}
533 is unset).
535 Locale names which are longer than an implementation-defined limit are
536 invalid and cause @code{setlocale} to fail.
538 As a special case, locale names used with @code{LC_ALL} can combine
539 several locales, reflecting different locale settings for different
540 categories.  For example, you might want to use a U.S. locale with ISO
541 A4 paper format, so you set @code{LANG} to @samp{en_US.UTF-8}, and
542 @code{LC_PAPER} to @samp{de_DE.UTF-8}.  In this case, the
543 @code{LC_ALL}-style combined locale name is
545 @smallexample
546 LC_CTYPE=en_US.UTF-8;LC_TIME=en_US.UTF-8;LC_PAPER=de_DE.UTF-8;@dots{}
547 @end smallexample
549 followed by other category settings not shown here.
551 @vindex LOCPATH
552 The path used for finding locale data can be set using the
553 @code{LOCPATH} environment variable.  This variable lists the
554 directories in which to search for locale definitions, separated by a
555 colon @samp{:}.
557 The default path for finding locale data is system specific.  A typical
558 value for the @code{LOCPATH} default is:
560 @smallexample
561 /usr/share/locale
562 @end smallexample
564 The value of @code{LOCPATH} is ignored by privileged programs for
565 security reasons, and only the default directory is used.
567 @node Locale Information, Formatting Numbers, Locale Names, Locales
568 @section Accessing Locale Information
570 There are several ways to access locale information.  The simplest
571 way is to let the C library itself do the work.  Several of the
572 functions in this library implicitly access the locale data, and use
573 what information is provided by the currently selected locale.  This is
574 how the locale model is meant to work normally.
576 As an example take the @code{strftime} function, which is meant to nicely
577 format date and time information (@pxref{Formatting Calendar Time}).
578 Part of the standard information contained in the @code{LC_TIME}
579 category is the names of the months.  Instead of requiring the
580 programmer to take care of providing the translations the
581 @code{strftime} function does this all by itself.  @code{%A}
582 in the format string is replaced by the appropriate weekday
583 name of the locale currently selected by @code{LC_TIME}.  This is an
584 easy example, and wherever possible functions do things automatically
585 in this way.
587 But there are quite often situations when there is simply no function
588 to perform the task, or it is simply not possible to do the work
589 automatically.  For these cases it is necessary to access the
590 information in the locale directly.  To do this the C library provides
591 two functions: @code{localeconv} and @code{nl_langinfo}.  The former is
592 part of @w{ISO C} and therefore portable, but has a brain-damaged
593 interface.  The second is part of the Unix interface and is portable in
594 as far as the system follows the Unix standards.
596 @menu
597 * The Lame Way to Locale Data::   ISO C's @code{localeconv}.
598 * The Elegant and Fast Way::      X/Open's @code{nl_langinfo}.
599 @end menu
601 @node The Lame Way to Locale Data, The Elegant and Fast Way, ,Locale Information
602 @subsection @code{localeconv}: It is portable but @dots{}
604 Together with the @code{setlocale} function the @w{ISO C} people
605 invented the @code{localeconv} function.  It is a masterpiece of poor
606 design.  It is expensive to use, not extensible, and not generally
607 usable as it provides access to only @code{LC_MONETARY} and
608 @code{LC_NUMERIC} related information.  Nevertheless, if it is
609 applicable to a given situation it should be used since it is very
610 portable.  The function @code{strfmon} formats monetary amounts
611 according to the selected locale using this information.
612 @pindex locale.h
613 @cindex monetary value formatting
614 @cindex numeric value formatting
616 @deftypefun {struct lconv *} localeconv (void)
617 @standards{ISO, locale.h}
618 @safety{@prelim{}@mtunsafe{@mtasurace{:localeconv} @mtslocale{}}@asunsafe{}@acsafe{}}
619 @c This function reads from multiple components of the locale object,
620 @c without synchronization, while writing to the static buffer it uses
621 @c as the return value.
622 The @code{localeconv} function returns a pointer to a structure whose
623 components contain information about how numeric and monetary values
624 should be formatted in the current locale.
626 You should not modify the structure or its contents.  The structure might
627 be overwritten by subsequent calls to @code{localeconv}, or by calls to
628 @code{setlocale}, but no other function in the library overwrites this
629 value.
630 @end deftypefun
632 @deftp {Data Type} {struct lconv}
633 @standards{ISO, locale.h}
634 @code{localeconv}'s return value is of this data type.  Its elements are
635 described in the following subsections.
636 @end deftp
638 If a member of the structure @code{struct lconv} has type @code{char},
639 and the value is @code{CHAR_MAX}, it means that the current locale has
640 no value for that parameter.
642 @menu
643 * General Numeric::             Parameters for formatting numbers and
644                                  currency amounts.
645 * Currency Symbol::             How to print the symbol that identifies an
646                                  amount of money (e.g. @samp{$}).
647 * Sign of Money Amount::        How to print the (positive or negative) sign
648                                  for a monetary amount, if one exists.
649 @end menu
651 @node General Numeric, Currency Symbol, , The Lame Way to Locale Data
652 @subsubsection Generic Numeric Formatting Parameters
654 These are the standard members of @code{struct lconv}; there may be
655 others.
657 @table @code
658 @item char *decimal_point
659 @itemx char *mon_decimal_point
660 These are the decimal-point separators used in formatting non-monetary
661 and monetary quantities, respectively.  In the @samp{C} locale, the
662 value of @code{decimal_point} is @code{"."}, and the value of
663 @code{mon_decimal_point} is @code{""}.
664 @cindex decimal-point separator
666 @item char *thousands_sep
667 @itemx char *mon_thousands_sep
668 These are the separators used to delimit groups of digits to the left of
669 the decimal point in formatting non-monetary and monetary quantities,
670 respectively.  In the @samp{C} locale, both members have a value of
671 @code{""} (the empty string).
673 @item char *grouping
674 @itemx char *mon_grouping
675 These are strings that specify how to group the digits to the left of
676 the decimal point.  @code{grouping} applies to non-monetary quantities
677 and @code{mon_grouping} applies to monetary quantities.  Use either
678 @code{thousands_sep} or @code{mon_thousands_sep} to separate the digit
679 groups.
680 @cindex grouping of digits
682 Each member of these strings is to be interpreted as an integer value of
683 type @code{char}.  Successive numbers (from left to right) give the
684 sizes of successive groups (from right to left, starting at the decimal
685 point.)  The last member is either @code{0}, in which case the previous
686 member is used over and over again for all the remaining groups, or
687 @code{CHAR_MAX}, in which case there is no more grouping---or, put
688 another way, any remaining digits form one large group without
689 separators.
691 For example, if @code{grouping} is @code{"\04\03\02"}, the correct
692 grouping for the number @code{123456787654321} is @samp{12}, @samp{34},
693 @samp{56}, @samp{78}, @samp{765}, @samp{4321}.  This uses a group of 4
694 digits at the end, preceded by a group of 3 digits, preceded by groups
695 of 2 digits (as many as needed).  With a separator of @samp{,}, the
696 number would be printed as @samp{12,34,56,78,765,4321}.
698 A value of @code{"\03"} indicates repeated groups of three digits, as
699 normally used in the U.S.
701 In the standard @samp{C} locale, both @code{grouping} and
702 @code{mon_grouping} have a value of @code{""}.  This value specifies no
703 grouping at all.
705 @item char int_frac_digits
706 @itemx char frac_digits
707 These are small integers indicating how many fractional digits (to the
708 right of the decimal point) should be displayed in a monetary value in
709 international and local formats, respectively.  (Most often, both
710 members have the same value.)
712 In the standard @samp{C} locale, both of these members have the value
713 @code{CHAR_MAX}, meaning ``unspecified''.  The ISO standard doesn't say
714 what to do when you find this value; we recommend printing no
715 fractional digits.  (This locale also specifies the empty string for
716 @code{mon_decimal_point}, so printing any fractional digits would be
717 confusing!)
718 @end table
720 @node Currency Symbol, Sign of Money Amount, General Numeric, The Lame Way to Locale Data
721 @subsubsection Printing the Currency Symbol
722 @cindex currency symbols
724 These members of the @code{struct lconv} structure specify how to print
725 the symbol to identify a monetary value---the international analog of
726 @samp{$} for US dollars.
728 Each country has two standard currency symbols.  The @dfn{local currency
729 symbol} is used commonly within the country, while the
730 @dfn{international currency symbol} is used internationally to refer to
731 that country's currency when it is necessary to indicate the country
732 unambiguously.
734 For example, many countries use the dollar as their monetary unit, and
735 when dealing with international currencies it's important to specify
736 that one is dealing with (say) Canadian dollars instead of U.S. dollars
737 or Australian dollars.  But when the context is known to be Canada,
738 there is no need to make this explicit---dollar amounts are implicitly
739 assumed to be in Canadian dollars.
741 @table @code
742 @item char *currency_symbol
743 The local currency symbol for the selected locale.
745 In the standard @samp{C} locale, this member has a value of @code{""}
746 (the empty string), meaning ``unspecified''.  The ISO standard doesn't
747 say what to do when you find this value; we recommend you simply print
748 the empty string as you would print any other string pointed to by this
749 variable.
751 @item char *int_curr_symbol
752 The international currency symbol for the selected locale.
754 The value of @code{int_curr_symbol} should normally consist of a
755 three-letter abbreviation determined by the international standard
756 @cite{ISO 4217 Codes for the Representation of Currency and Funds},
757 followed by a one-character separator (often a space).
759 In the standard @samp{C} locale, this member has a value of @code{""}
760 (the empty string), meaning ``unspecified''.  We recommend you simply print
761 the empty string as you would print any other string pointed to by this
762 variable.
764 @item char p_cs_precedes
765 @itemx char n_cs_precedes
766 @itemx char int_p_cs_precedes
767 @itemx char int_n_cs_precedes
768 These members are @code{1} if the @code{currency_symbol} or
769 @code{int_curr_symbol} strings should precede the value of a monetary
770 amount, or @code{0} if the strings should follow the value.  The
771 @code{p_cs_precedes} and @code{int_p_cs_precedes} members apply to
772 positive amounts (or zero), and the @code{n_cs_precedes} and
773 @code{int_n_cs_precedes} members apply to negative amounts.
775 In the standard @samp{C} locale, all of these members have a value of
776 @code{CHAR_MAX}, meaning ``unspecified''.  The ISO standard doesn't say
777 what to do when you find this value.  We recommend printing the
778 currency symbol before the amount, which is right for most countries.
779 In other words, treat all nonzero values alike in these members.
781 The members with the @code{int_} prefix apply to the
782 @code{int_curr_symbol} while the other two apply to
783 @code{currency_symbol}.
785 @item char p_sep_by_space
786 @itemx char n_sep_by_space
787 @itemx char int_p_sep_by_space
788 @itemx char int_n_sep_by_space
789 These members are @code{1} if a space should appear between the
790 @code{currency_symbol} or @code{int_curr_symbol} strings and the
791 amount, or @code{0} if no space should appear.  The
792 @code{p_sep_by_space} and @code{int_p_sep_by_space} members apply to
793 positive amounts (or zero), and the @code{n_sep_by_space} and
794 @code{int_n_sep_by_space} members apply to negative amounts.
796 In the standard @samp{C} locale, all of these members have a value of
797 @code{CHAR_MAX}, meaning ``unspecified''.  The ISO standard doesn't say
798 what you should do when you find this value; we suggest you treat it as
799 1 (print a space).  In other words, treat all nonzero values alike in
800 these members.
802 The members with the @code{int_} prefix apply to the
803 @code{int_curr_symbol} while the other two apply to
804 @code{currency_symbol}.  There is one specialty with the
805 @code{int_curr_symbol}, though.  Since all legal values contain a space
806 at the end of the string one either prints this space (if the currency
807 symbol must appear in front and must be separated) or one has to avoid
808 printing this character at all (especially when at the end of the
809 string).
810 @end table
812 @node Sign of Money Amount, , Currency Symbol, The Lame Way to Locale Data
813 @subsubsection Printing the Sign of a Monetary Amount
815 These members of the @code{struct lconv} structure specify how to print
816 the sign (if any) of a monetary value.
818 @table @code
819 @item char *positive_sign
820 @itemx char *negative_sign
821 These are strings used to indicate positive (or zero) and negative
822 monetary quantities, respectively.
824 In the standard @samp{C} locale, both of these members have a value of
825 @code{""} (the empty string), meaning ``unspecified''.
827 The ISO standard doesn't say what to do when you find this value; we
828 recommend printing @code{positive_sign} as you find it, even if it is
829 empty.  For a negative value, print @code{negative_sign} as you find it
830 unless both it and @code{positive_sign} are empty, in which case print
831 @samp{-} instead.  (Failing to indicate the sign at all seems rather
832 unreasonable.)
834 @item char p_sign_posn
835 @itemx char n_sign_posn
836 @itemx char int_p_sign_posn
837 @itemx char int_n_sign_posn
838 These members are small integers that indicate how to
839 position the sign for nonnegative and negative monetary quantities,
840 respectively.  (The string used for the sign is what was specified with
841 @code{positive_sign} or @code{negative_sign}.)  The possible values are
842 as follows:
844 @table @code
845 @item 0
846 The currency symbol and quantity should be surrounded by parentheses.
848 @item 1
849 Print the sign string before the quantity and currency symbol.
851 @item 2
852 Print the sign string after the quantity and currency symbol.
854 @item 3
855 Print the sign string right before the currency symbol.
857 @item 4
858 Print the sign string right after the currency symbol.
860 @item CHAR_MAX
861 ``Unspecified''.  Both members have this value in the standard
862 @samp{C} locale.
863 @end table
865 The ISO standard doesn't say what you should do when the value is
866 @code{CHAR_MAX}.  We recommend you print the sign after the currency
867 symbol.
869 The members with the @code{int_} prefix apply to the
870 @code{int_curr_symbol} while the other two apply to
871 @code{currency_symbol}.
872 @end table
874 @node The Elegant and Fast Way, , The Lame Way to Locale Data, Locale Information
875 @subsection Pinpoint Access to Locale Data
877 When writing the X/Open Portability Guide the authors realized that the
878 @code{localeconv} function is not enough to provide reasonable access to
879 locale information.  The information which was meant to be available
880 in the locale (as later specified in the POSIX.1 standard) requires more
881 ways to access it.  Therefore the @code{nl_langinfo} function
882 was introduced.
884 @deftypefun {char *} nl_langinfo (nl_item @var{item})
885 @standards{XOPEN, langinfo.h}
886 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
887 @c It calls _nl_langinfo_l with the current locale, which returns a
888 @c pointer into constant strings defined in locale data structures.
889 The @code{nl_langinfo} function can be used to access individual
890 elements of the locale categories.  Unlike the @code{localeconv}
891 function, which returns all the information, @code{nl_langinfo}
892 lets the caller select what information it requires.  This is very
893 fast and it is not a problem to call this function multiple times.
895 A second advantage is that in addition to the numeric and monetary
896 formatting information, information from the
897 @code{LC_TIME} and @code{LC_MESSAGES} categories is available.
899 @pindex langinfo.h
900 The type @code{nl_item} is defined in @file{nl_types.h}.  The argument
901 @var{item} is a numeric value defined in the header @file{langinfo.h}.
902 The X/Open standard defines the following values:
904 @vtable @code
905 @item CODESET
906 @code{nl_langinfo} returns a string with the name of the coded character
907 set used in the selected locale.
909 @item ABDAY_1
910 @itemx ABDAY_2
911 @itemx ABDAY_3
912 @itemx ABDAY_4
913 @itemx ABDAY_5
914 @itemx ABDAY_6
915 @itemx ABDAY_7
916 @code{nl_langinfo} returns the abbreviated weekday name.  @code{ABDAY_1}
917 corresponds to Sunday.
918 @item DAY_1
919 @itemx DAY_2
920 @itemx DAY_3
921 @itemx DAY_4
922 @itemx DAY_5
923 @itemx DAY_6
924 @itemx DAY_7
925 Similar to @code{ABDAY_1}, etc.,@: but here the return value is the
926 unabbreviated weekday name.
927 @item ABMON_1
928 @itemx ABMON_2
929 @itemx ABMON_3
930 @itemx ABMON_4
931 @itemx ABMON_5
932 @itemx ABMON_6
933 @itemx ABMON_7
934 @itemx ABMON_8
935 @itemx ABMON_9
936 @itemx ABMON_10
937 @itemx ABMON_11
938 @itemx ABMON_12
939 The return value is the abbreviated name of the month, in the
940 grammatical form used when the month forms part of a complete date.
941 @code{ABMON_1} corresponds to January.
942 @item MON_1
943 @itemx MON_2
944 @itemx MON_3
945 @itemx MON_4
946 @itemx MON_5
947 @itemx MON_6
948 @itemx MON_7
949 @itemx MON_8
950 @itemx MON_9
951 @itemx MON_10
952 @itemx MON_11
953 @itemx MON_12
954 Similar to @code{ABMON_1}, etc.,@: but here the month names are not
955 abbreviated.  Here the first value @code{MON_1} also corresponds to
956 January.
957 @item ALTMON_1
958 @itemx ALTMON_2
959 @itemx ALTMON_3
960 @itemx ALTMON_4
961 @itemx ALTMON_5
962 @itemx ALTMON_6
963 @itemx ALTMON_7
964 @itemx ALTMON_8
965 @itemx ALTMON_9
966 @itemx ALTMON_10
967 @itemx ALTMON_11
968 @itemx ALTMON_12
969 Similar to @code{MON_1}, etc.,@: but here the month names are in the
970 grammatical form used when the month is named by itself.  The
971 @code{strftime} functions use these month names for the conversion
972 specifier @code{%OB} (@pxref{Formatting Calendar Time}).
974 Note that not all languages need two different forms of the month names,
975 so the strings returned for @code{MON_@dots{}} and @code{ALTMON_@dots{}}
976 may or may not be the same, depending on the locale.
978 @strong{NB:} @code{ABALTMON_@dots{}} constants corresponding to the
979 @code{%Ob} conversion specifier are not currently provided, but are
980 expected to be in a future release.  In the meantime, it is possible
981 to use @code{_NL_ABALTMON_@dots{}}.
982 @item AM_STR
983 @itemx PM_STR
984 The return values are strings which can be used in the representation of time
985 as an hour from 1 to 12 plus an am/pm specifier.
987 Note that in locales which do not use this time representation
988 these strings might be empty, in which case the am/pm format
989 cannot be used at all.
990 @item D_T_FMT
991 The return value can be used as a format string for @code{strftime} to
992 represent time and date in a locale-specific way.
993 @item D_FMT
994 The return value can be used as a format string for @code{strftime} to
995 represent a date in a locale-specific way.
996 @item T_FMT
997 The return value can be used as a format string for @code{strftime} to
998 represent time in a locale-specific way.
999 @item T_FMT_AMPM
1000 The return value can be used as a format string for @code{strftime} to
1001 represent time in the am/pm format.
1003 Note that if the am/pm format does not make any sense for the
1004 selected locale, the return value might be the same as the one for
1005 @code{T_FMT}.
1006 @item ERA
1007 The return value represents the era used in the current locale.
1009 Most locales do not define this value.  An example of a locale which
1010 does define this value is the Japanese one.  In Japan, the traditional
1011 representation of dates includes the name of the era corresponding to
1012 the then-emperor's reign.
1014 Normally it should not be necessary to use this value directly.
1015 Specifying the @code{E} modifier in their format strings causes the
1016 @code{strftime} functions to use this information.  The format of the
1017 returned string is not specified, and therefore you should not assume
1018 knowledge of it on different systems.
1019 @item ERA_YEAR
1020 The return value gives the year in the relevant era of the locale.
1021 As for @code{ERA} it should not be necessary to use this value directly.
1022 @item ERA_D_T_FMT
1023 This return value can be used as a format string for @code{strftime} to
1024 represent dates and times in a locale-specific era-based way.
1025 @item ERA_D_FMT
1026 This return value can be used as a format string for @code{strftime} to
1027 represent a date in a locale-specific era-based way.
1028 @item ERA_T_FMT
1029 This return value can be used as a format string for @code{strftime} to
1030 represent time in a locale-specific era-based way.
1031 @item ALT_DIGITS
1032 The return value is a representation of up to @math{100} values used to
1033 represent the values @math{0} to @math{99}.  As for @code{ERA} this
1034 value is not intended to be used directly, but instead indirectly
1035 through the @code{strftime} function.  When the modifier @code{O} is
1036 used in a format which would otherwise use numerals to represent hours,
1037 minutes, seconds, weekdays, months, or weeks, the appropriate value for
1038 the locale is used instead.
1039 @item INT_CURR_SYMBOL
1040 The same as the value returned by @code{localeconv} in the
1041 @code{int_curr_symbol} element of the @code{struct lconv}.
1042 @item CURRENCY_SYMBOL
1043 @itemx CRNCYSTR
1044 The same as the value returned by @code{localeconv} in the
1045 @code{currency_symbol} element of the @code{struct lconv}.
1047 @code{CRNCYSTR} is a deprecated alias still required by Unix98.
1048 @item MON_DECIMAL_POINT
1049 The same as the value returned by @code{localeconv} in the
1050 @code{mon_decimal_point} element of the @code{struct lconv}.
1051 @item MON_THOUSANDS_SEP
1052 The same as the value returned by @code{localeconv} in the
1053 @code{mon_thousands_sep} element of the @code{struct lconv}.
1054 @item MON_GROUPING
1055 The same as the value returned by @code{localeconv} in the
1056 @code{mon_grouping} element of the @code{struct lconv}.
1057 @item POSITIVE_SIGN
1058 The same as the value returned by @code{localeconv} in the
1059 @code{positive_sign} element of the @code{struct lconv}.
1060 @item NEGATIVE_SIGN
1061 The same as the value returned by @code{localeconv} in the
1062 @code{negative_sign} element of the @code{struct lconv}.
1063 @item INT_FRAC_DIGITS
1064 The same as the value returned by @code{localeconv} in the
1065 @code{int_frac_digits} element of the @code{struct lconv}.
1066 @item FRAC_DIGITS
1067 The same as the value returned by @code{localeconv} in the
1068 @code{frac_digits} element of the @code{struct lconv}.
1069 @item P_CS_PRECEDES
1070 The same as the value returned by @code{localeconv} in the
1071 @code{p_cs_precedes} element of the @code{struct lconv}.
1072 @item P_SEP_BY_SPACE
1073 The same as the value returned by @code{localeconv} in the
1074 @code{p_sep_by_space} element of the @code{struct lconv}.
1075 @item N_CS_PRECEDES
1076 The same as the value returned by @code{localeconv} in the
1077 @code{n_cs_precedes} element of the @code{struct lconv}.
1078 @item N_SEP_BY_SPACE
1079 The same as the value returned by @code{localeconv} in the
1080 @code{n_sep_by_space} element of the @code{struct lconv}.
1081 @item P_SIGN_POSN
1082 The same as the value returned by @code{localeconv} in the
1083 @code{p_sign_posn} element of the @code{struct lconv}.
1084 @item N_SIGN_POSN
1085 The same as the value returned by @code{localeconv} in the
1086 @code{n_sign_posn} element of the @code{struct lconv}.
1088 @item INT_P_CS_PRECEDES
1089 The same as the value returned by @code{localeconv} in the
1090 @code{int_p_cs_precedes} element of the @code{struct lconv}.
1091 @item INT_P_SEP_BY_SPACE
1092 The same as the value returned by @code{localeconv} in the
1093 @code{int_p_sep_by_space} element of the @code{struct lconv}.
1094 @item INT_N_CS_PRECEDES
1095 The same as the value returned by @code{localeconv} in the
1096 @code{int_n_cs_precedes} element of the @code{struct lconv}.
1097 @item INT_N_SEP_BY_SPACE
1098 The same as the value returned by @code{localeconv} in the
1099 @code{int_n_sep_by_space} element of the @code{struct lconv}.
1100 @item INT_P_SIGN_POSN
1101 The same as the value returned by @code{localeconv} in the
1102 @code{int_p_sign_posn} element of the @code{struct lconv}.
1103 @item INT_N_SIGN_POSN
1104 The same as the value returned by @code{localeconv} in the
1105 @code{int_n_sign_posn} element of the @code{struct lconv}.
1107 @item DECIMAL_POINT
1108 @itemx RADIXCHAR
1109 The same as the value returned by @code{localeconv} in the
1110 @code{decimal_point} element of the @code{struct lconv}.
1112 The name @code{RADIXCHAR} is a deprecated alias still used in Unix98.
1113 @item THOUSANDS_SEP
1114 @itemx THOUSEP
1115 The same as the value returned by @code{localeconv} in the
1116 @code{thousands_sep} element of the @code{struct lconv}.
1118 The name @code{THOUSEP} is a deprecated alias still used in Unix98.
1119 @item GROUPING
1120 The same as the value returned by @code{localeconv} in the
1121 @code{grouping} element of the @code{struct lconv}.
1122 @item YESEXPR
1123 The return value is a regular expression which can be used with the
1124 @code{regex} function to recognize a positive response to a yes/no
1125 question.  @Theglibc{} provides the @code{rpmatch} function for
1126 easier handling in applications.
1127 @item NOEXPR
1128 The return value is a regular expression which can be used with the
1129 @code{regex} function to recognize a negative response to a yes/no
1130 question.
1131 @item YESSTR
1132 The return value is a locale-specific translation of the positive response
1133 to a yes/no question.
1135 Using this value is deprecated since it is a very special case of
1136 message translation, and is better handled by the message
1137 translation functions (@pxref{Message Translation}).
1139 The use of this symbol is deprecated.  Instead message translation
1140 should be used.
1141 @item NOSTR
1142 The return value is a locale-specific translation of the negative response
1143 to a yes/no question.  What is said for @code{YESSTR} is also true here.
1145 The use of this symbol is deprecated.  Instead message translation
1146 should be used.
1147 @end vtable
1149 The file @file{langinfo.h} defines a lot more symbols but none of them
1150 are official.  Using them is not portable, and the format of the
1151 return values might change.  Therefore we recommended you not use
1152 them.
1154 Note that the return value for any valid argument can be used
1155 in all situations (with the possible exception of the am/pm time formatting
1156 codes).  If the user has not selected any locale for the
1157 appropriate category, @code{nl_langinfo} returns the information from the
1158 @code{"C"} locale.  It is therefore possible to use this function as
1159 shown in the example below.
1161 If the argument @var{item} is not valid, a pointer to an empty string is
1162 returned.
1163 @end deftypefun
1165 An example of @code{nl_langinfo} usage is a function which has to
1166 print a given date and time in a locale-specific way.  At first one
1167 might think that, since @code{strftime} internally uses the locale
1168 information, writing something like the following is enough:
1170 @smallexample
1171 size_t
1172 i18n_time_n_data (char *s, size_t len, const struct tm *tp)
1174   return strftime (s, len, "%X %D", tp);
1176 @end smallexample
1178 The format contains no weekday or month names and therefore is
1179 internationally usable.  Wrong!  The output produced is something like
1180 @code{"hh:mm:ss MM/DD/YY"}.  This format is only recognizable in the
1181 USA.  Other countries use different formats.  Therefore the function
1182 should be rewritten like this:
1184 @smallexample
1185 size_t
1186 i18n_time_n_data (char *s, size_t len, const struct tm *tp)
1188   return strftime (s, len, nl_langinfo (D_T_FMT), tp);
1190 @end smallexample
1192 Now it uses the date and time format of the locale
1193 selected when the program runs.  If the user selects the locale
1194 correctly there should never be a misunderstanding over the time and
1195 date format.
1197 @node Formatting Numbers, Yes-or-No Questions, Locale Information, Locales
1198 @section A dedicated function to format numbers
1200 We have seen that the structure returned by @code{localeconv} as well as
1201 the values given to @code{nl_langinfo} allow you to retrieve the various
1202 pieces of locale-specific information to format numbers and monetary
1203 amounts.  We have also seen that the underlying rules are quite complex.
1205 Therefore the X/Open standards introduce a function which uses such
1206 locale information, making it easier for the user to format
1207 numbers according to these rules.
1209 @deftypefun ssize_t strfmon (char *@var{s}, size_t @var{maxsize}, const char *@var{format}, @dots{})
1210 @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
1211 @c It (and strfmon_l) both call __vstrfmon_l_internal, which, besides
1212 @c accessing the locale object passed to it, accesses the active
1213 @c locale through isdigit (but to_digit assumes ASCII digits only).
1214 @c It may call __printf_fp (@mtslocale @ascuheap @acsmem) and
1215 @c guess_grouping (safe).
1216 The @code{strfmon} function is similar to the @code{strftime} function
1217 in that it takes a buffer, its size, a format string,
1218 and values to write into the buffer as text in a form specified
1219 by the format string.  Like @code{strftime}, the function
1220 also returns the number of bytes written into the buffer.
1222 There are two differences: @code{strfmon} can take more than one
1223 argument, and, of course, the format specification is different.  Like
1224 @code{strftime}, the format string consists of normal text, which is
1225 output as is, and format specifiers, which are indicated by a @samp{%}.
1226 Immediately after the @samp{%}, you can optionally specify various flags
1227 and formatting information before the main formatting character, in a
1228 similar way to @code{printf}:
1230 @itemize @bullet
1231 @item
1232 Immediately following the @samp{%} there can be one or more of the
1233 following flags:
1234 @table @asis
1235 @item @samp{=@var{f}}
1236 The single byte character @var{f} is used for this field as the numeric
1237 fill character.  By default this character is a space character.
1238 Filling with this character is only performed if a left precision
1239 is specified.  It is not just to fill to the given field width.
1240 @item @samp{^}
1241 The number is printed without grouping the digits according to the rules
1242 of the current locale.  By default grouping is enabled.
1243 @item @samp{+}, @samp{(}
1244 At most one of these flags can be used.  They select which format to
1245 represent the sign of a currency amount.  By default, and if
1246 @samp{+} is given, the locale equivalent of @math{+}/@math{-} is used.  If
1247 @samp{(} is given, negative amounts are enclosed in parentheses.  The
1248 exact format is determined by the values of the @code{LC_MONETARY}
1249 category of the locale selected at program runtime.
1250 @item @samp{!}
1251 The output will not contain the currency symbol.
1252 @item @samp{-}
1253 The output will be formatted left-justified instead of right-justified if
1254 it does not fill the entire field width.
1255 @end table
1256 @end itemize
1258 The next part of the specification is an optional field width.  If no
1259 width is specified @math{0} is taken.  During output, the function first
1260 determines how much space is required.  If it requires at least as many
1261 characters as given by the field width, it is output using as much space
1262 as necessary.  Otherwise, it is extended to use the full width by
1263 filling with the space character.  The presence or absence of the
1264 @samp{-} flag determines the side at which such padding occurs.  If
1265 present, the spaces are added at the right making the output
1266 left-justified, and vice versa.
1268 So far the format looks familiar, being similar to the @code{printf} and
1269 @code{strftime} formats.  However, the next two optional fields
1270 introduce something new.  The first one is a @samp{#} character followed
1271 by a decimal digit string.  The value of the digit string specifies the
1272 number of @emph{digit} positions to the left of the decimal point (or
1273 equivalent).  This does @emph{not} include the grouping character when
1274 the @samp{^} flag is not given.  If the space needed to print the number
1275 does not fill the whole width, the field is padded at the left side with
1276 the fill character, which can be selected using the @samp{=} flag and by
1277 default is a space.  For example, if the field width is selected as 6
1278 and the number is @math{123}, the fill character is @samp{*} the result
1279 will be @samp{***123}.
1281 The second optional field starts with a @samp{.} (period) and consists
1282 of another decimal digit string.  Its value describes the number of
1283 characters printed after the decimal point.  The default is selected
1284 from the current locale (@code{frac_digits}, @code{int_frac_digits}, see
1285 @pxref{General Numeric}).  If the exact representation needs more digits
1286 than given by the field width, the displayed value is rounded.  If the
1287 number of fractional digits is selected to be zero, no decimal point is
1288 printed.
1290 As a GNU extension, the @code{strfmon} implementation in @theglibc{}
1291 allows an optional @samp{L} next as a format modifier.  If this modifier
1292 is given, the argument is expected to be a @code{long double} instead of
1293 a @code{double} value.
1295 Finally, the last component is a format specifier.  There are three
1296 specifiers defined:
1298 @table @asis
1299 @item @samp{i}
1300 Use the locale's rules for formatting an international currency value.
1301 @item @samp{n}
1302 Use the locale's rules for formatting a national currency value.
1303 @item @samp{%}
1304 Place a @samp{%} in the output.  There must be no flag, width
1305 specifier or modifier given, only @samp{%%} is allowed.
1306 @end table
1308 As for @code{printf}, the function reads the format string
1309 from left to right and uses the values passed to the function following
1310 the format string.  The values are expected to be either of type
1311 @code{double} or @code{long double}, depending on the presence of the
1312 modifier @samp{L}.  The result is stored in the buffer pointed to by
1313 @var{s}.  At most @var{maxsize} characters are stored.
1315 The return value of the function is the number of characters stored in
1316 @var{s}, including the terminating @code{NULL} byte.  If the number of
1317 characters stored would exceed @var{maxsize}, the function returns
1318 @math{-1} and the content of the buffer @var{s} is unspecified.  In this
1319 case @code{errno} is set to @code{E2BIG}.
1320 @end deftypefun
1322 A few examples should make clear how the function works.  It is
1323 assumed that all the following pieces of code are executed in a program
1324 which uses the USA locale (@code{en_US}).  The simplest
1325 form of the format is this:
1327 @smallexample
1328 strfmon (buf, 100, "@@%n@@%n@@%n@@", 123.45, -567.89, 12345.678);
1329 @end smallexample
1331 @noindent
1332 The output produced is
1333 @smallexample
1334 "@@$123.45@@-$567.89@@$12,345.68@@"
1335 @end smallexample
1337 We can notice several things here.  First, the widths of the output
1338 numbers are different.  We have not specified a width in the format
1339 string, and so this is no wonder.  Second, the third number is printed
1340 using thousands separators.  The thousands separator for the
1341 @code{en_US} locale is a comma.  The number is also rounded.
1342 @math{.678} is rounded to @math{.68} since the format does not specify a
1343 precision and the default value in the locale is @math{2}.  Finally,
1344 note that the national currency symbol is printed since @samp{%n} was
1345 used, not @samp{i}.  The next example shows how we can align the output.
1347 @smallexample
1348 strfmon (buf, 100, "@@%=*11n@@%=*11n@@%=*11n@@", 123.45, -567.89, 12345.678);
1349 @end smallexample
1351 @noindent
1352 The output this time is:
1354 @smallexample
1355 "@@    $123.45@@   -$567.89@@ $12,345.68@@"
1356 @end smallexample
1358 Two things stand out.  Firstly, all fields have the same width (eleven
1359 characters) since this is the width given in the format and since no
1360 number required more characters to be printed.  The second important
1361 point is that the fill character is not used.  This is correct since the
1362 white space was not used to achieve a precision given by a @samp{#}
1363 modifier, but instead to fill to the given width.  The difference
1364 becomes obvious if we now add a width specification.
1366 @smallexample
1367 strfmon (buf, 100, "@@%=*11#5n@@%=*11#5n@@%=*11#5n@@",
1368          123.45, -567.89, 12345.678);
1369 @end smallexample
1371 @noindent
1372 The output is
1374 @smallexample
1375 "@@ $***123.45@@-$***567.89@@ $12,456.68@@"
1376 @end smallexample
1378 Here we can see that all the currency symbols are now aligned, and that
1379 the space between the currency sign and the number is filled with the
1380 selected fill character.  Note that although the width is selected to be
1381 @math{5} and @math{123.45} has three digits left of the decimal point,
1382 the space is filled with three asterisks.  This is correct since, as
1383 explained above, the width does not include the positions used to store
1384 thousands separators.  One last example should explain the remaining
1385 functionality.
1387 @smallexample
1388 strfmon (buf, 100, "@@%=0(16#5.3i@@%=0(16#5.3i@@%=0(16#5.3i@@",
1389          123.45, -567.89, 12345.678);
1390 @end smallexample
1392 @noindent
1393 This rather complex format string produces the following output:
1395 @smallexample
1396 "@@ USD 000123,450 @@(USD 000567.890)@@ USD 12,345.678 @@"
1397 @end smallexample
1399 The most noticeable change is the alternative way of representing
1400 negative numbers.  In financial circles this is often done using
1401 parentheses, and this is what the @samp{(} flag selected.  The fill
1402 character is now @samp{0}.  Note that this @samp{0} character is not
1403 regarded as a numeric zero, and therefore the first and second numbers
1404 are not printed using a thousands separator.  Since we used the format
1405 specifier @samp{i} instead of @samp{n}, the international form of the
1406 currency symbol is used.  This is a four letter string, in this case
1407 @code{"USD "}.  The last point is that since the precision right of the
1408 decimal point is selected to be three, the first and second numbers are
1409 printed with an extra zero at the end and the third number is printed
1410 without rounding.
1412 @node Yes-or-No Questions,  , Formatting Numbers , Locales
1413 @section Yes-or-No Questions
1415 Some non GUI programs ask a yes-or-no question.  If the messages
1416 (especially the questions) are translated into foreign languages, be
1417 sure that you localize the answers too.  It would be very bad habit to
1418 ask a question in one language and request the answer in another, often
1419 English.
1421 @Theglibc{} contains @code{rpmatch} to give applications easy
1422 access to the corresponding locale definitions.
1424 @deftypefun int rpmatch (const char *@var{response})
1425 @standards{GNU, stdlib.h}
1426 @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @asulock{} @ascudlopen{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
1427 @c Calls nl_langinfo with YESEXPR and NOEXPR, triggering @mtslocale but
1428 @c it's regcomp and regexec that bring in all of the safety issues.
1429 @c regfree is also called, but it doesn't introduce any further issues.
1430 The function @code{rpmatch} checks the string in @var{response} for whether
1431 or not it is a correct yes-or-no answer and if yes, which one.  The
1432 check uses the @code{YESEXPR} and @code{NOEXPR} data in the
1433 @code{LC_MESSAGES} category of the currently selected locale.  The
1434 return value is as follows:
1436 @table @code
1437 @item 1
1438 The user entered an affirmative answer.
1440 @item 0
1441 The user entered a negative answer.
1443 @item -1
1444 The answer matched neither the @code{YESEXPR} nor the @code{NOEXPR}
1445 regular expression.
1446 @end table
1448 This function is not standardized but available beside in @theglibc{} at
1449 least also in the IBM AIX library.
1450 @end deftypefun
1452 @noindent
1453 This function would normally be used like this:
1455 @smallexample
1456   @dots{}
1457   /* @r{Use a safe default.}  */
1458   _Bool doit = false;
1460   fputs (gettext ("Do you really want to do this? "), stdout);
1461   fflush (stdout);
1462   /* @r{Prepare the @code{getline} call.}  */
1463   line = NULL;
1464   len = 0;
1465   while (getline (&line, &len, stdin) >= 0)
1466     @{
1467       /* @r{Check the response.}  */
1468       int res = rpmatch (line);
1469       if (res >= 0)
1470         @{
1471           /* @r{We got a definitive answer.}  */
1472           if (res > 0)
1473             doit = true;
1474           break;
1475         @}
1476     @}
1477   /* @r{Free what @code{getline} allocated.}  */
1478   free (line);
1479 @end smallexample
1481 Note that the loop continues until a read error is detected or until a
1482 definitive (positive or negative) answer is read.