ldpart.c revision 92986
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 92986 2002-03-22 21:53:29Z obrien $"); 2992986Sobrien 3072331Sphantom#include "namespace.h" 3172165Sphantom#include <sys/types.h> 3272165Sphantom#include <sys/stat.h> 3372165Sphantom#include <sys/syslimits.h> 3472165Sphantom#include <fcntl.h> 3572165Sphantom#include <stdlib.h> 3672165Sphantom#include <string.h> 3775367Sdeischen#include <unistd.h> 3875367Sdeischen#include "un-namespace.h" 3987658Sphantom 4072165Sphantom#include "setlocale.h" 4172165Sphantom#include "ldpart.h" 4272165Sphantom 4372165Sphantomstatic int split_lines(char *, const char *); 4472165Sphantomstatic void set_from_buf(const char *, int, const char **); 4572165Sphantom 4672165Sphantomint 4772165Sphantom__part_load_locale(const char *name, 4888309Sphantom int *using_locale, 4972165Sphantom char *locale_buf, 5088309Sphantom const char *category_filename, 5172442Sphantom int locale_buf_size_max, 5272442Sphantom int locale_buf_size_min, 5372165Sphantom const char **dst_localebuf) { 5472165Sphantom 5572165Sphantom static char locale_buf_C[] = "C"; 5672165Sphantom static int num_lines; 5772165Sphantom 5888309Sphantom int fd; 5988309Sphantom char *lbuf; 6088309Sphantom char *p; 6188309Sphantom const char *plim; 6288309Sphantom char filename[PATH_MAX]; 6388309Sphantom struct stat st; 6488309Sphantom size_t namesize; 6588309Sphantom size_t bufsize; 6688309Sphantom int save_using_locale; 6772165Sphantom 6872165Sphantom save_using_locale = *using_locale; 6972165Sphantom *using_locale = 0; 7072165Sphantom 7172165Sphantom if (name == NULL) 7272165Sphantom goto no_locale; 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 9272165Sphantom if (!_PathLocale) 9372165Sphantom goto no_locale; 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; 10572165Sphantom if (st.st_size <= 0) 10672165Sphantom goto bad_locale; 10772165Sphantom bufsize = namesize + st.st_size; 10872165Sphantom locale_buf = NULL; 10972165Sphantom lbuf = (lbuf == NULL || lbuf == locale_buf_C) ? 11072165Sphantom malloc(bufsize) : reallocf(lbuf, bufsize); 11172165Sphantom if (lbuf == NULL) 11272165Sphantom goto bad_locale; 11372165Sphantom (void) strcpy(lbuf, name); 11472165Sphantom p = lbuf + namesize; 11572165Sphantom plim = p + st.st_size; 11672165Sphantom if (_read(fd, p, (size_t) st.st_size) != st.st_size) 11772165Sphantom goto bad_lbuf; 11872165Sphantom if (_close(fd) != 0) 11972165Sphantom goto bad_lbuf; 12072165Sphantom /* 12187658Sphantom * Parse the locale file into localebuf. 12287658Sphantom */ 12372165Sphantom if (plim[-1] != '\n') 12472165Sphantom goto bad_lbuf; 12572165Sphantom num_lines = split_lines(p, plim); 12672442Sphantom if (num_lines >= locale_buf_size_max) 12772442Sphantom num_lines = locale_buf_size_max; 12872442Sphantom else if (num_lines >= locale_buf_size_min) 12972442Sphantom num_lines = locale_buf_size_min; 13072165Sphantom else 13172165Sphantom goto reset_locale; 13272165Sphantom set_from_buf(lbuf, num_lines, dst_localebuf); 13372165Sphantom /* 13488309Sphantom * Record the successful parse in the cache. 13588309Sphantom */ 13672165Sphantom locale_buf = lbuf; 13772165Sphantom 13872165Sphantom *using_locale = 1; 13972165Sphantom return 0; 14072165Sphantom 14172165Sphantomreset_locale: 14272165Sphantom locale_buf = locale_buf_C; 14372165Sphantom save_using_locale = 0; 14472165Sphantombad_lbuf: 14572165Sphantom free(lbuf); 14672165Sphantombad_locale: 14772165Sphantom (void)_close(fd); 14872165Sphantomno_locale: 14972165Sphantom *using_locale = save_using_locale; 15072165Sphantom return -1; 15172165Sphantom} 15272165Sphantom 15372165Sphantomstatic int 15488309Sphantomsplit_lines(char *p, const char *plim) { 15588309Sphantom 15672165Sphantom int i; 15772165Sphantom 15872165Sphantom for (i = 0; p < plim; i++) { 15972165Sphantom p = strchr(p, '\n'); 16072165Sphantom *p++ = '\0'; 16172165Sphantom } 16272165Sphantom return i; 16372165Sphantom} 16472165Sphantom 16572165Sphantomstatic void 16688309Sphantomset_from_buf(const char *p, int num_lines, const char **dst_localebuf) { 16788309Sphantom 16872165Sphantom const char **ap; 16972165Sphantom int i; 17072165Sphantom 17172165Sphantom for (ap = dst_localebuf, i = 0; i < num_lines; ++ap, ++i) 17272165Sphantom *ap = p += strlen(p) + 1; 17372165Sphantom} 17472165Sphantom 175