ldpart.c revision 101307
172165Sphantom/* 287658Sphantom * Copyright (c) 2000, 2001 Alexey Zelkin <phantom@FreeBSD.org> 372165Sphantom * All rights reserved. 472165Sphantom * 572165Sphantom * Redistribution and use in source and binary forms, with or without 672165Sphantom * modification, are permitted provided that the following conditions 772165Sphantom * are met: 872165Sphantom * 1. Redistributions of source code must retain the above copyright 972165Sphantom * notice, this list of conditions and the following disclaimer. 1072165Sphantom * 2. Redistributions in binary form must reproduce the above copyright 1172165Sphantom * notice, this list of conditions and the following disclaimer in the 1272165Sphantom * documentation and/or other materials provided with the distribution. 1372165Sphantom * 1472165Sphantom * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1572165Sphantom * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1672165Sphantom * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1772165Sphantom * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1872165Sphantom * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1972165Sphantom * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2072165Sphantom * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2172165Sphantom * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2272165Sphantom * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2372165Sphantom * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2472165Sphantom * SUCH DAMAGE. 2572165Sphantom */ 2672165Sphantom 2792986Sobrien#include <sys/cdefs.h> 2892986Sobrien__FBSDID("$FreeBSD: head/lib/libc/locale/ldpart.c 101307 2002-08-04 09:37:28Z ache $"); 2992986Sobrien 3072331Sphantom#include "namespace.h" 3172165Sphantom#include <sys/types.h> 3272165Sphantom#include <sys/stat.h> 3372165Sphantom#include <sys/syslimits.h> 34101307Sache#include <errno.h> 3572165Sphantom#include <fcntl.h> 3672165Sphantom#include <stdlib.h> 3772165Sphantom#include <string.h> 3875367Sdeischen#include <unistd.h> 3975367Sdeischen#include "un-namespace.h" 4087658Sphantom 4172165Sphantom#include "setlocale.h" 4272165Sphantom#include "ldpart.h" 4372165Sphantom 4472165Sphantomstatic int split_lines(char *, const char *); 4572165Sphantomstatic void set_from_buf(const char *, int, const char **); 4672165Sphantom 4772165Sphantomint 4872165Sphantom__part_load_locale(const char *name, 4988309Sphantom int *using_locale, 5072165Sphantom char *locale_buf, 5188309Sphantom const char *category_filename, 5272442Sphantom int locale_buf_size_max, 5372442Sphantom int locale_buf_size_min, 54101307Sache const char **dst_localebuf) 55101307Sache{ 5672165Sphantom static char locale_buf_C[] = "C"; 5772165Sphantom static int num_lines; 58101307Sache int saverr; 5988309Sphantom int fd; 6088309Sphantom char *lbuf; 6188309Sphantom char *p; 6288309Sphantom const char *plim; 6388309Sphantom char filename[PATH_MAX]; 6488309Sphantom struct stat st; 6588309Sphantom size_t namesize; 6688309Sphantom size_t bufsize; 6788309Sphantom int save_using_locale; 6872165Sphantom 6972165Sphantom save_using_locale = *using_locale; 7072165Sphantom *using_locale = 0; 7172165Sphantom 72101307Sache /* 'name' must be already checked. */ 7372165Sphantom 7472165Sphantom if (!strcmp(name, "C") || !strcmp(name, "POSIX")) 7572165Sphantom return 0; 7672165Sphantom 7772165Sphantom /* 7887658Sphantom * If the locale name is the same as our cache, use the cache. 7987658Sphantom */ 8072165Sphantom lbuf = locale_buf; 8172165Sphantom if (lbuf != NULL && strcmp(name, lbuf) == 0) { 8272165Sphantom set_from_buf(lbuf, num_lines, dst_localebuf); 8372165Sphantom *using_locale = 1; 8472165Sphantom return 0; 8572165Sphantom } 8688309Sphantom 8772165Sphantom /* 8887658Sphantom * Slurp the locale file into the cache. 8987658Sphantom */ 9072165Sphantom namesize = strlen(name) + 1; 9172165Sphantom 92101307Sache /* 'PathLocale' must be already set & checked. */ 93101307Sache 9472165Sphantom /* Range checking not needed, 'name' size is limited */ 9572165Sphantom strcpy(filename, _PathLocale); 9672165Sphantom strcat(filename, "/"); 9772165Sphantom strcat(filename, name); 9872165Sphantom strcat(filename, "/"); 9988309Sphantom strcat(filename, category_filename); 10072165Sphantom fd = _open(filename, O_RDONLY); 10172165Sphantom if (fd < 0) 10272165Sphantom goto no_locale; 10373253Sdeischen if (_fstat(fd, &st) != 0) 10472165Sphantom goto bad_locale; 105101307Sache if (st.st_size <= 0) { 106101307Sache errno = EFTYPE; 10772165Sphantom goto bad_locale; 108101307Sache } 10972165Sphantom bufsize = namesize + st.st_size; 11072165Sphantom locale_buf = NULL; 11172165Sphantom lbuf = (lbuf == NULL || lbuf == locale_buf_C) ? 11272165Sphantom malloc(bufsize) : reallocf(lbuf, bufsize); 11372165Sphantom if (lbuf == NULL) 11472165Sphantom goto bad_locale; 11572165Sphantom (void) strcpy(lbuf, name); 11672165Sphantom p = lbuf + namesize; 11772165Sphantom plim = p + st.st_size; 11872165Sphantom if (_read(fd, p, (size_t) st.st_size) != st.st_size) 11972165Sphantom goto bad_lbuf; 12072165Sphantom if (_close(fd) != 0) 12172165Sphantom goto bad_lbuf; 12272165Sphantom /* 12387658Sphantom * Parse the locale file into localebuf. 12487658Sphantom */ 125101307Sache if (plim[-1] != '\n') { 126101307Sache errno = EFTYPE; 12772165Sphantom goto bad_lbuf; 128101307Sache } 12972165Sphantom num_lines = split_lines(p, plim); 13072442Sphantom if (num_lines >= locale_buf_size_max) 13172442Sphantom num_lines = locale_buf_size_max; 13272442Sphantom else if (num_lines >= locale_buf_size_min) 13372442Sphantom num_lines = locale_buf_size_min; 134101307Sache else { 135101307Sache errno = EFTYPE; 13672165Sphantom goto reset_locale; 137101307Sache } 13872165Sphantom set_from_buf(lbuf, num_lines, dst_localebuf); 13972165Sphantom /* 14088309Sphantom * Record the successful parse in the cache. 14188309Sphantom */ 14272165Sphantom locale_buf = lbuf; 14372165Sphantom 14472165Sphantom *using_locale = 1; 14572165Sphantom return 0; 14672165Sphantom 14772165Sphantomreset_locale: 14872165Sphantom locale_buf = locale_buf_C; 14972165Sphantom save_using_locale = 0; 15072165Sphantombad_lbuf: 151101307Sache saverr = errno; free(lbuf); errno = saverr; 15272165Sphantombad_locale: 153101307Sache saverr = errno; (void)_close(fd); errno = saverr; 15472165Sphantomno_locale: 15572165Sphantom *using_locale = save_using_locale; 15672165Sphantom return -1; 15772165Sphantom} 15872165Sphantom 15972165Sphantomstatic int 16088309Sphantomsplit_lines(char *p, const char *plim) { 16188309Sphantom 16272165Sphantom int i; 16372165Sphantom 16472165Sphantom for (i = 0; p < plim; i++) { 16572165Sphantom p = strchr(p, '\n'); 16672165Sphantom *p++ = '\0'; 16772165Sphantom } 16872165Sphantom return i; 16972165Sphantom} 17072165Sphantom 17172165Sphantomstatic void 17288309Sphantomset_from_buf(const char *p, int num_lines, const char **dst_localebuf) { 17388309Sphantom 17472165Sphantom const char **ap; 17572165Sphantom int i; 17672165Sphantom 17772165Sphantom for (ap = dst_localebuf, i = 0; i < num_lines; ++ap, ++i) 17872165Sphantom *ap = p += strlen(p) + 1; 17972165Sphantom} 18072165Sphantom 181