1 /* Load needed message catalogs.
\r
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
\r
4 This program is free software; you can redistribute it and/or modify
\r
5 it under the terms of the GNU General Public License as published by
\r
6 the Free Software Foundation; either version 2, or (at your option)
\r
9 This program is distributed in the hope that it will be useful,
\r
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 GNU General Public License for more details.
\r
14 You should have received a copy of the GNU General Public License
\r
15 along with this program; if not, write to the Free Software Foundation,
\r
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA. */
\r
18 #include "intlconfig.h"
\r
21 #include <sys/types.h>
\r
22 #include <sys/stat.h>
\r
24 #if defined STDC_HEADERS || defined _LIBC
\r
25 # include <stdlib.h>
\r
28 #if defined HAVE_UNISTD_H || defined _LIBC
\r
29 # include <unistd.h>
\r
32 #if (defined HAVE_MMAP && defined HAVE_MUNMAP) || defined _LIBC
\r
33 # include <sys/mman.h>
\r
39 #pragma warning (disable:4018)
\r
43 #include "gettext.h"
\r
44 #include "gettextP.h"
\r
46 /* @@ end of prolog @@ */
\r
49 /* Rename the non ISO C functions. This is required by the standard
\r
50 because some ISO C functions will require linking with this object
\r
51 file and the name space must not be polluted. */
\r
52 # define open __open
\r
53 # define close __close
\r
54 # define read __read
\r
55 # define mmap __mmap
\r
56 # define munmap __munmap
\r
59 /* We need a sign, whether a new catalog was loaded, which can be associated
\r
60 with all translations. This is important if the translations are
\r
61 cached by one of GCC's features. */
\r
62 int _nl_msg_cat_cntr = 0;
\r
65 /* Load the message catalogs specified by FILENAME. If it is no valid
\r
66 message catalog do nothing. */
\r
69 _nl_load_domain (domain_file)
\r
70 struct loaded_l10nfile *domain_file;
\r
75 struct mo_file_header *data = (struct mo_file_header *) -1;
\r
76 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
\r
80 struct loaded_domain *domain;
\r
82 domain_file->decided = 1;
\r
83 domain_file->data = NULL;
\r
85 /* If the record does not represent a valid locale the FILENAME
\r
86 might be NULL. This can happen when according to the given
\r
87 specification the locale file name is different for XPG and CEN
\r
89 if (domain_file->filename == NULL)
\r
92 /* Try to open the addressed file. */
\r
93 fd = open (domain_file->filename, O_RDONLY | O_BINARY); /*FRANCO - binary*/
\r
97 /* We must know about the size of the file. */
\r
98 if (fstat (fd, &st) != 0
\r
99 || (size = (size_t) st.st_size) != st.st_size
\r
100 || size < sizeof (struct mo_file_header))
\r
102 /* Something went wrong. */
\r
107 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
\r
109 /* Now we are ready to load the file. If mmap() is available we try
\r
110 this first. If not available or it failed we try to load it. */
\r
111 data = (struct mo_file_header *) mmap (NULL, size, PROT_READ,
\r
112 MAP_PRIVATE, fd, 0);
\r
114 if (data != (struct mo_file_header *) -1)
\r
116 /* mmap() call was successful. */
\r
122 /* If the data is not yet available (i.e. mmap'ed) we try to load
\r
124 if (data == (struct mo_file_header *) -1)
\r
129 data = (struct mo_file_header *) malloc (size);
\r
134 read_ptr = (char *) data;
\r
137 long int nb = (long int) read (fd, read_ptr, to_read);
\r
147 while (to_read > 0);
\r
152 /* Using the magic number we can test whether it really is a message
\r
154 if (data->magic != _MAGIC && data->magic != _MAGIC_SWAPPED)
\r
156 /* The magic number is wrong: not a message catalog file. */
\r
157 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
\r
160 munmap ((caddr_t) data, size);
\r
168 = (struct loaded_domain *) malloc (sizeof (struct loaded_domain));
\r
169 if (domain_file->data == NULL)
\r
172 domain = (struct loaded_domain *) domain_file->data;
\r
173 domain->data = (char *) data;
\r
174 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
\r
176 domain->use_mmap = use_mmap;
\r
178 domain->mmap_size = size;
\r
179 domain->must_swap = data->magic != _MAGIC;
\r
181 /* Fill in the information about the available tables. */
\r
182 switch (W (domain->must_swap, data->revision))
\r
185 domain->nstrings = W (domain->must_swap, data->nstrings);
\r
186 domain->orig_tab = (struct string_desc *)
\r
187 ((char *) data + W (domain->must_swap, data->orig_tab_offset));
\r
188 domain->trans_tab = (struct string_desc *)
\r
189 ((char *) data + W (domain->must_swap, data->trans_tab_offset));
\r
190 domain->hash_size = W (domain->must_swap, data->hash_tab_size);
\r
191 domain->hash_tab = (nls_uint32 *)
\r
192 ((char *) data + W (domain->must_swap, data->hash_tab_offset));
\r
195 /* This is an illegal revision. */
\r
196 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
\r
199 munmap ((caddr_t) data, size);
\r
204 domain_file->data = NULL;
\r
208 /* Show that one domain is changed. This might make some cached
\r
209 translations invalid. */
\r
210 ++_nl_msg_cat_cntr;
\r
217 _nl_unload_domain (domain)
\r
218 struct loaded_domain *domain;
\r
220 if (domain->use_mmap)
\r
221 munmap ((caddr_t) domain->data, domain->mmap_size);
\r
223 free ((void *) domain->data);
\r