Branch data Line data Source code
1 : : /* 2 : : * libdpkg - Debian packaging suite library routines 3 : : * i18n.c - i18n support 4 : : * 5 : : * Copyright © 2013 Guillem Jover <guillem@debian.org> 6 : : * 7 : : * This is free software; you can redistribute it and/or modify 8 : : * it under the terms of the GNU General Public License as published by 9 : : * the Free Software Foundation; either version 2 of the License, or 10 : : * (at your option) any later version. 11 : : * 12 : : * This is distributed in the hope that it will be useful, 13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 : : * GNU General Public License for more details. 16 : : * 17 : : * You should have received a copy of the GNU General Public License 18 : : * along with this program. If not, see <https://www.gnu.org/licenses/>. 19 : : */ 20 : : 21 : : #include <config.h> 22 : : #include <compat.h> 23 : : 24 : : #include <dpkg/i18n.h> 25 : : 26 : : #ifdef HAVE_USELOCALE 27 : : #ifdef HAVE_XLOCALE_H 28 : : #include <xlocale.h> 29 : : #endif 30 : : static locale_t dpkg_C_locale; 31 : : #endif 32 : : 33 : : void 34 : 371 : dpkg_locales_init(const char *package) 35 : : { 36 : 371 : setlocale(LC_ALL, ""); 37 : 371 : bindtextdomain(package, LOCALEDIR); 38 : 371 : textdomain(package); 39 : : 40 : : #ifdef HAVE_USELOCALE 41 : 371 : dpkg_C_locale = newlocale(LC_ALL_MASK, "C", (locale_t)0); 42 : : #endif 43 : : 44 : : #if defined(__APPLE__) && defined(__MACH__) 45 : : /* 46 : : * On Mac OS X, the libintl code needs to call into the CoreFoundation 47 : : * framework, which is internally threaded, to initialize some caches. 48 : : * This is a problem when that first call is done after a fork(3), 49 : : * because per POSIX, only one thread will survive, leaving the 50 : : * process in a very inconsistent state, leading to crashes. 51 : : * 52 : : * XXX: To workaround this, we try to force the caches initialization 53 : : * at program startup time, by performing a dummy gettext() call. 54 : : */ 55 : : gettext(""); 56 : : #endif 57 : 371 : } 58 : : 59 : : void 60 : 306 : dpkg_locales_done(void) 61 : : { 62 : : #ifdef HAVE_USELOCALE 63 : 306 : freelocale(dpkg_C_locale); 64 : 306 : dpkg_C_locale = (locale_t)0; 65 : : #endif 66 : 306 : } 67 : : 68 : : struct dpkg_locale 69 : 0 : dpkg_locale_switch_C(void) 70 : : { 71 : : struct dpkg_locale loc; 72 : : 73 : : #ifdef HAVE_USELOCALE 74 : 0 : loc.oldloc = uselocale(dpkg_C_locale); 75 : : #else 76 : : loc.oldloc = setlocale(LC_ALL, NULL); 77 : : setlocale(LC_ALL, "C"); 78 : : #endif 79 : : 80 : 0 : return loc; 81 : : } 82 : : 83 : : void 84 : 0 : dpkg_locale_switch_back(struct dpkg_locale loc) 85 : : { 86 : : #ifdef HAVE_USELOCALE 87 : 0 : uselocale(loc.oldloc); 88 : : #else 89 : : setlocale(LC_ALL, loc.oldloc); 90 : : #endif 91 : 0 : }