ldpart.c revision 75367
172165Sphantom/* 272165Sphantom * Copyright (c) 2000, 2001 Alexey Zelkin 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 75367 2001-04-10 03:55:19Z deischen $ 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" 3872165Sphantom#include "setlocale.h" 3972165Sphantom#include "ldpart.h" 4072165Sphantom 4172165Sphantomstatic int split_lines(char *, const char *); 4272165Sphantomstatic void set_from_buf(const char *, int, const char **); 4372165Sphantom 4472165Sphantomint 4572165Sphantom__part_load_locale(const char *name, 4672165Sphantom int* using_locale, 4772165Sphantom char *locale_buf, 4872165Sphantom const char *category_name, 4972442Sphantom int locale_buf_size_max, 5072442Sphantom int locale_buf_size_min, 5172165Sphantom const char **dst_localebuf) { 5272165Sphantom 5372165Sphantom static char locale_buf_C[] = "C"; 5472165Sphantom static int num_lines; 5572165Sphantom 5672165Sphantom int fd; 5772165Sphantom char * lbuf; 5872165Sphantom char * p; 5972165Sphantom const char * plim; 6072165Sphantom char filename[PATH_MAX]; 6172165Sphantom struct stat st; 6272165Sphantom size_t namesize; 6372165Sphantom size_t bufsize; 6472165Sphantom int save_using_locale; 6572165Sphantom 6672165Sphantom save_using_locale = *using_locale; 6772165Sphantom *using_locale = 0; 6872165Sphantom 6972165Sphantom if (name == NULL) 7072165Sphantom goto no_locale; 7172165Sphantom 7272165Sphantom if (!strcmp(name, "C") || !strcmp(name, "POSIX")) 7372165Sphantom return 0; 7472165Sphantom 7572165Sphantom /* 7672165Sphantom ** If the locale name is the same as our cache, use the cache. 7772165Sphantom */ 7872165Sphantom lbuf = locale_buf; 7972165Sphantom if (lbuf != NULL && strcmp(name, lbuf) == 0) { 8072165Sphantom set_from_buf(lbuf, num_lines, dst_localebuf); 8172165Sphantom *using_locale = 1; 8272165Sphantom return 0; 8372165Sphantom } 8472165Sphantom /* 8572165Sphantom ** Slurp the locale file into the cache. 8672165Sphantom */ 8772165Sphantom namesize = strlen(name) + 1; 8872165Sphantom 8972165Sphantom if (!_PathLocale) 9072165Sphantom goto no_locale; 9172165Sphantom /* Range checking not needed, 'name' size is limited */ 9272165Sphantom strcpy(filename, _PathLocale); 9372165Sphantom strcat(filename, "/"); 9472165Sphantom strcat(filename, name); 9572165Sphantom strcat(filename, "/"); 9672165Sphantom strcat(filename, category_name); 9772165Sphantom fd = _open(filename, O_RDONLY); 9872165Sphantom if (fd < 0) 9972165Sphantom goto no_locale; 10073253Sdeischen if (_fstat(fd, &st) != 0) 10172165Sphantom goto bad_locale; 10272165Sphantom if (st.st_size <= 0) 10372165Sphantom goto bad_locale; 10472165Sphantom bufsize = namesize + st.st_size; 10572165Sphantom locale_buf = NULL; 10672165Sphantom lbuf = (lbuf == NULL || lbuf == locale_buf_C) ? 10772165Sphantom malloc(bufsize) : reallocf(lbuf, bufsize); 10872165Sphantom if (lbuf == NULL) 10972165Sphantom goto bad_locale; 11072165Sphantom (void) strcpy(lbuf, name); 11172165Sphantom p = lbuf + namesize; 11272165Sphantom plim = p + st.st_size; 11372165Sphantom if (_read(fd, p, (size_t) st.st_size) != st.st_size) 11472165Sphantom goto bad_lbuf; 11572165Sphantom if (_close(fd) != 0) 11672165Sphantom goto bad_lbuf; 11772165Sphantom /* 11872165Sphantom ** Parse the locale file into localebuf. 11972165Sphantom */ 12072165Sphantom if (plim[-1] != '\n') 12172165Sphantom goto bad_lbuf; 12272165Sphantom num_lines = split_lines(p, plim); 12372442Sphantom if (num_lines >= locale_buf_size_max) 12472442Sphantom num_lines = locale_buf_size_max; 12572442Sphantom else if (num_lines >= locale_buf_size_min) 12672442Sphantom num_lines = locale_buf_size_min; 12772165Sphantom else 12872165Sphantom goto reset_locale; 12972165Sphantom set_from_buf(lbuf, num_lines, dst_localebuf); 13072165Sphantom /* 13172165Sphantom ** Record the successful parse in the cache. 13272165Sphantom */ 13372165Sphantom locale_buf = lbuf; 13472165Sphantom 13572165Sphantom *using_locale = 1; 13672165Sphantom return 0; 13772165Sphantom 13872165Sphantomreset_locale: 13972165Sphantom locale_buf = locale_buf_C; 14072165Sphantom save_using_locale = 0; 14172165Sphantombad_lbuf: 14272165Sphantom free(lbuf); 14372165Sphantombad_locale: 14472165Sphantom (void)_close(fd); 14572165Sphantomno_locale: 14672165Sphantom *using_locale = save_using_locale; 14772165Sphantom return -1; 14872165Sphantom} 14972165Sphantom 15072165Sphantomstatic int 15172165Sphantomsplit_lines(char *p, const char *plim) 15272165Sphantom{ 15372165Sphantom int i; 15472165Sphantom 15572165Sphantom for (i = 0; p < plim; i++) { 15672165Sphantom p = strchr(p, '\n'); 15772165Sphantom *p++ = '\0'; 15872165Sphantom } 15972165Sphantom return i; 16072165Sphantom} 16172165Sphantom 16272165Sphantomstatic void 16372165Sphantomset_from_buf(const char *p, int num_lines, const char **dst_localebuf) 16472165Sphantom{ 16572165Sphantom const char **ap; 16672165Sphantom int i; 16772165Sphantom 16872165Sphantom for (ap = dst_localebuf, i = 0; i < num_lines; ++ap, ++i) 16972165Sphantom *ap = p += strlen(p) + 1; 17072165Sphantom} 17172165Sphantom 172