ldpart.c revision 72165
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 72165 2001-02-08 16:58:53Z phantom $ 2772165Sphantom */ 2872165Sphantom 2972165Sphantom#include <sys/types.h> 3072165Sphantom#include <sys/stat.h> 3172165Sphantom#include <sys/syslimits.h> 3272165Sphantom#include <fcntl.h> 3372165Sphantom#include <stdlib.h> 3472165Sphantom#include <string.h> 3572165Sphantom#include "setlocale.h" 3672165Sphantom#include "ldpart.h" 3772165Sphantom 3872165Sphantomstatic int split_lines(char *, const char *); 3972165Sphantomstatic void set_from_buf(const char *, int, const char **); 4072165Sphantom 4172165Sphantomint 4272165Sphantom__part_load_locale(const char *name, 4372165Sphantom int* using_locale, 4472165Sphantom char *locale_buf, 4572165Sphantom const char *category_name, 4672165Sphantom int locale_buf_size, 4772165Sphantom const char **dst_localebuf) { 4872165Sphantom 4972165Sphantom static char locale_buf_C[] = "C"; 5072165Sphantom static int num_lines; 5172165Sphantom 5272165Sphantom int fd; 5372165Sphantom char * lbuf; 5472165Sphantom char * p; 5572165Sphantom const char * plim; 5672165Sphantom char filename[PATH_MAX]; 5772165Sphantom struct stat st; 5872165Sphantom size_t namesize; 5972165Sphantom size_t bufsize; 6072165Sphantom int save_using_locale; 6172165Sphantom 6272165Sphantom save_using_locale = *using_locale; 6372165Sphantom *using_locale = 0; 6472165Sphantom 6572165Sphantom if (name == NULL) 6672165Sphantom goto no_locale; 6772165Sphantom 6872165Sphantom if (!strcmp(name, "C") || !strcmp(name, "POSIX")) 6972165Sphantom return 0; 7072165Sphantom 7172165Sphantom /* 7272165Sphantom ** If the locale name is the same as our cache, use the cache. 7372165Sphantom */ 7472165Sphantom lbuf = locale_buf; 7572165Sphantom if (lbuf != NULL && strcmp(name, lbuf) == 0) { 7672165Sphantom set_from_buf(lbuf, num_lines, dst_localebuf); 7772165Sphantom *using_locale = 1; 7872165Sphantom return 0; 7972165Sphantom } 8072165Sphantom /* 8172165Sphantom ** Slurp the locale file into the cache. 8272165Sphantom */ 8372165Sphantom namesize = strlen(name) + 1; 8472165Sphantom 8572165Sphantom if (!_PathLocale) 8672165Sphantom goto no_locale; 8772165Sphantom /* Range checking not needed, 'name' size is limited */ 8872165Sphantom strcpy(filename, _PathLocale); 8972165Sphantom strcat(filename, "/"); 9072165Sphantom strcat(filename, name); 9172165Sphantom strcat(filename, "/"); 9272165Sphantom strcat(filename, category_name); 9372165Sphantom fd = _open(filename, O_RDONLY); 9472165Sphantom if (fd < 0) 9572165Sphantom goto no_locale; 9672165Sphantom if (fstat(fd, &st) != 0) 9772165Sphantom goto bad_locale; 9872165Sphantom if (st.st_size <= 0) 9972165Sphantom goto bad_locale; 10072165Sphantom bufsize = namesize + st.st_size; 10172165Sphantom locale_buf = NULL; 10272165Sphantom lbuf = (lbuf == NULL || lbuf == locale_buf_C) ? 10372165Sphantom malloc(bufsize) : reallocf(lbuf, bufsize); 10472165Sphantom if (lbuf == NULL) 10572165Sphantom goto bad_locale; 10672165Sphantom (void) strcpy(lbuf, name); 10772165Sphantom p = lbuf + namesize; 10872165Sphantom plim = p + st.st_size; 10972165Sphantom if (_read(fd, p, (size_t) st.st_size) != st.st_size) 11072165Sphantom goto bad_lbuf; 11172165Sphantom if (_close(fd) != 0) 11272165Sphantom goto bad_lbuf; 11372165Sphantom /* 11472165Sphantom ** Parse the locale file into localebuf. 11572165Sphantom */ 11672165Sphantom if (plim[-1] != '\n') 11772165Sphantom goto bad_lbuf; 11872165Sphantom num_lines = split_lines(p, plim); 11972165Sphantom if (num_lines >= locale_buf_size) 12072165Sphantom num_lines = locale_buf_size; 12172165Sphantom else 12272165Sphantom goto reset_locale; 12372165Sphantom set_from_buf(lbuf, num_lines, dst_localebuf); 12472165Sphantom /* 12572165Sphantom ** Record the successful parse in the cache. 12672165Sphantom */ 12772165Sphantom locale_buf = lbuf; 12872165Sphantom 12972165Sphantom *using_locale = 1; 13072165Sphantom return 0; 13172165Sphantom 13272165Sphantomreset_locale: 13372165Sphantom locale_buf = locale_buf_C; 13472165Sphantom save_using_locale = 0; 13572165Sphantombad_lbuf: 13672165Sphantom free(lbuf); 13772165Sphantombad_locale: 13872165Sphantom (void)_close(fd); 13972165Sphantomno_locale: 14072165Sphantom *using_locale = save_using_locale; 14172165Sphantom return -1; 14272165Sphantom} 14372165Sphantom 14472165Sphantomstatic int 14572165Sphantomsplit_lines(char *p, const char *plim) 14672165Sphantom{ 14772165Sphantom int i; 14872165Sphantom 14972165Sphantom for (i = 0; p < plim; i++) { 15072165Sphantom p = strchr(p, '\n'); 15172165Sphantom *p++ = '\0'; 15272165Sphantom } 15372165Sphantom return i; 15472165Sphantom} 15572165Sphantom 15672165Sphantomstatic void 15772165Sphantomset_from_buf(const char *p, int num_lines, const char **dst_localebuf) 15872165Sphantom{ 15972165Sphantom const char **ap; 16072165Sphantom int i; 16172165Sphantom 16272165Sphantom for (ap = dst_localebuf, i = 0; i < num_lines; ++ap, ++i) 16372165Sphantom *ap = p += strlen(p) + 1; 16472165Sphantom} 16572165Sphantom 166