1 /* loadmsgcat.c -- load needed message catalogs
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
25 #include <sys/types.h>
28 #if defined STDC_HEADERS || defined _LIBC
32 #if defined HAVE_UNISTD_H || defined _LIBC
36 #if (defined HAVE_MMAP && defined HAVE_MUNMAP) || defined _LIBC
37 # include <sys/mman.h>
43 /* @@ end of prolog @@ */
46 /* Rename the non ANSI C functions. This is required by the standard
47 because some ANSI C functions will require linking with this object
48 file and the name space must not be polluted. */
49 # define fstat __fstat
51 # define close __close
54 # define munmap __munmap
57 /* We need a sign, whether a new catalog was loaded, which can be associated
58 with all translations. This is important if the translations are
59 cached by one of GCC's features. */
63 /* Load the message catalogs specified by FILENAME. If it is no valid
64 message catalog do nothing. */
66 _nl_load_domain (domain_file
)
67 struct loaded_l10nfile
*domain_file
;
71 struct mo_file_header
*data
= (struct mo_file_header
*) -1;
72 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
76 struct loaded_domain
*domain
;
78 domain_file
->decided
= 1;
79 domain_file
->data
= NULL
;
81 /* If the record does not represent a valid locale the FILENAME
82 might be NULL. This can happen when according to the given
83 specification the locale file name is different for XPG and CEN
85 if (domain_file
->filename
== NULL
)
88 /* Try to open the addressed file. */
89 fd
= open (domain_file
->filename
, O_RDONLY
);
93 /* We must know about the size of the file. */
94 if (fstat (fd
, &st
) != 0
95 && st
.st_size
< (off_t
) sizeof (struct mo_file_header
))
97 /* Something went wrong. */
102 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
104 /* Now we are ready to load the file. If mmap() is available we try
105 this first. If not available or it failed we try to load it. */
106 data
= (struct mo_file_header
*) mmap (NULL
, st
.st_size
, PROT_READ
,
109 if (data
!= (struct mo_file_header
*) -1)
111 /* mmap() call was successful. */
117 /* If the data is not yet available (i.e. mmap'ed) we try to load
119 if (data
== (struct mo_file_header
*) -1)
124 data
= (struct mo_file_header
*) malloc (st
.st_size
);
128 to_read
= st
.st_size
;
129 read_ptr
= (char *) data
;
132 long int nb
= (long int) read (fd
, read_ptr
, to_read
);
147 /* Using the magic number we can test whether it really is a message
149 if (data
->magic
!= _MAGIC
&& data
->magic
!= _MAGIC_SWAPPED
)
151 /* The magic number is wrong: not a message catalog file. */
152 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
155 munmap ((caddr_t
) data
, st
.st_size
);
163 = (struct loaded_domain
*) malloc (sizeof (struct loaded_domain
*));
164 if (domain
->data
== NULL
)
167 domain
= (struct loaded_domain
*) domain_file
->data
;
168 domain
->data
= (char *) data
;
169 domain
->must_swap
= data
->magic
!= _MAGIC
;
171 /* Fill in the information about the available tables. */
172 switch (W (domain
->must_swap
, data
->revision
))
175 domain
->nstrings
= W (domain
->must_swap
, data
->nstrings
);
176 domain
->orig_tab
= (struct string_desc
*)
177 ((char *) data
+ W (domain
->must_swap
, data
->orig_tab_offset
));
178 domain
->trans_tab
= (struct string_desc
*)
179 ((char *) data
+ W (domain
->must_swap
, data
->trans_tab_offset
));
180 domain
->hash_size
= W (domain
->must_swap
, data
->hash_tab_size
);
181 domain
->hash_tab
= (nls_uint32
*)
182 ((char *) data
+ W (domain
->must_swap
, data
->hash_tab_offset
));
185 /* This is an illegal revision. */
186 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
189 munmap ((caddr_t
) data
, st
.st_size
);
194 domain_file
->data
= NULL
;
198 /* Show that one domain is changed. This might make some cached
199 translations invalid. */