duplocale - duplicate a locale object
#include <locale.h>
locale_t duplocale(locale_t locobj);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
On success, duplocale() returns a handle for the new
locale object. On error, it returns (locale_t) 0
, and sets
errno
to indicate the error.
The program below uses uselocale(3) and duplocale() to obtain a handle for the current locale which is then passed to toupper_l(3). The program takes one command-line argument, a string of characters that is converted to uppercase and displayed on standard output. An example of its use is the following:
$ ./a.out abc
ABC
#define _XOPEN_SOURCE 700
#include <ctype.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)
int
main(int argc, char *argv[])
{
locale_t loc, nloc;
if (argc != 2) {
fprintf(stderr, "Usage: %s string\n", argv[0]);
exit(EXIT_FAILURE);
}
/* This sequence is necessary, because uselocale() might return
the value LC_GLOBAL_LOCALE, which can't be passed as an
argument to toupper_l(). */
loc = uselocale((locale_t) 0);
if (loc == (locale_t) 0)
errExit("uselocale");
nloc = duplocale(loc);
if (nloc == (locale_t) 0)
errExit("duplocale");
for (char *p = argv[1]; *p; p++)
putchar(toupper_l(*p, nloc));
printf("\n");
freelocale(nloc);
exit(EXIT_SUCCESS);
}