1 /* Load needed message catalogs.
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of the GNU C Library. Its master source is NOT part of
5 the C library, however.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
27 #include <sys/types.h>
30 #if defined STDC_HEADERS || defined _LIBC
34 #if defined HAVE_UNISTD_H || defined _LIBC
38 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
39 || (defined _LIBC && defined _POSIX_MAPPED_FILES)
40 # include <sys/mman.h>
50 /* @@ end of prolog @@ */
53 /* Rename the non ISO C functions. This is required by the standard
54 because some ISO C functions will require linking with this object
55 file and the name space must not be polluted. */
57 # define close __close
60 # define munmap __munmap
63 /* We need a sign, whether a new catalog was loaded, which can be associated
64 with all translations. This is important if the translations are
65 cached by one of GCC's features. */
66 int _nl_msg_cat_cntr
= 0;
69 /* Load the message catalogs specified by FILENAME. If it is no valid
70 message catalog do nothing. */
73 _nl_load_domain (domain_file
)
74 struct loaded_l10nfile
*domain_file
;
79 struct mo_file_header
*data
= (struct mo_file_header
*) -1;
81 struct loaded_domain
*domain
;
83 domain_file
->decided
= 1;
84 domain_file
->data
= NULL
;
86 /* If the record does not represent a valid locale the FILENAME
87 might be NULL. This can happen when according to the given
88 specification the locale file name is different for XPG and CEN
90 if (domain_file
->filename
== NULL
)
93 /* Try to open the addressed file. */
94 fd
= open (domain_file
->filename
, O_RDONLY
);
98 /* We must know about the size of the file. */
99 if (fstat (fd
, &st
) != 0
100 || (size
= (size_t) st
.st_size
) != st
.st_size
101 || size
< sizeof (struct mo_file_header
))
103 /* Something went wrong. */
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
,
114 if (data
!= (struct mo_file_header
*) -1)
116 /* mmap() call was successful. */
122 /* If the data is not yet available (i.e. mmap'ed) we try to load
124 if (data
== (struct mo_file_header
*) -1)
129 data
= (struct mo_file_header
*) malloc (size
);
134 read_ptr
= (char *) data
;
137 long int nb
= (long int) read (fd
, read_ptr
, to_read
);
152 /* Using the magic number we can test whether it really is a message
154 if (data
->magic
!= _MAGIC
&& data
->magic
!= _MAGIC_SWAPPED
)
156 /* The magic number is wrong: not a message catalog file. */
159 munmap ((caddr_t
) data
, size
);
167 = (struct loaded_domain
*) malloc (sizeof (struct loaded_domain
));
168 if (domain_file
->data
== NULL
)
171 domain
= (struct loaded_domain
*) domain_file
->data
;
172 domain
->data
= (char *) data
;
173 domain
->use_mmap
= use_mmap
;
174 domain
->mmap_size
= size
;
175 domain
->must_swap
= data
->magic
!= _MAGIC
;
177 /* Fill in the information about the available tables. */
178 switch (W (domain
->must_swap
, data
->revision
))
181 domain
->nstrings
= W (domain
->must_swap
, data
->nstrings
);
182 domain
->orig_tab
= (struct string_desc
*)
183 ((char *) data
+ W (domain
->must_swap
, data
->orig_tab_offset
));
184 domain
->trans_tab
= (struct string_desc
*)
185 ((char *) data
+ W (domain
->must_swap
, data
->trans_tab_offset
));
186 domain
->hash_size
= W (domain
->must_swap
, data
->hash_tab_size
);
187 domain
->hash_tab
= (nls_uint32
*)
188 ((char *) data
+ W (domain
->must_swap
, data
->hash_tab_offset
));
191 /* This is an invalid revision. */
194 munmap ((caddr_t
) data
, size
);
199 domain_file
->data
= NULL
;
203 /* Show that one domain is changed. This might make some cached
204 translations invalid. */
212 _nl_unload_domain (domain
)
213 struct loaded_domain
*domain
;
215 #ifdef _POSIX_MAPPED_FILES
216 if (domain
->use_mmap
)
217 munmap ((caddr_t
) domain
->data
, domain
->mmap_size
);
219 #endif /* _POSIX_MAPPED_FILES */
220 free ((void *) domain
->data
);