190423eceSAlexey Zelkin /* 274f2b975SAlexey Zelkin * Copyright (c) 2000, 2001 Alexey Zelkin <phantom@FreeBSD.org> 390423eceSAlexey Zelkin * All rights reserved. 490423eceSAlexey Zelkin * 590423eceSAlexey Zelkin * Redistribution and use in source and binary forms, with or without 690423eceSAlexey Zelkin * modification, are permitted provided that the following conditions 790423eceSAlexey Zelkin * are met: 890423eceSAlexey Zelkin * 1. Redistributions of source code must retain the above copyright 990423eceSAlexey Zelkin * notice, this list of conditions and the following disclaimer. 1090423eceSAlexey Zelkin * 2. Redistributions in binary form must reproduce the above copyright 1190423eceSAlexey Zelkin * notice, this list of conditions and the following disclaimer in the 1290423eceSAlexey Zelkin * documentation and/or other materials provided with the distribution. 1390423eceSAlexey Zelkin * 1490423eceSAlexey Zelkin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1590423eceSAlexey Zelkin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1690423eceSAlexey Zelkin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1790423eceSAlexey Zelkin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1890423eceSAlexey Zelkin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1990423eceSAlexey Zelkin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2090423eceSAlexey Zelkin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2190423eceSAlexey Zelkin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2290423eceSAlexey Zelkin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2390423eceSAlexey Zelkin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2490423eceSAlexey Zelkin * SUCH DAMAGE. 2590423eceSAlexey Zelkin */ 2690423eceSAlexey Zelkin 27333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 28333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 29333fc21eSDavid E. O'Brien 307d2cc62bSAlexey Zelkin #include "namespace.h" 3190423eceSAlexey Zelkin #include <sys/types.h> 3290423eceSAlexey Zelkin #include <sys/stat.h> 33688dfe45SGarrett Wollman 34f75bb0aaSAndrey A. Chernov #include <errno.h> 3590423eceSAlexey Zelkin #include <fcntl.h> 36688dfe45SGarrett Wollman #include <limits.h> 3790423eceSAlexey Zelkin #include <stdlib.h> 3890423eceSAlexey Zelkin #include <string.h> 39278d1a20SDaniel Eischen #include <unistd.h> 40278d1a20SDaniel Eischen #include "un-namespace.h" 4174f2b975SAlexey Zelkin 4290423eceSAlexey Zelkin #include "ldpart.h" 43683fe113SAlexey Zelkin #include "setlocale.h" 4490423eceSAlexey Zelkin 4590423eceSAlexey Zelkin static int split_lines(char *, const char *); 4690423eceSAlexey Zelkin 4790423eceSAlexey Zelkin int 4890423eceSAlexey Zelkin __part_load_locale(const char *name, 4990423eceSAlexey Zelkin int *using_locale, 503dfdc427SJordan K. Hubbard char **locale_buf, 51f43a321bSAlexey Zelkin const char *category_filename, 52fc38c1e5SAlexey Zelkin int locale_buf_size_max, 53fc38c1e5SAlexey Zelkin int locale_buf_size_min, 54f75bb0aaSAndrey A. Chernov const char **dst_localebuf) 55f75bb0aaSAndrey A. Chernov { 5676692b80SAndrey A. Chernov int saverr, fd, i, num_lines; 5776692b80SAndrey A. Chernov char *lbuf, *p; 5890423eceSAlexey Zelkin const char *plim; 5990423eceSAlexey Zelkin char filename[PATH_MAX]; 6090423eceSAlexey Zelkin struct stat st; 6176692b80SAndrey A. Chernov size_t namesize, bufsize; 6290423eceSAlexey Zelkin 63f75bb0aaSAndrey A. Chernov /* 'name' must be already checked. */ 6476692b80SAndrey A. Chernov if (strcmp(name, "C") == 0 || strcmp(name, "POSIX") == 0) { 6576692b80SAndrey A. Chernov *using_locale = 0; 6676692b80SAndrey A. Chernov return (_LDP_CACHE); 6776692b80SAndrey A. Chernov } 6890423eceSAlexey Zelkin 6990423eceSAlexey Zelkin /* 7074f2b975SAlexey Zelkin * If the locale name is the same as our cache, use the cache. 7190423eceSAlexey Zelkin */ 723dfdc427SJordan K. Hubbard if (*locale_buf != NULL && strcmp(name, *locale_buf) == 0) { 7390423eceSAlexey Zelkin *using_locale = 1; 7476692b80SAndrey A. Chernov return (_LDP_CACHE); 7590423eceSAlexey Zelkin } 76f43a321bSAlexey Zelkin 7790423eceSAlexey Zelkin /* 7874f2b975SAlexey Zelkin * Slurp the locale file into the cache. 7990423eceSAlexey Zelkin */ 8090423eceSAlexey Zelkin namesize = strlen(name) + 1; 8190423eceSAlexey Zelkin 82f75bb0aaSAndrey A. Chernov /* 'PathLocale' must be already set & checked. */ 83683fe113SAlexey Zelkin 8490423eceSAlexey Zelkin /* Range checking not needed, 'name' size is limited */ 8590423eceSAlexey Zelkin strcpy(filename, _PathLocale); 8690423eceSAlexey Zelkin strcat(filename, "/"); 8790423eceSAlexey Zelkin strcat(filename, name); 8890423eceSAlexey Zelkin strcat(filename, "/"); 89f43a321bSAlexey Zelkin strcat(filename, category_filename); 9076692b80SAndrey A. Chernov if ((fd = _open(filename, O_RDONLY)) < 0) 9176692b80SAndrey A. Chernov return (_LDP_ERROR); 923b5b529fSDaniel Eischen if (_fstat(fd, &st) != 0) 9390423eceSAlexey Zelkin goto bad_locale; 94f75bb0aaSAndrey A. Chernov if (st.st_size <= 0) { 95f75bb0aaSAndrey A. Chernov errno = EFTYPE; 9690423eceSAlexey Zelkin goto bad_locale; 97f75bb0aaSAndrey A. Chernov } 9890423eceSAlexey Zelkin bufsize = namesize + st.st_size; 9976692b80SAndrey A. Chernov if ((lbuf = malloc(bufsize)) == NULL) { 10076692b80SAndrey A. Chernov errno = ENOMEM; 10190423eceSAlexey Zelkin goto bad_locale; 10276692b80SAndrey A. Chernov } 10390423eceSAlexey Zelkin (void)strcpy(lbuf, name); 10490423eceSAlexey Zelkin p = lbuf + namesize; 10590423eceSAlexey Zelkin plim = p + st.st_size; 10690423eceSAlexey Zelkin if (_read(fd, p, (size_t) st.st_size) != st.st_size) 10790423eceSAlexey Zelkin goto bad_lbuf; 10890423eceSAlexey Zelkin /* 10974f2b975SAlexey Zelkin * Parse the locale file into localebuf. 11090423eceSAlexey Zelkin */ 111f75bb0aaSAndrey A. Chernov if (plim[-1] != '\n') { 112f75bb0aaSAndrey A. Chernov errno = EFTYPE; 11390423eceSAlexey Zelkin goto bad_lbuf; 114f75bb0aaSAndrey A. Chernov } 11590423eceSAlexey Zelkin num_lines = split_lines(p, plim); 116fc38c1e5SAlexey Zelkin if (num_lines >= locale_buf_size_max) 117fc38c1e5SAlexey Zelkin num_lines = locale_buf_size_max; 118fc38c1e5SAlexey Zelkin else if (num_lines >= locale_buf_size_min) 119fc38c1e5SAlexey Zelkin num_lines = locale_buf_size_min; 120f75bb0aaSAndrey A. Chernov else { 121f75bb0aaSAndrey A. Chernov errno = EFTYPE; 12276692b80SAndrey A. Chernov goto bad_lbuf; 123f75bb0aaSAndrey A. Chernov } 12476692b80SAndrey A. Chernov (void)_close(fd); 12590423eceSAlexey Zelkin /* 126f43a321bSAlexey Zelkin * Record the successful parse in the cache. 12790423eceSAlexey Zelkin */ 1283dfdc427SJordan K. Hubbard if (*locale_buf != NULL) 1293dfdc427SJordan K. Hubbard free(*locale_buf); 1303dfdc427SJordan K. Hubbard *locale_buf = lbuf; 1313dfdc427SJordan K. Hubbard for (p = *locale_buf, i = 0; i < num_lines; i++) 13276692b80SAndrey A. Chernov dst_localebuf[i] = (p += strlen(p) + 1); 13376692b80SAndrey A. Chernov for (i = num_lines; i < locale_buf_size_max; i++) 13476692b80SAndrey A. Chernov dst_localebuf[i] = NULL; 13590423eceSAlexey Zelkin *using_locale = 1; 136ecc4c620SAndrey A. Chernov 13776692b80SAndrey A. Chernov return (_LDP_LOADED); 13890423eceSAlexey Zelkin 13990423eceSAlexey Zelkin bad_lbuf: 140ecc4c620SAndrey A. Chernov saverr = errno; 141ecc4c620SAndrey A. Chernov free(lbuf); 142ecc4c620SAndrey A. Chernov errno = saverr; 14390423eceSAlexey Zelkin bad_locale: 144ecc4c620SAndrey A. Chernov saverr = errno; 145ecc4c620SAndrey A. Chernov (void)_close(fd); 146ecc4c620SAndrey A. Chernov errno = saverr; 147ecc4c620SAndrey A. Chernov 14876692b80SAndrey A. Chernov return (_LDP_ERROR); 14990423eceSAlexey Zelkin } 15090423eceSAlexey Zelkin 15190423eceSAlexey Zelkin static int 152ecc4c620SAndrey A. Chernov split_lines(char *p, const char *plim) 153ecc4c620SAndrey A. Chernov { 15490423eceSAlexey Zelkin int i; 15590423eceSAlexey Zelkin 15628aec5a6SAndrey A. Chernov i = 0; 15728aec5a6SAndrey A. Chernov while (p < plim) { 15828aec5a6SAndrey A. Chernov if (*p == '\n') { 15928aec5a6SAndrey A. Chernov *p = '\0'; 16028aec5a6SAndrey A. Chernov i++; 16128aec5a6SAndrey A. Chernov } 16228aec5a6SAndrey A. Chernov p++; 16390423eceSAlexey Zelkin } 164ecc4c620SAndrey A. Chernov return (i); 16590423eceSAlexey Zelkin } 16690423eceSAlexey Zelkin 167