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]
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 weak _gettxt = gettxt
39 #include <sys/types.h>
48 #include "../i18n/_locale.h"
49 #include "../i18n/_loc_path.h"
51 #define MESSAGES "/LC_MESSAGES/"
52 #define DB_NAME_LEN 15
54 #define handle_return(s) \
55 ((char *)((s) != NULL && *(s) != '\0' ? (s) : not_found))
57 extern char cur_cat
[];
58 extern rwlock_t _rw_cur_cat
;
60 static mutex_t gettxt_lock
= DEFAULTMUTEX
;
61 static const char *not_found
= "Message not found!!\n";
62 static const char *loc_C
= "C";
65 char db_name
[DB_NAME_LEN
]; /* name of the message file */
66 uintptr_t addr
; /* virtual memory address */
73 struct db_cache
*next
;
76 static struct db_cache
*db_cache
;
79 gettxt(const char *msg_id
, const char *dflt_str
)
83 char msgfile
[DB_NAME_LEN
]; /* name of static shared library */
84 int msgnum
; /* message number */
85 char pathname
[PATH_MAX
]; /* full pathname to message file */
94 if ((msg_id
== NULL
) || (*msg_id
== '\0')) {
95 return (handle_return(dflt_str
));
99 if (((tokp
= strchr(msg_id
, ':')) == NULL
) || *(tokp
+1) == '\0')
100 return (handle_return(dflt_str
));
101 if ((name_len
= (tokp
- msg_id
)) >= DB_NAME_LEN
)
102 return (handle_return(dflt_str
));
104 (void) strncpy(msgfile
, msg_id
, name_len
);
105 msgfile
[name_len
] = '\0';
107 lrw_rdlock(&_rw_cur_cat
);
108 if (cur_cat
== NULL
|| *cur_cat
== '\0') {
109 lrw_unlock(&_rw_cur_cat
);
110 return (handle_return(dflt_str
));
113 * We know the following strcpy is safe.
115 (void) strcpy(msgfile
, cur_cat
);
116 lrw_unlock(&_rw_cur_cat
);
119 if (!isdigit((unsigned char)*tokp
))
120 return (handle_return(dflt_str
));
122 msgnum
= atoi(msg_id
+ name_len
+ 1);
123 loc
= uselocale(NULL
);
124 curloc
= current_locale(loc
, LC_MESSAGES
);
126 lmutex_lock(&gettxt_lock
);
131 if (strcmp(curloc
, dbc
->loc
) == 0) {
134 if (strcmp(msgfile
, dbl
->db_name
) == 0) {
136 lmutex_unlock(&gettxt_lock
);
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
));
158 (void) strcpy(dbc
->loc
, curloc
);
159 /* connect dbc to the dbc list */
160 dbc
->next
= db_cache
;
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
) >=
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
) {
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
;
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
;
201 lmutex_unlock(&gettxt_lock
);
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))));