ldpart.c revision 87658
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 * $FreeBSD: head/lib/libc/locale/ldpart.c 87658 2001-12-11 15:55:42Z phantom $ 2772165Sphantom */ 2872165Sphantom 2972331Sphantom#include "namespace.h" 3072165Sphantom#include <sys/types.h> 3172165Sphantom#include <sys/stat.h> 3272165Sphantom#include <sys/syslimits.h> 3372165Sphantom#include <fcntl.h> 3472165Sphantom#include <stdlib.h> 3572165Sphantom#include <string.h> 3675367Sdeischen#include <unistd.h> 3775367Sdeischen#include "un-namespace.h" 3887658Sphantom 3972165Sphantom#include "setlocale.h" 4072165Sphantom#include "ldpart.h" 4172165Sphantom 4272165Sphantomstatic int split_lines(char *, const char *); 4372165Sphantomstatic void set_from_buf(const char *, int, const char **); 4472165Sphantom 4572165Sphantomint 4672165Sphantom__part_load_locale(const char *name, 4772165Sphantom int* using_locale, 4872165Sphantom char *locale_buf, 4972165Sphantom const char *category_name, 5072442Sphantom int locale_buf_size_max, 5172442Sphantom int locale_buf_size_min, 5272165Sphantom const char **dst_localebuf) { 5372165Sphantom 5472165Sphantom static char locale_buf_C[] = "C"; 5572165Sphantom static int num_lines; 5672165Sphantom 5772165Sphantom int fd; 5872165Sphantom char * lbuf; 5972165Sphantom char * p; 6072165Sphantom const char * plim; 6172165Sphantom char filename[PATH_MAX]; 6272165Sphantom struct stat st; 6372165Sphantom size_t namesize; 6472165Sphantom size_t bufsize; 6572165Sphantom int save_using_locale; 6672165Sphantom 6772165Sphantom save_using_locale = *using_locale; 6872165Sphantom *using_locale = 0; 6972165Sphantom 7072165Sphantom if (name == NULL) 7172165Sphantom goto no_locale; 7272165Sphantom 7372165Sphantom if (!strcmp(name, "C") || !strcmp(name, "POSIX")) 7472165Sphantom return 0; 7572165Sphantom 7672165Sphantom /* 7787658Sphantom * If the locale name is the same as our cache, use the cache. 7887658Sphantom */ 7972165Sphantom lbuf = locale_buf; 8072165Sphantom if (lbuf != NULL && strcmp(name, lbuf) == 0) { 8172165Sphantom set_from_buf(lbuf, num_lines, dst_localebuf); 8272165Sphantom *using_locale = 1; 8372165Sphantom return 0; 8472165Sphantom } 8572165Sphantom /* 8687658Sphantom * Slurp the locale file into the cache. 8787658Sphantom */ 8872165Sphantom namesize = strlen(name) + 1; 8972165Sphantom 9072165Sphantom if (!_PathLocale) 9172165Sphantom goto no_locale; 9272165Sphantom /* Range checking not needed, 'name' size is limited */ 9372165Sphantom strcpy(filename, _PathLocale); 9472165Sphantom strcat(filename, "/"); 9572165Sphantom strcat(filename, name); 9672165Sphantom strcat(filename, "/"); 9772165Sphantom strcat(filename, category_name); 9872165Sphantom fd = _open(filename, O_RDONLY); 9972165Sphantom if (fd < 0) 10072165Sphantom goto no_locale; 10173253Sdeischen if (_fstat(fd, &st) != 0) 10272165Sphantom goto bad_locale; 10372165Sphantom if (st.st_size <= 0) 10472165Sphantom goto bad_locale; 10572165Sphantom bufsize = namesize + st.st_size; 10672165Sphantom locale_buf = NULL; 10772165Sphantom lbuf = (lbuf == NULL || lbuf == locale_buf_C) ? 10872165Sphantom malloc(bufsize) : reallocf(lbuf, bufsize); 10972165Sphantom if (lbuf == NULL) 11072165Sphantom goto bad_locale; 11172165Sphantom (void) strcpy(lbuf, name); 11272165Sphantom p = lbuf + namesize; 11372165Sphantom plim = p + st.st_size; 11472165Sphantom if (_read(fd, p, (size_t) st.st_size) != st.st_size) 11572165Sphantom goto bad_lbuf; 11672165Sphantom if (_close(fd) != 0) 11772165Sphantom goto bad_lbuf; 11872165Sphantom /* 11987658Sphantom * Parse the locale file into localebuf. 12087658Sphantom */ 12172165Sphantom if (plim[-1] != '\n') 12272165Sphantom goto bad_lbuf; 12372165Sphantom num_lines = split_lines(p, plim); 12472442Sphantom if (num_lines >= locale_buf_size_max) 12572442Sphantom num_lines = locale_buf_size_max; 12672442Sphantom else if (num_lines >= locale_buf_size_min) 12772442Sphantom num_lines = locale_buf_size_min; 12872165Sphantom else 12972165Sphantom goto reset_locale; 13072165Sphantom set_from_buf(lbuf, num_lines, dst_localebuf); 13172165Sphantom /* 13272165Sphantom ** Record the successful parse in the cache. 13372165Sphantom */ 13472165Sphantom locale_buf = lbuf; 13572165Sphantom 13672165Sphantom *using_locale = 1; 13772165Sphantom return 0; 13872165Sphantom 13972165Sphantomreset_locale: 14072165Sphantom locale_buf = locale_buf_C; 14172165Sphantom save_using_locale = 0; 14272165Sphantombad_lbuf: 14372165Sphantom free(lbuf); 14472165Sphantombad_locale: 14572165Sphantom (void)_close(fd); 14672165Sphantomno_locale: 14772165Sphantom *using_locale = save_using_locale; 14872165Sphantom return -1; 14972165Sphantom} 15072165Sphantom 15172165Sphantomstatic int 15272165Sphantomsplit_lines(char *p, const char *plim) 15372165Sphantom{ 15472165Sphantom int i; 15572165Sphantom 15672165Sphantom for (i = 0; p < plim; i++) { 15772165Sphantom p = strchr(p, '\n'); 15872165Sphantom *p++ = '\0'; 15972165Sphantom } 16072165Sphantom return i; 16172165Sphantom} 16272165Sphantom 16372165Sphantomstatic void 16472165Sphantomset_from_buf(const char *p, int num_lines, const char **dst_localebuf) 16572165Sphantom{ 16672165Sphantom const char **ap; 16772165Sphantom int i; 16872165Sphantom 16972165Sphantom for (ap = dst_localebuf, i = 0; i < num_lines; ++ap, ++i) 17072165Sphantom *ap = p += strlen(p) + 1; 17172165Sphantom} 17272165Sphantom 173