xref: /onnv-gate/usr/src/lib/libc/port/gen/catopen.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * catopen.c
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
34*6812Sraf #pragma weak _catopen = catopen
35*6812Sraf #pragma weak _catclose = catclose
360Sstevel@tonic-gate 
37*6812Sraf #include "lint.h"
380Sstevel@tonic-gate #include "libc.h"
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <unistd.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <string.h>
430Sstevel@tonic-gate #include <sys/stat.h>
440Sstevel@tonic-gate #include <sys/mman.h>
450Sstevel@tonic-gate #include <nl_types.h>
460Sstevel@tonic-gate #include <locale.h>
470Sstevel@tonic-gate #include <limits.h>
480Sstevel@tonic-gate #include <errno.h>
490Sstevel@tonic-gate #include "../i18n/_loc_path.h"
500Sstevel@tonic-gate #include "nlspath_checks.h"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #define	SAFE_F		1
530Sstevel@tonic-gate #define	UNSAFE_F	0
540Sstevel@tonic-gate 
550Sstevel@tonic-gate static char *
560Sstevel@tonic-gate replace_nls_option(char *, char *, char *, char *, char *, char *, char *);
570Sstevel@tonic-gate static nl_catd file_open(const char *, int);
580Sstevel@tonic-gate static nl_catd process_nls_path(char *, int);
590Sstevel@tonic-gate 
600Sstevel@tonic-gate nl_catd
catopen(const char * name,int oflag)61*6812Sraf catopen(const char *name, int oflag)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	nl_catd p;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	if (!name) {				/* Null pointer */
660Sstevel@tonic-gate 		errno = EFAULT;
670Sstevel@tonic-gate 		return ((nl_catd)-1);
680Sstevel@tonic-gate 	} else if (!*name) {		/* Empty string */
690Sstevel@tonic-gate 		errno = ENOENT;
700Sstevel@tonic-gate 		return ((nl_catd)-1);
710Sstevel@tonic-gate 	} else if (strchr(name, '/') != NULL) {
720Sstevel@tonic-gate 		/* If name contains '/', then it is complete file name */
730Sstevel@tonic-gate 		p = file_open(name, SAFE_F);
740Sstevel@tonic-gate 	} else {				/* Normal case */
750Sstevel@tonic-gate 		p = process_nls_path((char *)name, oflag);
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	if (p == NULL) {  /* Opening catalog file failed */
790Sstevel@tonic-gate 		return ((nl_catd)-1);
800Sstevel@tonic-gate 	} else {
810Sstevel@tonic-gate 		return (p);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate }
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * This routine will process NLSPATH environment variable.
880Sstevel@tonic-gate  * It will return catd id whenever it finds valid catalog.
890Sstevel@tonic-gate  */
900Sstevel@tonic-gate static nl_catd
process_nls_path(char * name,int oflag)910Sstevel@tonic-gate process_nls_path(char *name, int oflag)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	char	*s, *s1, *s2, *t;
940Sstevel@tonic-gate 	char	*nlspath, *lang, *territory, *codeset, *locale;
950Sstevel@tonic-gate 	char	pathname[PATH_MAX + 1];
960Sstevel@tonic-gate 	nl_catd	p;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	/*
990Sstevel@tonic-gate 	 * locale=language_territory.codeset
1000Sstevel@tonic-gate 	 * XPG4 uses LC_MESSAGES.
1010Sstevel@tonic-gate 	 * XPG3 uses LANG.
1020Sstevel@tonic-gate 	 * From the following two lines, choose one depending on XPG3 or 4.
1030Sstevel@tonic-gate 	 *
1040Sstevel@tonic-gate 	 * Chose XPG4. If oflag == NL_CAT_LOCALE, use LC_MESSAGES.
1050Sstevel@tonic-gate 	 */
1060Sstevel@tonic-gate 	if (oflag == NL_CAT_LOCALE)
1070Sstevel@tonic-gate 		locale = setlocale(LC_MESSAGES, NULL);
1080Sstevel@tonic-gate 	else
1090Sstevel@tonic-gate 		locale = getenv("LANG");
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	nlspath = getenv("NLSPATH");
1120Sstevel@tonic-gate 	lang = NULL;
1130Sstevel@tonic-gate 	if (nlspath) {
1140Sstevel@tonic-gate 		territory = NULL;
1150Sstevel@tonic-gate 		codeset = NULL;
1160Sstevel@tonic-gate 		/*
1170Sstevel@tonic-gate 		 * extract lang, territory and codeset from locale name
1180Sstevel@tonic-gate 		 */
1190Sstevel@tonic-gate 		if (locale) {
1200Sstevel@tonic-gate 			lang = s = libc_strdup(locale);
1210Sstevel@tonic-gate 			if (!lang) {
1220Sstevel@tonic-gate 				/* strdup failed */
1230Sstevel@tonic-gate 				return (NULL);
1240Sstevel@tonic-gate 			}
1250Sstevel@tonic-gate 			s1 = s2 = NULL;
1260Sstevel@tonic-gate 			while (s && *s) {
1270Sstevel@tonic-gate 				if (*s == '_') {
1280Sstevel@tonic-gate 					s1 = s;
1290Sstevel@tonic-gate 					*s1++ = NULL;
1300Sstevel@tonic-gate 				} else if (*s == '.') {
1310Sstevel@tonic-gate 					s2 = s;
1320Sstevel@tonic-gate 					*s2++ = NULL;
1330Sstevel@tonic-gate 				}
1340Sstevel@tonic-gate 				s++;
1350Sstevel@tonic-gate 			}
1360Sstevel@tonic-gate 			territory = s1;
1370Sstevel@tonic-gate 			codeset   = s2;
1380Sstevel@tonic-gate 		} /* if (locale) */
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 		/*
1410Sstevel@tonic-gate 		 * March through NLSPATH until finds valid cat file
1420Sstevel@tonic-gate 		 */
1430Sstevel@tonic-gate 		s = nlspath;
1440Sstevel@tonic-gate 		while (*s) {
1450Sstevel@tonic-gate 			if (*s == ':') {
1460Sstevel@tonic-gate 				/* unqualified pathname is unsafe */
1470Sstevel@tonic-gate 				p = file_open(name, UNSAFE_F);
1480Sstevel@tonic-gate 				if (p != NULL) {
1490Sstevel@tonic-gate 					if (lang)
1500Sstevel@tonic-gate 						libc_free(lang);
1510Sstevel@tonic-gate 					return (p);
1520Sstevel@tonic-gate 				}
1530Sstevel@tonic-gate 				++s;
1540Sstevel@tonic-gate 				continue;
1550Sstevel@tonic-gate 			}
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 			/* replace Substitution field */
1580Sstevel@tonic-gate 			s = replace_nls_option(s, name, pathname, locale,
159*6812Sraf 			    lang, territory, codeset);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 			p = file_open(pathname, UNSAFE_F);
1620Sstevel@tonic-gate 			if (p != NULL) {
1630Sstevel@tonic-gate 				if (lang)
1640Sstevel@tonic-gate 					libc_free(lang);
1650Sstevel@tonic-gate 				return (p);
1660Sstevel@tonic-gate 			}
1670Sstevel@tonic-gate 			if (*s)
1680Sstevel@tonic-gate 				++s;
1690Sstevel@tonic-gate 		} /* while */
1700Sstevel@tonic-gate 	} /* if (nlspath) */
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	/* lang is not used any more, free it */
1730Sstevel@tonic-gate 	if (lang)
1740Sstevel@tonic-gate 		libc_free(lang);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	/*
1770Sstevel@tonic-gate 	 * Implementation dependent default location of XPG3.
1780Sstevel@tonic-gate 	 * We use /usr/lib/locale/<locale>/LC_MESSAGES/%N.
1790Sstevel@tonic-gate 	 * If C locale, do not translate message.
1800Sstevel@tonic-gate 	 */
1810Sstevel@tonic-gate 	if (locale == NULL) {
1820Sstevel@tonic-gate 		return (NULL);
1830Sstevel@tonic-gate 	} else if (locale[0] == 'C' && locale[1] == '\0') {
1840Sstevel@tonic-gate 		p = libc_malloc(sizeof (struct _nl_catd_struct));
1850Sstevel@tonic-gate 		if (p == NULL) {
1860Sstevel@tonic-gate 			/* malloc failed */
1870Sstevel@tonic-gate 			return (NULL);
1880Sstevel@tonic-gate 		}
1890Sstevel@tonic-gate 		p->__content = NULL;
1900Sstevel@tonic-gate 		p->__size = 0;
1910Sstevel@tonic-gate 		p->__trust = 1;
1920Sstevel@tonic-gate 		return (p);
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	s = _DFLT_LOC_PATH;
1960Sstevel@tonic-gate 	t = pathname;
1970Sstevel@tonic-gate 	while (*t++ = *s++)
1980Sstevel@tonic-gate 		continue;
1990Sstevel@tonic-gate 	t--;
2000Sstevel@tonic-gate 	s = locale;
2010Sstevel@tonic-gate 	while (*s && t < pathname + PATH_MAX)
2020Sstevel@tonic-gate 		*t++ = *s++;
2030Sstevel@tonic-gate 	s = "/LC_MESSAGES/";
2040Sstevel@tonic-gate 	while (*s && t < pathname + PATH_MAX)
2050Sstevel@tonic-gate 		*t++ = *s++;
2060Sstevel@tonic-gate 	s = name;
2070Sstevel@tonic-gate 	while (*s && t < pathname + PATH_MAX)
2080Sstevel@tonic-gate 		*t++ = *s++;
2090Sstevel@tonic-gate 	*t = NULL;
2100Sstevel@tonic-gate 	return (file_open(pathname, SAFE_F));
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  * This routine will replace substitution parameters in NLSPATH
2160Sstevel@tonic-gate  * with appropiate values. Returns expanded pathname.
2170Sstevel@tonic-gate  */
2180Sstevel@tonic-gate static char *
replace_nls_option(char * s,char * name,char * pathname,char * locale,char * lang,char * territory,char * codeset)2190Sstevel@tonic-gate replace_nls_option(char *s, char *name, char *pathname, char *locale,
2200Sstevel@tonic-gate 	char *lang, char *territory, char *codeset)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate 	char	*t, *u;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	t = pathname;
2250Sstevel@tonic-gate 	while (*s && *s != ':') {
2260Sstevel@tonic-gate 		if (t < pathname + PATH_MAX) {
2270Sstevel@tonic-gate 			/*
2280Sstevel@tonic-gate 			 * %% is considered a single % character (XPG).
2290Sstevel@tonic-gate 			 * %L : LC_MESSAGES (XPG4) LANG(XPG3)
2300Sstevel@tonic-gate 			 * %l : The language element from the current locale.
2310Sstevel@tonic-gate 			 *	(XPG3, XPG4)
2320Sstevel@tonic-gate 			 */
2330Sstevel@tonic-gate 			if (*s != '%')
2340Sstevel@tonic-gate 				*t++ = *s;
2350Sstevel@tonic-gate 			else if (*++s == 'N') {
2360Sstevel@tonic-gate 				u = name;
2370Sstevel@tonic-gate 				while (*u && t < pathname + PATH_MAX)
2380Sstevel@tonic-gate 					*t++ = *u++;
2390Sstevel@tonic-gate 			} else if (*s == 'L') {
2400Sstevel@tonic-gate 				if (locale) {
2410Sstevel@tonic-gate 					u = locale;
2420Sstevel@tonic-gate 					while (*u && t < pathname + PATH_MAX)
2430Sstevel@tonic-gate 						*t++ = *u++;
2440Sstevel@tonic-gate 				}
2450Sstevel@tonic-gate 			} else if (*s == 'l') {
2460Sstevel@tonic-gate 				if (lang) {
2470Sstevel@tonic-gate 					u = lang;
2480Sstevel@tonic-gate 					while (*u && *u != '_' &&
249*6812Sraf 					    t < pathname + PATH_MAX)
2500Sstevel@tonic-gate 						*t++ = *u++;
2510Sstevel@tonic-gate 				}
2520Sstevel@tonic-gate 			} else if (*s == 't') {
2530Sstevel@tonic-gate 				if (territory) {
2540Sstevel@tonic-gate 					u = territory;
2550Sstevel@tonic-gate 					while (*u && *u != '.' &&
256*6812Sraf 					    t < pathname + PATH_MAX)
2570Sstevel@tonic-gate 						*t++ = *u++;
2580Sstevel@tonic-gate 				}
2590Sstevel@tonic-gate 			} else if (*s == 'c') {
2600Sstevel@tonic-gate 				if (codeset) {
2610Sstevel@tonic-gate 					u = codeset;
2620Sstevel@tonic-gate 					while (*u && t < pathname + PATH_MAX)
2630Sstevel@tonic-gate 						*t++ = *u++;
2640Sstevel@tonic-gate 				}
2650Sstevel@tonic-gate 			} else {
2660Sstevel@tonic-gate 				if (t < pathname + PATH_MAX)
2670Sstevel@tonic-gate 					*t++ = *s;
2680Sstevel@tonic-gate 			}
2690Sstevel@tonic-gate 		}
2700Sstevel@tonic-gate 		++s;
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 	*t = NULL;
2730Sstevel@tonic-gate 	return (s);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate /*
2770Sstevel@tonic-gate  * This routine will open file, mmap it, and return catd id.
2780Sstevel@tonic-gate  */
2790Sstevel@tonic-gate static nl_catd
file_open(const char * name,int safe)2800Sstevel@tonic-gate file_open(const char *name, int safe)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	int		fd;
2830Sstevel@tonic-gate 	struct stat64	statbuf;
2840Sstevel@tonic-gate 	void		*addr;
2850Sstevel@tonic-gate 	struct _cat_hdr	*tmp;
2860Sstevel@tonic-gate 	nl_catd		tmp_catd;
2870Sstevel@tonic-gate 	int		trust;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	fd = nls_safe_open(name, &statbuf, &trust, safe);
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	if (fd == -1) {
2920Sstevel@tonic-gate 		return (NULL);
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	addr = mmap(0, (size_t)statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
2960Sstevel@tonic-gate 	(void) close(fd);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	if (addr == MAP_FAILED) {
2990Sstevel@tonic-gate 		return (NULL);
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	/* check MAGIC number of catalogue file */
3030Sstevel@tonic-gate 	tmp = (struct _cat_hdr *)addr;
3040Sstevel@tonic-gate 	if (tmp->__hdr_magic != _CAT_MAGIC) {
3050Sstevel@tonic-gate 		(void) munmap(addr, (size_t)statbuf.st_size);
3060Sstevel@tonic-gate 		return (NULL);
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	tmp_catd = libc_malloc(sizeof (struct _nl_catd_struct));
3100Sstevel@tonic-gate 	if (tmp_catd == NULL) {
3110Sstevel@tonic-gate 		/* malloc failed */
3120Sstevel@tonic-gate 		(void) munmap(addr, statbuf.st_size);
3130Sstevel@tonic-gate 		return (NULL);
3140Sstevel@tonic-gate 	}
3150Sstevel@tonic-gate 	tmp_catd->__content = addr;
3160Sstevel@tonic-gate 	tmp_catd->__size = (int)statbuf.st_size;
3170Sstevel@tonic-gate 	tmp_catd->__trust = trust;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	return (tmp_catd);
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate int
catclose(nl_catd catd)323*6812Sraf catclose(nl_catd catd)
3240Sstevel@tonic-gate {
3250Sstevel@tonic-gate 	if (catd &&
3260Sstevel@tonic-gate 	    catd != (nl_catd)-1) {
3270Sstevel@tonic-gate 		if (catd->__content) {
3280Sstevel@tonic-gate 			(void) munmap(catd->__content, catd->__size);
3290Sstevel@tonic-gate 			catd->__content = NULL;
3300Sstevel@tonic-gate 		}
3310Sstevel@tonic-gate 		catd->__size = 0;
3320Sstevel@tonic-gate 		catd->__trust = 0;
3330Sstevel@tonic-gate 		libc_free(catd);
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 	return (0);
3360Sstevel@tonic-gate }
337