17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57257d1b4Sraf * Common Development and Distribution License (the "License").
67257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217257d1b4Sraf
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate * catopen.c
297c478bd9Sstevel@tonic-gate *
307c478bd9Sstevel@tonic-gate */
317c478bd9Sstevel@tonic-gate
327257d1b4Sraf #pragma weak _catopen = catopen
337257d1b4Sraf #pragma weak _catclose = catclose
347c478bd9Sstevel@tonic-gate
357257d1b4Sraf #include "lint.h"
367c478bd9Sstevel@tonic-gate #include "libc.h"
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <stdlib.h>
407c478bd9Sstevel@tonic-gate #include <string.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include <sys/mman.h>
437c478bd9Sstevel@tonic-gate #include <nl_types.h>
447c478bd9Sstevel@tonic-gate #include <locale.h>
457c478bd9Sstevel@tonic-gate #include <limits.h>
467c478bd9Sstevel@tonic-gate #include <errno.h>
477c478bd9Sstevel@tonic-gate #include "../i18n/_loc_path.h"
487c478bd9Sstevel@tonic-gate #include "nlspath_checks.h"
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate #define SAFE_F 1
517c478bd9Sstevel@tonic-gate #define UNSAFE_F 0
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate static char *
547c478bd9Sstevel@tonic-gate replace_nls_option(char *, char *, char *, char *, char *, char *, char *);
557c478bd9Sstevel@tonic-gate static nl_catd file_open(const char *, int);
567c478bd9Sstevel@tonic-gate static nl_catd process_nls_path(char *, int);
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate nl_catd
catopen(const char * name,int oflag)597257d1b4Sraf catopen(const char *name, int oflag)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate nl_catd p;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate if (!name) { /* Null pointer */
647c478bd9Sstevel@tonic-gate errno = EFAULT;
657c478bd9Sstevel@tonic-gate return ((nl_catd)-1);
667c478bd9Sstevel@tonic-gate } else if (!*name) { /* Empty string */
677c478bd9Sstevel@tonic-gate errno = ENOENT;
687c478bd9Sstevel@tonic-gate return ((nl_catd)-1);
697c478bd9Sstevel@tonic-gate } else if (strchr(name, '/') != NULL) {
707c478bd9Sstevel@tonic-gate /* If name contains '/', then it is complete file name */
717c478bd9Sstevel@tonic-gate p = file_open(name, SAFE_F);
727c478bd9Sstevel@tonic-gate } else { /* Normal case */
737c478bd9Sstevel@tonic-gate p = process_nls_path((char *)name, oflag);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate if (p == NULL) { /* Opening catalog file failed */
777c478bd9Sstevel@tonic-gate return ((nl_catd)-1);
787c478bd9Sstevel@tonic-gate } else {
797c478bd9Sstevel@tonic-gate return (p);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate * This routine will process NLSPATH environment variable.
867c478bd9Sstevel@tonic-gate * It will return catd id whenever it finds valid catalog.
877c478bd9Sstevel@tonic-gate */
887c478bd9Sstevel@tonic-gate static nl_catd
process_nls_path(char * name,int oflag)897c478bd9Sstevel@tonic-gate process_nls_path(char *name, int oflag)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate char *s, *s1, *s2, *t;
927c478bd9Sstevel@tonic-gate char *nlspath, *lang, *territory, *codeset, *locale;
937c478bd9Sstevel@tonic-gate char pathname[PATH_MAX + 1];
947c478bd9Sstevel@tonic-gate nl_catd p;
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate * locale=language_territory.codeset
987c478bd9Sstevel@tonic-gate * XPG4 uses LC_MESSAGES.
997c478bd9Sstevel@tonic-gate * XPG3 uses LANG.
1007c478bd9Sstevel@tonic-gate * From the following two lines, choose one depending on XPG3 or 4.
1017c478bd9Sstevel@tonic-gate *
1027c478bd9Sstevel@tonic-gate * Chose XPG4. If oflag == NL_CAT_LOCALE, use LC_MESSAGES.
1037c478bd9Sstevel@tonic-gate */
104*b599bd93SRobert Mustacchi if (oflag == NL_CAT_LOCALE) {
105*b599bd93SRobert Mustacchi locale_t loc = uselocale(NULL);
106*b599bd93SRobert Mustacchi locale = current_locale(loc, LC_MESSAGES);
107*b599bd93SRobert Mustacchi } else {
1087c478bd9Sstevel@tonic-gate locale = getenv("LANG");
109*b599bd93SRobert Mustacchi }
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate nlspath = getenv("NLSPATH");
1127c478bd9Sstevel@tonic-gate lang = NULL;
1137c478bd9Sstevel@tonic-gate if (nlspath) {
1147c478bd9Sstevel@tonic-gate territory = NULL;
1157c478bd9Sstevel@tonic-gate codeset = NULL;
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate * extract lang, territory and codeset from locale name
1187c478bd9Sstevel@tonic-gate */
1197c478bd9Sstevel@tonic-gate if (locale) {
1207c478bd9Sstevel@tonic-gate lang = s = libc_strdup(locale);
1217c478bd9Sstevel@tonic-gate if (!lang) {
1227c478bd9Sstevel@tonic-gate /* strdup failed */
1237c478bd9Sstevel@tonic-gate return (NULL);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate s1 = s2 = NULL;
1267c478bd9Sstevel@tonic-gate while (s && *s) {
1277c478bd9Sstevel@tonic-gate if (*s == '_') {
1287c478bd9Sstevel@tonic-gate s1 = s;
1297c478bd9Sstevel@tonic-gate *s1++ = NULL;
1307c478bd9Sstevel@tonic-gate } else if (*s == '.') {
1317c478bd9Sstevel@tonic-gate s2 = s;
1327c478bd9Sstevel@tonic-gate *s2++ = NULL;
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate s++;
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate territory = s1;
1377c478bd9Sstevel@tonic-gate codeset = s2;
1387c478bd9Sstevel@tonic-gate } /* if (locale) */
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate * March through NLSPATH until finds valid cat file
1427c478bd9Sstevel@tonic-gate */
1437c478bd9Sstevel@tonic-gate s = nlspath;
1447c478bd9Sstevel@tonic-gate while (*s) {
1457c478bd9Sstevel@tonic-gate if (*s == ':') {
1467c478bd9Sstevel@tonic-gate /* unqualified pathname is unsafe */
1477c478bd9Sstevel@tonic-gate p = file_open(name, UNSAFE_F);
1487c478bd9Sstevel@tonic-gate if (p != NULL) {
1497c478bd9Sstevel@tonic-gate if (lang)
1507c478bd9Sstevel@tonic-gate libc_free(lang);
1517c478bd9Sstevel@tonic-gate return (p);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate ++s;
1547c478bd9Sstevel@tonic-gate continue;
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate /* replace Substitution field */
1587c478bd9Sstevel@tonic-gate s = replace_nls_option(s, name, pathname, locale,
1597c478bd9Sstevel@tonic-gate lang, territory, codeset);
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate p = file_open(pathname, UNSAFE_F);
1627c478bd9Sstevel@tonic-gate if (p != NULL) {
1637c478bd9Sstevel@tonic-gate if (lang)
1647c478bd9Sstevel@tonic-gate libc_free(lang);
1657c478bd9Sstevel@tonic-gate return (p);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate if (*s)
1687c478bd9Sstevel@tonic-gate ++s;
1697c478bd9Sstevel@tonic-gate } /* while */
1707c478bd9Sstevel@tonic-gate } /* if (nlspath) */
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate /* lang is not used any more, free it */
1737c478bd9Sstevel@tonic-gate if (lang)
1747c478bd9Sstevel@tonic-gate libc_free(lang);
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate * Implementation dependent default location of XPG3.
1787c478bd9Sstevel@tonic-gate * We use /usr/lib/locale/<locale>/LC_MESSAGES/%N.
1797c478bd9Sstevel@tonic-gate * If C locale, do not translate message.
1807c478bd9Sstevel@tonic-gate */
1817c478bd9Sstevel@tonic-gate if (locale == NULL) {
1827c478bd9Sstevel@tonic-gate return (NULL);
1837c478bd9Sstevel@tonic-gate } else if (locale[0] == 'C' && locale[1] == '\0') {
1847c478bd9Sstevel@tonic-gate p = libc_malloc(sizeof (struct _nl_catd_struct));
1857c478bd9Sstevel@tonic-gate if (p == NULL) {
1867c478bd9Sstevel@tonic-gate /* malloc failed */
1877c478bd9Sstevel@tonic-gate return (NULL);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate p->__content = NULL;
1907c478bd9Sstevel@tonic-gate p->__size = 0;
1917c478bd9Sstevel@tonic-gate p->__trust = 1;
1927c478bd9Sstevel@tonic-gate return (p);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate s = _DFLT_LOC_PATH;
1967c478bd9Sstevel@tonic-gate t = pathname;
1977c478bd9Sstevel@tonic-gate while (*t++ = *s++)
1987c478bd9Sstevel@tonic-gate continue;
1997c478bd9Sstevel@tonic-gate t--;
2007c478bd9Sstevel@tonic-gate s = locale;
2017c478bd9Sstevel@tonic-gate while (*s && t < pathname + PATH_MAX)
2027c478bd9Sstevel@tonic-gate *t++ = *s++;
2037c478bd9Sstevel@tonic-gate s = "/LC_MESSAGES/";
2047c478bd9Sstevel@tonic-gate while (*s && t < pathname + PATH_MAX)
2057c478bd9Sstevel@tonic-gate *t++ = *s++;
2067c478bd9Sstevel@tonic-gate s = name;
2077c478bd9Sstevel@tonic-gate while (*s && t < pathname + PATH_MAX)
2087c478bd9Sstevel@tonic-gate *t++ = *s++;
2097c478bd9Sstevel@tonic-gate *t = NULL;
2107c478bd9Sstevel@tonic-gate return (file_open(pathname, SAFE_F));
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate * This routine will replace substitution parameters in NLSPATH
2167c478bd9Sstevel@tonic-gate * with appropiate values. Returns expanded pathname.
2177c478bd9Sstevel@tonic-gate */
2187c478bd9Sstevel@tonic-gate static char *
replace_nls_option(char * s,char * name,char * pathname,char * locale,char * lang,char * territory,char * codeset)2197c478bd9Sstevel@tonic-gate replace_nls_option(char *s, char *name, char *pathname, char *locale,
2207c478bd9Sstevel@tonic-gate char *lang, char *territory, char *codeset)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate char *t, *u;
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate t = pathname;
2257c478bd9Sstevel@tonic-gate while (*s && *s != ':') {
2267c478bd9Sstevel@tonic-gate if (t < pathname + PATH_MAX) {
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate * %% is considered a single % character (XPG).
2297c478bd9Sstevel@tonic-gate * %L : LC_MESSAGES (XPG4) LANG(XPG3)
2307c478bd9Sstevel@tonic-gate * %l : The language element from the current locale.
2317c478bd9Sstevel@tonic-gate * (XPG3, XPG4)
2327c478bd9Sstevel@tonic-gate */
2337c478bd9Sstevel@tonic-gate if (*s != '%')
2347c478bd9Sstevel@tonic-gate *t++ = *s;
2357c478bd9Sstevel@tonic-gate else if (*++s == 'N') {
2367c478bd9Sstevel@tonic-gate u = name;
2377c478bd9Sstevel@tonic-gate while (*u && t < pathname + PATH_MAX)
2387c478bd9Sstevel@tonic-gate *t++ = *u++;
2397c478bd9Sstevel@tonic-gate } else if (*s == 'L') {
2407c478bd9Sstevel@tonic-gate if (locale) {
2417c478bd9Sstevel@tonic-gate u = locale;
2427c478bd9Sstevel@tonic-gate while (*u && t < pathname + PATH_MAX)
2437c478bd9Sstevel@tonic-gate *t++ = *u++;
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate } else if (*s == 'l') {
2467c478bd9Sstevel@tonic-gate if (lang) {
2477c478bd9Sstevel@tonic-gate u = lang;
2487c478bd9Sstevel@tonic-gate while (*u && *u != '_' &&
2497c478bd9Sstevel@tonic-gate t < pathname + PATH_MAX)
2507c478bd9Sstevel@tonic-gate *t++ = *u++;
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate } else if (*s == 't') {
2537c478bd9Sstevel@tonic-gate if (territory) {
2547c478bd9Sstevel@tonic-gate u = territory;
2557c478bd9Sstevel@tonic-gate while (*u && *u != '.' &&
2567c478bd9Sstevel@tonic-gate t < pathname + PATH_MAX)
2577c478bd9Sstevel@tonic-gate *t++ = *u++;
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate } else if (*s == 'c') {
2607c478bd9Sstevel@tonic-gate if (codeset) {
2617c478bd9Sstevel@tonic-gate u = codeset;
2627c478bd9Sstevel@tonic-gate while (*u && t < pathname + PATH_MAX)
2637c478bd9Sstevel@tonic-gate *t++ = *u++;
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate } else {
2667c478bd9Sstevel@tonic-gate if (t < pathname + PATH_MAX)
2677c478bd9Sstevel@tonic-gate *t++ = *s;
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate ++s;
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate *t = NULL;
2737c478bd9Sstevel@tonic-gate return (s);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate /*
2777c478bd9Sstevel@tonic-gate * This routine will open file, mmap it, and return catd id.
2787c478bd9Sstevel@tonic-gate */
2797c478bd9Sstevel@tonic-gate static nl_catd
file_open(const char * name,int safe)2807c478bd9Sstevel@tonic-gate file_open(const char *name, int safe)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate int fd;
2837c478bd9Sstevel@tonic-gate struct stat64 statbuf;
2847c478bd9Sstevel@tonic-gate void *addr;
2857c478bd9Sstevel@tonic-gate struct _cat_hdr *tmp;
2867c478bd9Sstevel@tonic-gate nl_catd tmp_catd;
2877c478bd9Sstevel@tonic-gate int trust;
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate fd = nls_safe_open(name, &statbuf, &trust, safe);
2907c478bd9Sstevel@tonic-gate
2917c478bd9Sstevel@tonic-gate if (fd == -1) {
2927c478bd9Sstevel@tonic-gate return (NULL);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate addr = mmap(0, (size_t)statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
2967c478bd9Sstevel@tonic-gate (void) close(fd);
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate if (addr == MAP_FAILED) {
2997c478bd9Sstevel@tonic-gate return (NULL);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate
3027c478bd9Sstevel@tonic-gate /* check MAGIC number of catalogue file */
3037c478bd9Sstevel@tonic-gate tmp = (struct _cat_hdr *)addr;
3047c478bd9Sstevel@tonic-gate if (tmp->__hdr_magic != _CAT_MAGIC) {
3057c478bd9Sstevel@tonic-gate (void) munmap(addr, (size_t)statbuf.st_size);
3067c478bd9Sstevel@tonic-gate return (NULL);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate tmp_catd = libc_malloc(sizeof (struct _nl_catd_struct));
3107c478bd9Sstevel@tonic-gate if (tmp_catd == NULL) {
3117c478bd9Sstevel@tonic-gate /* malloc failed */
3127c478bd9Sstevel@tonic-gate (void) munmap(addr, statbuf.st_size);
3137c478bd9Sstevel@tonic-gate return (NULL);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate tmp_catd->__content = addr;
3167c478bd9Sstevel@tonic-gate tmp_catd->__size = (int)statbuf.st_size;
3177c478bd9Sstevel@tonic-gate tmp_catd->__trust = trust;
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate return (tmp_catd);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate int
catclose(nl_catd catd)3237257d1b4Sraf catclose(nl_catd catd)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate if (catd &&
3267c478bd9Sstevel@tonic-gate catd != (nl_catd)-1) {
3277c478bd9Sstevel@tonic-gate if (catd->__content) {
3287c478bd9Sstevel@tonic-gate (void) munmap(catd->__content, catd->__size);
3297c478bd9Sstevel@tonic-gate catd->__content = NULL;
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate catd->__size = 0;
3327c478bd9Sstevel@tonic-gate catd->__trust = 0;
3337c478bd9Sstevel@tonic-gate libc_free(catd);
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate return (0);
3367c478bd9Sstevel@tonic-gate }
337