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