1 /* Load needed message catalogs.
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
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)
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include <sys/types.h>
26 #if defined STDC_HEADERS || defined _LIBC
30 #if defined HAVE_UNISTD_H || defined _LIBC
34 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
35 || (defined _LIBC && defined _POSIX_MAPPED_FILES)
36 # include <sys/mman.h>
46 /* @@ end of prolog @@ */
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. */
53 # define close __close
56 # define munmap __munmap
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;
65 /* Load the message catalogs specified by FILENAME. If it is no valid
66 message catalog do nothing. */
69 _nl_load_domain (domain_file)
70 struct loaded_l10nfile *domain_file;
75 struct mo_file_header *data = (struct mo_file_header *) -1;
77 struct loaded_domain *domain;
79 domain_file->decided = 1;
80 domain_file->data = NULL;
82 /* If the record does not represent a valid locale the FILENAME
83 might be NULL. This can happen when according to the given
84 specification the locale file name is different for XPG and CEN
86 if (domain_file->filename == NULL)
89 /* Try to open the addressed file. */
90 fd = open (domain_file->filename, O_RDONLY);
94 /* We must know about the size of the file. */
95 if (fstat (fd, &st) != 0
96 || (size = (size_t) st.st_size) != st.st_size
97 || size < sizeof (struct mo_file_header))
99 /* Something went wrong. */
105 /* Now we are ready to load the file. If mmap() is available we try
106 this first. If not available or it failed we try to load it. */
107 data = (struct mo_file_header *) mmap (NULL, size, PROT_READ,
110 if (data != (struct mo_file_header *) -1)
112 /* mmap() call was successful. */
118 /* If the data is not yet available (i.e. mmap'ed) we try to load
120 if (data == (struct mo_file_header *) -1)
125 data = (struct mo_file_header *) malloc (size);
130 read_ptr = (char *) data;
133 long int nb = (long int) read (fd, read_ptr, to_read);
148 /* Using the magic number we can test whether it really is a message
150 if (data->magic != _MAGIC && data->magic != _MAGIC_SWAPPED)
152 /* The magic number is wrong: not a message catalog file. */
155 munmap ((caddr_t) data, size);
163 = (struct loaded_domain *) malloc (sizeof (struct loaded_domain));
164 if (domain_file->data == NULL)
167 domain = (struct loaded_domain *) domain_file->data;
168 domain->data = (char *) data;
169 domain->use_mmap = use_mmap;
170 domain->mmap_size = size;
171 domain->must_swap = data->magic != _MAGIC;
173 /* Fill in the information about the available tables. */
174 switch (W (domain->must_swap, data->revision))
177 domain->nstrings = W (domain->must_swap, data->nstrings);
178 domain->orig_tab = (struct string_desc *)
179 ((char *) data + W (domain->must_swap, data->orig_tab_offset));
180 domain->trans_tab = (struct string_desc *)
181 ((char *) data + W (domain->must_swap, data->trans_tab_offset));
182 domain->hash_size = W (domain->must_swap, data->hash_tab_size);
183 domain->hash_tab = (nls_uint32 *)
184 ((char *) data + W (domain->must_swap, data->hash_tab_offset));
187 /* This is an invalid revision. */
190 munmap ((caddr_t) data, size);
195 domain_file->data = NULL;
199 /* Show that one domain is changed. This might make some cached
200 translations invalid. */
208 _nl_unload_domain (domain)
209 struct loaded_domain *domain;
211 #ifdef _POSIX_MAPPED_FILES
212 if (domain->use_mmap)
213 munmap ((caddr_t) domain->data, domain->mmap_size);
215 #endif /* _POSIX_MAPPED_FILES */
216 free ((void *) domain->data);