6811333 Remove prom_printf() message in emlxs driver
[opensolaris.git] / usr / src / lib / libc / port / gen / gettxt.c
blob88e0b5b72cf3229cd77d0003e37a7a090ccbc597
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
32 #pragma weak _gettxt = gettxt
34 #include "lint.h"
35 #include "libc.h"
36 #include <mtlib.h>
37 #include <ctype.h>
38 #include <string.h>
39 #include <locale.h>
40 #include <fcntl.h>
41 #include <sys/types.h>
42 #include <sys/file.h>
43 #include <sys/mman.h>
44 #include <sys/stat.h>
45 #include <pfmt.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <limits.h>
49 #include <thread.h>
50 #include "../i18n/_locale.h"
51 #include "../i18n/_loc_path.h"
53 #define MESSAGES "/LC_MESSAGES/"
54 #define DB_NAME_LEN 15
56 #define handle_return(s) \
57 ((char *)((s) != NULL && *(s) != '\0' ? (s) : not_found))
59 extern char cur_cat[];
60 extern rwlock_t _rw_cur_cat;
62 static mutex_t gettxt_lock = DEFAULTMUTEX;
63 static const char *not_found = "Message not found!!\n";
64 static const char *loc_C = "C";
66 struct db_list {
67 char db_name[DB_NAME_LEN]; /* name of the message file */
68 uintptr_t addr; /* virtual memory address */
69 struct db_list *next;
72 struct db_cache {
73 char *loc;
74 struct db_list *info;
75 struct db_cache *next;
78 static struct db_cache *db_cache;
80 char *
81 gettxt(const char *msg_id, const char *dflt_str)
83 struct db_cache *dbc;
84 struct db_list *dbl;
85 char msgfile[DB_NAME_LEN]; /* name of static shared library */
86 int msgnum; /* message number */
87 char pathname[PATH_MAX]; /* full pathname to message file */
88 int fd;
89 struct stat64 sb;
90 void *addr;
91 char *tokp;
92 size_t name_len;
93 char *curloc;
95 if ((msg_id == NULL) || (*msg_id == '\0')) {
96 return (handle_return(dflt_str));
99 /* parse msg_id */
100 if (((tokp = strchr(msg_id, ':')) == NULL) || *(tokp+1) == '\0')
101 return (handle_return(dflt_str));
102 if ((name_len = (tokp - msg_id)) >= DB_NAME_LEN)
103 return (handle_return(dflt_str));
104 if (name_len > 0) {
105 (void) strncpy(msgfile, msg_id, name_len);
106 msgfile[name_len] = '\0';
107 } else {
108 lrw_rdlock(&_rw_cur_cat);
109 if (cur_cat == NULL || *cur_cat == '\0') {
110 lrw_unlock(&_rw_cur_cat);
111 return (handle_return(dflt_str));
114 * We know the following strcpy is safe.
116 (void) strcpy(msgfile, cur_cat);
117 lrw_unlock(&_rw_cur_cat);
119 while (*++tokp) {
120 if (!isdigit((unsigned char)*tokp))
121 return (handle_return(dflt_str));
123 msgnum = atoi(msg_id + name_len + 1);
124 curloc = setlocale(LC_MESSAGES, NULL);
126 lmutex_lock(&gettxt_lock);
128 try_C:
129 dbc = db_cache;
130 while (dbc) {
131 if (strcmp(curloc, dbc->loc) == 0) {
132 dbl = dbc->info;
133 while (dbl) {
134 if (strcmp(msgfile, dbl->db_name) == 0) {
135 /* msgfile found */
136 lmutex_unlock(&gettxt_lock);
137 goto msgfile_found;
139 dbl = dbl->next;
141 /* not found */
142 break;
144 dbc = dbc->next;
146 if (dbc == NULL) {
147 /* new locale */
148 if ((dbc = lmalloc(sizeof (struct db_cache))) == NULL) {
149 lmutex_unlock(&gettxt_lock);
150 return (handle_return(dflt_str));
152 if ((dbc->loc = lmalloc(strlen(curloc) + 1)) == NULL) {
153 lfree(dbc, sizeof (struct db_cache));
154 lmutex_unlock(&gettxt_lock);
155 return (handle_return(dflt_str));
157 dbc->info = NULL;
158 (void) strcpy(dbc->loc, curloc);
159 /* connect dbc to the dbc list */
160 dbc->next = db_cache;
161 db_cache = dbc;
163 if ((dbl = lmalloc(sizeof (struct db_list))) == NULL) {
164 lmutex_unlock(&gettxt_lock);
165 return (handle_return(dflt_str));
168 if (snprintf(pathname, sizeof (pathname),
169 _DFLT_LOC_PATH "%s" MESSAGES "%s", dbc->loc, msgfile) >=
170 sizeof (pathname)) {
171 lfree(dbl, sizeof (struct db_list));
172 lmutex_unlock(&gettxt_lock);
173 return (handle_return(dflt_str));
175 if ((fd = open(pathname, O_RDONLY)) == -1 ||
176 fstat64(fd, &sb) == -1 ||
177 (addr = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_SHARED,
178 fd, 0L)) == MAP_FAILED) {
179 if (fd != -1)
180 (void) close(fd);
181 lfree(dbl, sizeof (struct db_list));
183 if (strcmp(dbc->loc, "C") == 0) {
184 lmutex_unlock(&gettxt_lock);
185 return (handle_return(dflt_str));
187 /* Change locale to C */
188 curloc = (char *)loc_C;
189 goto try_C;
191 (void) close(fd);
193 /* save file name, memory address, fd and size */
194 (void) strcpy(dbl->db_name, msgfile);
195 dbl->addr = (uintptr_t)addr;
197 /* connect dbl to the dbc->info list */
198 dbl->next = dbc->info;
199 dbc->info = dbl;
201 lmutex_unlock(&gettxt_lock);
203 msgfile_found:
204 /* check if msgnum out of domain */
205 if (msgnum <= 0 || msgnum > *(int *)dbl->addr)
206 return (handle_return(dflt_str));
207 /* return pointer to message */
208 return ((char *)(dbl->addr +
209 *(int *)(dbl->addr + msgnum * sizeof (int))));