1 /* Load needed message catalogs.
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
17
18 #include "intlconfig.h"
19
20 #include <fcntl.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23
24 #if defined STDC_HEADERS || defined _LIBC
25 # include <stdlib.h>
26 #endif
27
28 #if defined HAVE_UNISTD_H || defined _LIBC
29 # include <unistd.h>
30 #endif
31
32 #if (defined HAVE_MMAP && defined HAVE_MUNMAP) || defined _LIBC
33 # include <sys/mman.h>
34 #endif
35
36 #ifdef _WIN32
37 #include <io.h>
38 #ifdef _MSC_VER
39 #pragma warning (disable:4018)
40 #endif
41 #endif
42
43 #include "gettext.h"
44 #include "gettextP.h"
45
46 /* @@ end of prolog @@ */
47
48 #ifdef _LIBC
49 /* Rename the non ISO C functions. This is required by the standard
50 because some ISO C functions will require linking with this object
51 file and the name space must not be polluted. */
52 # define open __open
53 # define close __close
54 # define read __read
55 # define mmap __mmap
56 # define munmap __munmap
57 #endif
58
59 /* We need a sign, whether a new catalog was loaded, which can be associated
60 with all translations. This is important if the translations are
61 cached by one of GCC's features. */
62 int _nl_msg_cat_cntr = 0;
63
64
65 /* Load the message catalogs specified by FILENAME. If it is no valid
66 message catalog do nothing. */
67 void
68 internal_function
_nl_load_domain(domain_file)69 _nl_load_domain (domain_file)
70 struct loaded_l10nfile *domain_file;
71 {
72 int fd;
73 size_t size;
74 struct stat st;
75 struct mo_file_header *data = (struct mo_file_header *) -1;
76 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
77 || defined _LIBC
78 int use_mmap = 0;
79 #endif
80 struct loaded_domain *domain;
81
82 domain_file->decided = 1;
83 domain_file->data = NULL;
84
85 /* If the record does not represent a valid locale the FILENAME
86 might be NULL. This can happen when according to the given
87 specification the locale file name is different for XPG and CEN
88 syntax. */
89 if (domain_file->filename == NULL)
90 return;
91
92 /* Try to open the addressed file. */
93 fd = open (domain_file->filename, O_RDONLY | O_BINARY); /*FRANCO - binary*/
94 if (fd == -1)
95 return;
96
97 /* We must know about the size of the file. */
98 if (fstat (fd, &st) != 0
99 || (size = (size_t) st.st_size) != st.st_size
100 || size < sizeof (struct mo_file_header))
101 {
102 /* Something went wrong. */
103 close (fd);
104 return;
105 }
106
107 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
108 || defined _LIBC
109 /* Now we are ready to load the file. If mmap() is available we try
110 this first. If not available or it failed we try to load it. */
111 data = (struct mo_file_header *) mmap (NULL, size, PROT_READ,
112 MAP_PRIVATE, fd, 0);
113
114 if (data != (struct mo_file_header *) -1)
115 {
116 /* mmap() call was successful. */
117 close (fd);
118 use_mmap = 1;
119 }
120 #endif
121
122 /* If the data is not yet available (i.e. mmap'ed) we try to load
123 it manually. */
124 if (data == (struct mo_file_header *) -1)
125 {
126 size_t to_read;
127 char *read_ptr;
128
129 data = (struct mo_file_header *) malloc (size);
130 if (data == NULL)
131 return;
132
133 to_read = size;
134 read_ptr = (char *) data;
135 do
136 {
137 long int nb = (long int) read (fd, read_ptr, to_read);
138 if (nb == -1)
139 {
140 close (fd);
141 return;
142 }
143
144 read_ptr += nb;
145 to_read -= nb;
146 }
147 while (to_read > 0);
148
149 close (fd);
150 }
151
152 /* Using the magic number we can test whether it really is a message
153 catalog file. */
154 if (data->magic != _MAGIC && data->magic != _MAGIC_SWAPPED)
155 {
156 /* The magic number is wrong: not a message catalog file. */
157 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
158 || defined _LIBC
159 if (use_mmap)
160 munmap ((caddr_t) data, size);
161 else
162 #endif
163 free (data);
164 return;
165 }
166
167 domain_file->data
168 = (struct loaded_domain *) malloc (sizeof (struct loaded_domain));
169 if (domain_file->data == NULL)
170 return;
171
172 domain = (struct loaded_domain *) domain_file->data;
173 domain->data = (char *) data;
174 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
175 || defined _LIBC
176 domain->use_mmap = use_mmap;
177 #endif
178 domain->mmap_size = size;
179 domain->must_swap = data->magic != _MAGIC;
180
181 /* Fill in the information about the available tables. */
182 switch (W (domain->must_swap, data->revision))
183 {
184 case 0:
185 domain->nstrings = W (domain->must_swap, data->nstrings);
186 domain->orig_tab = (struct string_desc *)
187 ((char *) data + W (domain->must_swap, data->orig_tab_offset));
188 domain->trans_tab = (struct string_desc *)
189 ((char *) data + W (domain->must_swap, data->trans_tab_offset));
190 domain->hash_size = W (domain->must_swap, data->hash_tab_size);
191 domain->hash_tab = (nls_uint32 *)
192 ((char *) data + W (domain->must_swap, data->hash_tab_offset));
193 break;
194 default:
195 /* This is an illegal revision. */
196 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
197 || defined _LIBC
198 if (use_mmap)
199 munmap ((caddr_t) data, size);
200 else
201 #endif
202 free (data);
203 free (domain);
204 domain_file->data = NULL;
205 return;
206 }
207
208 /* Show that one domain is changed. This might make some cached
209 translations invalid. */
210 ++_nl_msg_cat_cntr;
211 }
212
213
214 #ifdef _LIBC
215 void
216 internal_function
_nl_unload_domain(domain)217 _nl_unload_domain (domain)
218 struct loaded_domain *domain;
219 {
220 if (domain->use_mmap)
221 munmap ((caddr_t) domain->data, domain->mmap_size);
222 else
223 free ((void *) domain->data);
224
225 free (domain);
226 }
227 #endif
228