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