1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 30*0Sstevel@tonic-gate /* All Rights Reserved */ 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #pragma weak Msgdb = _Msgdb 34*0Sstevel@tonic-gate #pragma weak gettxt = _gettxt 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate #include "synonyms.h" 37*0Sstevel@tonic-gate #include <ctype.h> 38*0Sstevel@tonic-gate #include <string.h> 39*0Sstevel@tonic-gate #include <locale.h> 40*0Sstevel@tonic-gate #include <fcntl.h> 41*0Sstevel@tonic-gate #include <sys/types.h> 42*0Sstevel@tonic-gate #include <sys/file.h> 43*0Sstevel@tonic-gate #include <sys/mman.h> 44*0Sstevel@tonic-gate #include <sys/stat.h> 45*0Sstevel@tonic-gate #include <pfmt.h> 46*0Sstevel@tonic-gate #include <stdlib.h> 47*0Sstevel@tonic-gate #include <unistd.h> 48*0Sstevel@tonic-gate #include <limits.h> 49*0Sstevel@tonic-gate #include "../i18n/_locale.h" 50*0Sstevel@tonic-gate #include "../i18n/_loc_path.h" 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate #define MAXDB 10 /* maximum number of data bases per program */ 53*0Sstevel@tonic-gate #define MESSAGES "/LC_MESSAGES/" 54*0Sstevel@tonic-gate #define DB_NAME_LEN 15 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate char *handle_return(const char *); 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate /* support multiple versions of a package */ 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate char *Msgdb = (char *)NULL; 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate static char *saved_locale = NULL; 63*0Sstevel@tonic-gate static const char *not_found = "Message not found!!\n"; 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate static struct db_info { 66*0Sstevel@tonic-gate char db_name[DB_NAME_LEN]; /* name of the message file */ 67*0Sstevel@tonic-gate uintptr_t addr; /* virtual memory address */ 68*0Sstevel@tonic-gate size_t length; 69*0Sstevel@tonic-gate } *db_info; 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate static int db_count; /* number of currently accessible data bases */ 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate char * 74*0Sstevel@tonic-gate gettxt(const char *msg_id, const char *dflt_str) 75*0Sstevel@tonic-gate { 76*0Sstevel@tonic-gate char msgfile[DB_NAME_LEN]; /* name of static shared library */ 77*0Sstevel@tonic-gate int msgnum; /* message number */ 78*0Sstevel@tonic-gate char pathname[PATH_MAX]; /* full pathname to message file */ 79*0Sstevel@tonic-gate int i; 80*0Sstevel@tonic-gate int new_locale = 0; 81*0Sstevel@tonic-gate int fd; 82*0Sstevel@tonic-gate struct stat64 sb; 83*0Sstevel@tonic-gate void *addr; 84*0Sstevel@tonic-gate char *tokp; 85*0Sstevel@tonic-gate size_t name_len; 86*0Sstevel@tonic-gate char *curloc; 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate if ((msg_id == NULL) || (*msg_id == NULL)) { 89*0Sstevel@tonic-gate return (handle_return(dflt_str)); 90*0Sstevel@tonic-gate } 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate /* first time called, allocate space */ 93*0Sstevel@tonic-gate if (!db_info) { 94*0Sstevel@tonic-gate if ((db_info = (struct db_info *) \ 95*0Sstevel@tonic-gate malloc(MAXDB * sizeof (struct db_info))) == NULL) 96*0Sstevel@tonic-gate return (handle_return(dflt_str)); 97*0Sstevel@tonic-gate } 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gate /* parse msg_id */ 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate if (((tokp = strchr(msg_id, ':')) == NULL) || *(tokp+1) == '\0') 102*0Sstevel@tonic-gate return (handle_return(dflt_str)); 103*0Sstevel@tonic-gate if ((name_len = (tokp - msg_id)) >= DB_NAME_LEN) 104*0Sstevel@tonic-gate return (handle_return(dflt_str)); 105*0Sstevel@tonic-gate if (name_len) { 106*0Sstevel@tonic-gate (void) strncpy(msgfile, msg_id, name_len); 107*0Sstevel@tonic-gate msgfile[name_len] = '\0'; 108*0Sstevel@tonic-gate } else { 109*0Sstevel@tonic-gate if (Msgdb && strlen(Msgdb) < DB_NAME_LEN) 110*0Sstevel@tonic-gate (void) strcpy(msgfile, Msgdb); 111*0Sstevel@tonic-gate else { 112*0Sstevel@tonic-gate char *p; 113*0Sstevel@tonic-gate p = (char *)setcat((const char *)0); 114*0Sstevel@tonic-gate if ((p != NULL) && strlen(p) < DB_NAME_LEN) 115*0Sstevel@tonic-gate (void) strcpy(msgfile, p); 116*0Sstevel@tonic-gate else 117*0Sstevel@tonic-gate return (handle_return(dflt_str)); 118*0Sstevel@tonic-gate } 119*0Sstevel@tonic-gate } 120*0Sstevel@tonic-gate while (*++tokp) 121*0Sstevel@tonic-gate if (!isdigit(*tokp)) 122*0Sstevel@tonic-gate return (handle_return(dflt_str)); 123*0Sstevel@tonic-gate msgnum = atoi(msg_id + name_len + 1); 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate /* Has locale been changed? */ 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gate curloc = setlocale(LC_MESSAGES, NULL); 128*0Sstevel@tonic-gate if (saved_locale != NULL && strcmp(curloc, saved_locale) == 0) { 129*0Sstevel@tonic-gate for (i = 0; i < db_count; i++) 130*0Sstevel@tonic-gate if (strcmp(db_info[i].db_name, msgfile) == 0) 131*0Sstevel@tonic-gate break; 132*0Sstevel@tonic-gate } else { /* new locale - clear everything */ 133*0Sstevel@tonic-gate if (saved_locale) 134*0Sstevel@tonic-gate free(saved_locale); 135*0Sstevel@tonic-gate /* 136*0Sstevel@tonic-gate * allocate at least 2 bytes, so that we can copy "C" 137*0Sstevel@tonic-gate * without re-allocating the saved_locale. 138*0Sstevel@tonic-gate */ 139*0Sstevel@tonic-gate if ((saved_locale = malloc(strlen(curloc)+2)) == NULL) 140*0Sstevel@tonic-gate return (handle_return(dflt_str)); 141*0Sstevel@tonic-gate (void) strcpy(saved_locale, curloc); 142*0Sstevel@tonic-gate for (i = 0; i < db_count; i++) { 143*0Sstevel@tonic-gate (void) munmap((void *)db_info[i].addr, 144*0Sstevel@tonic-gate db_info[i].length); 145*0Sstevel@tonic-gate (void) strcpy(db_info[i].db_name, ""); 146*0Sstevel@tonic-gate new_locale++; 147*0Sstevel@tonic-gate } 148*0Sstevel@tonic-gate db_count = 0; 149*0Sstevel@tonic-gate } 150*0Sstevel@tonic-gate if (new_locale || i == db_count) { 151*0Sstevel@tonic-gate if (db_count == MAXDB) 152*0Sstevel@tonic-gate return (handle_return(dflt_str)); 153*0Sstevel@tonic-gate if (snprintf(pathname, sizeof (pathname), 154*0Sstevel@tonic-gate _DFLT_LOC_PATH "%s" MESSAGES "%s", 155*0Sstevel@tonic-gate saved_locale, msgfile) >= sizeof (pathname)) { 156*0Sstevel@tonic-gate return (handle_return(dflt_str)); 157*0Sstevel@tonic-gate } 158*0Sstevel@tonic-gate if ((fd = open(pathname, O_RDONLY)) == -1 || 159*0Sstevel@tonic-gate fstat64(fd, &sb) == -1 || 160*0Sstevel@tonic-gate (addr = mmap(0, (size_t)sb.st_size, 161*0Sstevel@tonic-gate PROT_READ, MAP_SHARED, 162*0Sstevel@tonic-gate fd, 0)) == MAP_FAILED) { 163*0Sstevel@tonic-gate if (fd != -1) 164*0Sstevel@tonic-gate (void) close(fd); 165*0Sstevel@tonic-gate if (strcmp(saved_locale, "C") == 0) 166*0Sstevel@tonic-gate return (handle_return(dflt_str)); 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate /* Change locale to C */ 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate if (snprintf(pathname, sizeof (pathname), 171*0Sstevel@tonic-gate _DFLT_LOC_PATH "C" MESSAGES "%s", 172*0Sstevel@tonic-gate msgfile) >= sizeof (pathname)) { 173*0Sstevel@tonic-gate return (handle_return(dflt_str)); 174*0Sstevel@tonic-gate } 175*0Sstevel@tonic-gate 176*0Sstevel@tonic-gate for (i = 0; i < db_count; i++) { 177*0Sstevel@tonic-gate (void) munmap((void *)db_info[i].addr, 178*0Sstevel@tonic-gate db_info[i].length); 179*0Sstevel@tonic-gate (void) strcpy(db_info[i].db_name, ""); 180*0Sstevel@tonic-gate } 181*0Sstevel@tonic-gate db_count = 0; 182*0Sstevel@tonic-gate if ((fd = open(pathname, O_RDONLY)) != -1 && 183*0Sstevel@tonic-gate fstat64(fd, &sb) != -1 && 184*0Sstevel@tonic-gate (addr = mmap(0, (size_t)sb.st_size, 185*0Sstevel@tonic-gate PROT_READ, MAP_SHARED, 186*0Sstevel@tonic-gate fd, 0)) != MAP_FAILED) { 187*0Sstevel@tonic-gate (void) strcpy(saved_locale, "C"); 188*0Sstevel@tonic-gate } else { 189*0Sstevel@tonic-gate if (fd != -1) 190*0Sstevel@tonic-gate (void) close(fd); 191*0Sstevel@tonic-gate return (handle_return(dflt_str)); 192*0Sstevel@tonic-gate } 193*0Sstevel@tonic-gate } 194*0Sstevel@tonic-gate if (fd != -1) 195*0Sstevel@tonic-gate (void) close(fd); 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate /* save file name, memory address, fd and size */ 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate (void) strcpy(db_info[db_count].db_name, msgfile); 200*0Sstevel@tonic-gate db_info[db_count].addr = (uintptr_t)addr; 201*0Sstevel@tonic-gate db_info[db_count].length = (size_t)sb.st_size; 202*0Sstevel@tonic-gate i = db_count; 203*0Sstevel@tonic-gate db_count++; 204*0Sstevel@tonic-gate } 205*0Sstevel@tonic-gate /* check if msgnum out of domain */ 206*0Sstevel@tonic-gate if (msgnum <= 0 || msgnum > *(int *)(db_info[i].addr)) 207*0Sstevel@tonic-gate return (handle_return(dflt_str)); 208*0Sstevel@tonic-gate /* return pointer to message */ 209*0Sstevel@tonic-gate return ((char *)(db_info[i].addr + *(int *)(db_info[i].addr 210*0Sstevel@tonic-gate + msgnum * sizeof (int)))); 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate char * 214*0Sstevel@tonic-gate handle_return(const char *dflt_str) 215*0Sstevel@tonic-gate { 216*0Sstevel@tonic-gate return ((char *)(dflt_str && *dflt_str ? dflt_str : not_found)); 217*0Sstevel@tonic-gate } 218