char * TEXTDOMAIN (const char *domainname) { char *new_domain; char *old_domain; /* A NULL pointer requests the current setting. */ if (domainname == NULL) return (char *) _nl_current_default_domain; __libc_rwlock_wrlock (_nl_state_lock); old_domain = (char *) _nl_current_default_domain; /* If domain name is the null string set to default domain "messages". */ if (domainname[0] == '\0' || strcmp (domainname, _nl_default_default_domain) == 0) { _nl_current_default_domain = _nl_default_default_domain; new_domain = (char *) _nl_current_default_domain; } else if (strcmp (domainname, old_domain) == 0) /* This can happen and people will use it to signal that some environment variable changed. */ new_domain = old_domain; else { /* If the following malloc fails `_nl_current_default_domain' will be NULL. This value will be returned and so signals we are out of core. */ #if defined _LIBC || defined HAVE_STRDUP new_domain = strdup (domainname); #else size_t len = strlen (domainname) + 1; new_domain = (char *) malloc (len); if (new_domain != NULL) memcpy (new_domain, domainname, len); #endif if (new_domain != NULL) _nl_current_default_domain = new_domain; } /* We use this possibility to signal a change of the loaded catalogs since this is most likely the case and there is no other easy we to do it. Do it only when the call was successful. */ if (new_domain != NULL) { ++_nl_msg_cat_cntr; if (old_domain != new_domain && old_domain != _nl_default_default_domain) free (old_domain); } __libc_rwlock_unlock (_nl_state_lock); return new_domain; }
__locale_t __duplocale (__locale_t dataset) { /* This static object is returned for newlocale (LC_ALL_MASK, "C"). */ if (dataset == _nl_C_locobj_ptr) return dataset; /* Handle a special value. */ if (dataset == LC_GLOBAL_LOCALE) dataset = &_nl_global_locale; __locale_t result; int cnt; size_t names_len = 0; /* Calculate the total space we need to store all the names. */ for (cnt = 0; cnt < __LC_LAST; ++cnt) if (cnt != LC_ALL && dataset->__names[cnt] != _nl_C_name) names_len += strlen (dataset->__names[cnt]) + 1; /* Get memory. */ result = malloc (sizeof (struct __locale_struct) + names_len); if (result != NULL) { char *namep = (char *) (result + 1); /* We modify global data (the usage counts). */ __libc_rwlock_wrlock (__libc_setlocale_lock); for (cnt = 0; cnt < __LC_LAST; ++cnt) if (cnt != LC_ALL) { result->__locales[cnt] = dataset->__locales[cnt]; if (result->__locales[cnt]->usage_count < MAX_USAGE_COUNT) ++result->__locales[cnt]->usage_count; if (dataset->__names[cnt] == _nl_C_name) result->__names[cnt] = _nl_C_name; else { result->__names[cnt] = namep; namep = __stpcpy (namep, dataset->__names[cnt]) + 1; } } /* Update the special members. */ result->__ctype_b = dataset->__ctype_b; result->__ctype_tolower = dataset->__ctype_tolower; result->__ctype_toupper = dataset->__ctype_toupper; /* It's done. */ __libc_rwlock_unlock (__libc_setlocale_lock); } return result; }
char * TEXTDOMAIN (const char *domainname) { char *new_domain; char *old_domain; if (domainname == NULL) return (char *) _nl_current_default_domain; __libc_rwlock_wrlock (_nl_state_lock); old_domain = (char *) _nl_current_default_domain; if (domainname[0] == '\0' || strcmp (domainname, _nl_default_default_domain) == 0) { _nl_current_default_domain = _nl_default_default_domain; new_domain = (char *) _nl_current_default_domain; } else if (strcmp (domainname, old_domain) == 0) new_domain = old_domain; else { #if defined _LIBC || defined HAVE_STRDUP new_domain = strdup (domainname); #else size_t len = strlen (domainname) + 1; new_domain = (char *) malloc (len); if (new_domain != NULL) memcpy (new_domain, domainname, len); #endif if (new_domain != NULL) _nl_current_default_domain = new_domain; } if (new_domain != NULL) { ++_nl_msg_cat_cntr; if (old_domain != new_domain && old_domain != _nl_default_default_domain) free (old_domain); } __libc_rwlock_unlock (_nl_state_lock); return new_domain; }
void __freelocale (__locale_t dataset) { int cnt; /* This static object is returned for newlocale (LC_ALL_MASK, "C"). */ if (dataset == _nl_C_locobj_ptr) return; /* We modify global data (the usage counts). */ __libc_rwlock_wrlock (__libc_setlocale_lock); for (cnt = 0; cnt < __LC_LAST; ++cnt) if (cnt != LC_ALL && dataset->__locales[cnt]->usage_count != UNDELETABLE) /* We can remove the data. */ _nl_remove_locale (cnt, dataset->__locales[cnt]); /* It's done. */ __libc_rwlock_unlock (__libc_setlocale_lock); /* Free the locale_t handle itself. */ free (dataset); }
__locale_t __newlocale (int category_mask, const char *locale, __locale_t base) { /* Intermediate memory for result. */ const char *newnames[__LC_LAST]; struct __locale_struct result; __locale_t result_ptr; char *locale_path; size_t locale_path_len; const char *locpath_var; int cnt; size_t names_len; /* We treat LC_ALL in the same way as if all bits were set. */ if (category_mask == 1 << LC_ALL) category_mask = (1 << __LC_LAST) - 1 - (1 << LC_ALL); /* Sanity check for CATEGORY argument. */ if ((category_mask & ~((1 << __LC_LAST) - 1 - (1 << LC_ALL))) != 0) ERROR_RETURN; /* `newlocale' does not support asking for the locale name. */ if (locale == NULL) ERROR_RETURN; if (base == _nl_C_locobj_ptr) /* We're to modify BASE, returned for a previous call with "C". We can't really modify the read-only structure, so instead start over by copying it. */ base = NULL; if ((base == NULL || category_mask == (1 << __LC_LAST) - 1 - (1 << LC_ALL)) && (category_mask == 0 || !strcmp (locale, "C"))) /* Asking for the "C" locale needn't allocate a new object. */ return _nl_C_locobj_ptr; /* Allocate memory for the result. */ if (base != NULL) result = *base; else /* Fill with pointers to C locale data. */ result = _nl_C_locobj; /* If no category is to be set we return BASE if available or a dataset using the C locale data. */ if (category_mask == 0) { result_ptr = (__locale_t) malloc (sizeof (struct __locale_struct)); if (result_ptr == NULL) return NULL; *result_ptr = result; goto update; } /* We perhaps really have to load some data. So we determine the path in which to look for the data now. The environment variable `LOCPATH' must only be used when the binary has no SUID or SGID bit set. If using the default path, we tell _nl_find_locale by passing null and it can check the canonical locale archive. */ locale_path = NULL; locale_path_len = 0; locpath_var = getenv ("LOCPATH"); if (locpath_var != NULL && locpath_var[0] != '\0') { if (__argz_create_sep (locpath_var, ':', &locale_path, &locale_path_len) != 0) return NULL; if (__argz_add_sep (&locale_path, &locale_path_len, _nl_default_locale_path, ':') != 0) return NULL; } /* Get the names for the locales we are interested in. We either allow a composite name or a single name. */ for (cnt = 0; cnt < __LC_LAST; ++cnt) if (cnt != LC_ALL) newnames[cnt] = locale; if (strchr (locale, ';') != NULL) { /* This is a composite name. Make a copy and split it up. */ char *np = strdupa (locale); char *cp; int specified_mask = 0; while ((cp = strchr (np, '=')) != NULL) { for (cnt = 0; cnt < __LC_LAST; ++cnt) if (cnt != LC_ALL && (size_t) (cp - np) == _nl_category_name_sizes[cnt] && memcmp (np, (_nl_category_names.str + _nl_category_name_idxs[cnt]), cp - np) == 0) break; if (cnt == __LC_LAST) /* Bogus category name. */ ERROR_RETURN; /* Found the category this clause sets. */ specified_mask |= 1 << cnt; newnames[cnt] = ++cp; cp = strchr (cp, ';'); if (cp != NULL) { /* Examine the next clause. */ *cp = '\0'; np = cp + 1; } else /* This was the last clause. We are done. */ break; } if (category_mask &~ specified_mask) /* The composite name did not specify all categories we need. */ ERROR_RETURN; } /* Protect global data. */ __libc_rwlock_wrlock (__libc_setlocale_lock); /* Now process all categories we are interested in. */ names_len = 0; for (cnt = 0; cnt < __LC_LAST; ++cnt) { if ((category_mask & 1 << cnt) != 0) { result.__locales[cnt] = _nl_find_locale (locale_path, locale_path_len, cnt, &newnames[cnt]); if (result.__locales[cnt] == NULL) { free_cnt_data_and_exit: while (cnt-- > 0) if (((category_mask & 1 << cnt) != 0) && result.__locales[cnt]->usage_count != UNDELETABLE) /* We can remove the data. */ _nl_remove_locale (cnt, result.__locales[cnt]); /* Critical section left. */ __libc_rwlock_unlock (__libc_setlocale_lock); return NULL; } if (newnames[cnt] != _nl_C_name) names_len += strlen (newnames[cnt]) + 1; } else if (cnt != LC_ALL && result.__names[cnt] != _nl_C_name) /* Tally up the unchanged names from BASE as well. */ names_len += strlen (result.__names[cnt]) + 1; } /* We successfully loaded all required data. Allocate a new structure. We can't just reuse the BASE pointer, because the name strings are changing and we need the old name string area intact so we can copy out of it into the new one without overlap problems should some category's name be getting longer. */ result_ptr = malloc (sizeof (struct __locale_struct) + names_len); if (result_ptr == NULL) { cnt = __LC_LAST; goto free_cnt_data_and_exit; } if (base == NULL) { /* Fill in this new structure from scratch. */ char *namep = (char *) (result_ptr + 1); /* Install copied new names in the new structure's __names array. If resolved to "C", that is already in RESULT.__names to start. */ for (cnt = 0; cnt < __LC_LAST; ++cnt) if ((category_mask & 1 << cnt) != 0 && newnames[cnt] != _nl_C_name) { result.__names[cnt] = namep; namep = __stpcpy (namep, newnames[cnt]) + 1; } *result_ptr = result; } else { /* We modify the base structure. */ char *namep = (char *) (result_ptr + 1); for (cnt = 0; cnt < __LC_LAST; ++cnt) if ((category_mask & 1 << cnt) != 0) { if (base->__locales[cnt]->usage_count != UNDELETABLE) /* We can remove the old data. */ _nl_remove_locale (cnt, base->__locales[cnt]); result_ptr->__locales[cnt] = result.__locales[cnt]; if (newnames[cnt] == _nl_C_name) result_ptr->__names[cnt] = _nl_C_name; else { result_ptr->__names[cnt] = namep; namep = __stpcpy (namep, newnames[cnt]) + 1; } } else if (cnt != LC_ALL) { /* The RESULT members point into the old BASE structure. */ result_ptr->__locales[cnt] = result.__locales[cnt]; if (result.__names[cnt] == _nl_C_name) result_ptr->__names[cnt] = _nl_C_name; else { result_ptr->__names[cnt] = namep; namep = __stpcpy (namep, result.__names[cnt]) + 1; } } free (base); } /* Critical section left. */ __libc_rwlock_unlock (__libc_setlocale_lock); /* Update the special members. */ update: { union locale_data_value *ctypes = result_ptr->__locales[LC_CTYPE]->values; result_ptr->__ctype_b = (const unsigned short int *) ctypes[_NL_ITEM_INDEX (_NL_CTYPE_CLASS)].string + 128; result_ptr->__ctype_tolower = (const int *) ctypes[_NL_ITEM_INDEX (_NL_CTYPE_TOLOWER)].string + 128; result_ptr->__ctype_toupper = (const int *) ctypes[_NL_ITEM_INDEX (_NL_CTYPE_TOUPPER)].string + 128; } return result_ptr; }
/* Specifies the directory name *DIRNAMEP and the output codeset *CODESETP to be used for the DOMAINNAME message catalog. If *DIRNAMEP or *CODESETP is NULL, the corresponding attribute is not modified, only the current value is returned. If DIRNAMEP or CODESETP is NULL, the corresponding attribute is neither modified nor returned. */ static void set_binding_values (const char *domainname, const char **dirnamep, const char **codesetp) { struct binding *binding; int modified; /* Some sanity checks. */ if (domainname == NULL || domainname[0] == '\0') { if (dirnamep) *dirnamep = NULL; if (codesetp) *codesetp = NULL; return; } __libc_rwlock_wrlock (_nl_state_lock); modified = 0; for (binding = _nl_domain_bindings; binding != NULL; binding = binding->next) { int compare = strcmp (domainname, binding->domainname); if (compare == 0) /* We found it! */ break; if (compare < 0) { /* It is not in the list. */ binding = NULL; break; } } if (binding != NULL) { if (dirnamep) { const char *dirname = *dirnamep; if (dirname == NULL) /* The current binding has be to returned. */ *dirnamep = binding->dirname; else { /* The domain is already bound. If the new value and the old one are equal we simply do nothing. Otherwise replace the old binding. */ char *result = binding->dirname; if (strcmp (dirname, result) != 0) { if (strcmp (dirname, INTUSE(_nl_default_dirname)) == 0) result = (char *) INTUSE(_nl_default_dirname); else { #if defined _LIBC || defined HAVE_STRDUP result = strdup (dirname); #else size_t len = strlen (dirname) + 1; result = (char *) malloc (len); if (__builtin_expect (result != NULL, 1)) memcpy (result, dirname, len); #endif } if (__builtin_expect (result != NULL, 1)) { if (binding->dirname != INTUSE(_nl_default_dirname)) free (binding->dirname); binding->dirname = result; modified = 1; } } *dirnamep = result; } } if (codesetp) { const char *codeset = *codesetp; if (codeset == NULL) /* The current binding has be to returned. */ *codesetp = binding->codeset; else { /* The domain is already bound. If the new value and the old one are equal we simply do nothing. Otherwise replace the old binding. */ char *result = binding->codeset; if (result == NULL || strcmp (codeset, result) != 0) { #if defined _LIBC || defined HAVE_STRDUP result = strdup (codeset); #else size_t len = strlen (codeset) + 1; result = (char *) malloc (len); if (__builtin_expect (result != NULL, 1)) memcpy (result, codeset, len); #endif if (__builtin_expect (result != NULL, 1)) { if (binding->codeset != NULL) free (binding->codeset); binding->codeset = result; binding->codeset_cntr++; modified = 1; } } *codesetp = result; } } } else if ((dirnamep == NULL || *dirnamep == NULL) && (codesetp == NULL || *codesetp == NULL)) { /* Simply return the default values. */ if (dirnamep) *dirnamep = INTUSE(_nl_default_dirname); if (codesetp) *codesetp = NULL; } else { /* We have to create a new binding. */ size_t len = strlen (domainname) + 1; struct binding *new_binding = (struct binding *) malloc (offsetof (struct binding, domainname) + len); if (__builtin_expect (new_binding == NULL, 0)) goto failed; memcpy (new_binding->domainname, domainname, len); if (dirnamep) { const char *dirname = *dirnamep; if (dirname == NULL) /* The default value. */ dirname = INTUSE(_nl_default_dirname); else { if (strcmp (dirname, INTUSE(_nl_default_dirname)) == 0) dirname = INTUSE(_nl_default_dirname); else { char *result; #if defined _LIBC || defined HAVE_STRDUP result = strdup (dirname); if (__builtin_expect (result == NULL, 0)) goto failed_dirname; #else size_t len = strlen (dirname) + 1; result = (char *) malloc (len); if (__builtin_expect (result == NULL, 0)) goto failed_dirname; memcpy (result, dirname, len); #endif dirname = result; } } *dirnamep = dirname; new_binding->dirname = (char *) dirname; } else /* The default value. */ new_binding->dirname = (char *) INTUSE(_nl_default_dirname); new_binding->codeset_cntr = 0; if (codesetp) { const char *codeset = *codesetp; if (codeset != NULL) { char *result; #if defined _LIBC || defined HAVE_STRDUP result = strdup (codeset); if (__builtin_expect (result == NULL, 0)) goto failed_codeset; #else size_t len = strlen (codeset) + 1; result = (char *) malloc (len); if (__builtin_expect (result == NULL, 0)) goto failed_codeset; memcpy (result, codeset, len); #endif codeset = result; new_binding->codeset_cntr++; } *codesetp = codeset; new_binding->codeset = (char *) codeset; } else new_binding->codeset = NULL; /* Now enqueue it. */ if (_nl_domain_bindings == NULL || strcmp (domainname, _nl_domain_bindings->domainname) < 0) { new_binding->next = _nl_domain_bindings; _nl_domain_bindings = new_binding; } else { binding = _nl_domain_bindings; while (binding->next != NULL && strcmp (domainname, binding->next->domainname) > 0) binding = binding->next; new_binding->next = binding->next; binding->next = new_binding; } modified = 1; /* Here we deal with memory allocation failures. */ if (0) { failed_codeset: if (new_binding->dirname != INTUSE(_nl_default_dirname)) free (new_binding->dirname); failed_dirname: free (new_binding); failed: if (dirnamep) *dirnamep = NULL; if (codesetp) *codesetp = NULL; } } /* If we modified any binding, we flush the caches. */ if (modified) ++_nl_msg_cat_cntr; __libc_rwlock_unlock (_nl_state_lock); }