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 ident "%Z%%M% %I% %E% SMI"
32 #pragma weak _gettxt = gettxt
41 #include <sys/types.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";
67 char db_name
[DB_NAME_LEN
]; /* name of the message file */
68 uintptr_t addr
; /* virtual memory address */
75 struct db_cache
*next
;
78 static struct db_cache
*db_cache
;
81 gettxt(const char *msg_id
, const char *dflt_str
)
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 */
95 if ((msg_id
== NULL
) || (*msg_id
== '\0')) {
96 return (handle_return(dflt_str
));
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
));
105 (void) strncpy(msgfile
, msg_id
, name_len
);
106 msgfile
[name_len
] = '\0';
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
);
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
);
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))));