1 /* loadmsgcat.c -- load needed message catalogs
2 Copyright (C) 1995, 1996 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. The master source lives in /gd/gnu/lib.
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
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, 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 _LIBC
39 # include <sys/mman.h>
45 /* @@ end of prolog @@ */
48 /* Rename the non ANSI C functions. This is required by the standard
49 because some ANSI C functions will require linking with this object
50 file and the name space must not be polluted. */
51 # define fstat __fstat
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. */
65 /* Load the message catalogs specified by FILENAME. If it is no valid
66 message catalog do nothing. */
68 _nl_load_domain (domain_file
)
69 struct loaded_l10nfile
*domain_file
;
73 struct mo_file_header
*data
= (struct mo_file_header
*) -1;
74 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
78 struct loaded_domain
*domain
;
80 domain_file
->decided
= 1;
81 domain_file
->data
= NULL
;
83 /* If the record does not represent a valid locale the FILENAME
84 might be NULL. This can happen when according to the given
85 specification the locale file name is different for XPG and CEN
87 if (domain_file
->filename
== NULL
)
90 /* Try to open the addressed file. */
91 fd
= open (domain_file
->filename
, O_RDONLY
);
95 /* We must know about the size of the file. */
96 if (fstat (fd
, &st
) != 0
97 && st
.st_size
< (off_t
) sizeof (struct mo_file_header
))
99 /* Something went wrong. */
104 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
106 /* Now we are ready to load the file. If mmap() is available we try
107 this first. If not available or it failed we try to load it. */
108 data
= (struct mo_file_header
*) mmap (NULL
, st
.st_size
, PROT_READ
,
111 if (data
!= (struct mo_file_header
*) -1)
113 /* mmap() call was successful. */
119 /* If the data is not yet available (i.e. mmap'ed) we try to load
121 if (data
== (struct mo_file_header
*) -1)
126 data
= (struct mo_file_header
*) malloc (st
.st_size
);
130 to_read
= st
.st_size
;
131 read_ptr
= (char *) data
;
134 long int nb
= (long int) read (fd
, read_ptr
, to_read
);
149 /* Using the magic number we can test whether it really is a message
151 if (data
->magic
!= _MAGIC
&& data
->magic
!= _MAGIC_SWAPPED
)
153 /* The magic number is wrong: not a message catalog file. */
154 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
157 munmap ((caddr_t
) data
, st
.st_size
);
165 = (struct loaded_domain
*) malloc (sizeof (struct loaded_domain
));
166 if (domain_file
->data
== NULL
)
169 domain
= (struct loaded_domain
*) domain_file
->data
;
170 domain
->data
= (char *) data
;
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 illegal revision. */
188 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
191 munmap ((caddr_t
) data
, st
.st_size
);
196 domain_file
->data
= NULL
;
200 /* Show that one domain is changed. This might make some cached
201 translations invalid. */