1 /* loadmsgcat.c -- load needed message catalogs
2 Copyright (C) 1995 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
16 Foundation, 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 _LIBC
35 # include <sys/mman.h>
41 /* @@ end of prolog @@ */
44 /* Rename the non ANSI C functions. This is required by the standard
45 because some ANSI C functions will require linking with this object
46 file and the name space must not be polluted. */
47 # define fstat __fstat
49 # define close __close
52 # define munmap __munmap
55 /* We need a sign, whether a new catalog was loaded, which can be associated
56 with all translations. This is important if the translations are
57 cached by one of GCC's features. */
61 /* Load the message catalogs specified by FILENAME. If it is no valid
62 message catalog do nothing. */
64 _nl_load_domain (domain
)
65 struct loaded_domain
*domain
;
69 struct mo_file_header
*data
= (struct mo_file_header
*) -1;
70 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
78 /* If the record does not represent a valid locale the FILENAME
79 might be NULL. This can happen when according to the given
80 specification the locale file name is different for XPG and CEN
82 if (domain
->filename
== NULL
)
85 /* Try to open the addressed file. */
86 fd
= open (domain
->filename
, O_RDONLY
);
90 /* We must know about the size of the file. */
91 if (fstat (fd
, &st
) != 0
92 && st
.st_size
< (off_t
) sizeof (struct mo_file_header
))
94 /* Something went wrong. */
99 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
101 /* Now we are ready to load the file. If mmap() is available we try
102 this first. If not available or it failed we try to load it. */
103 data
= (struct mo_file_header
*) mmap (NULL
, st
.st_size
, PROT_READ
,
106 if (data
!= (struct mo_file_header
*) -1)
108 /* mmap() call was successful. */
114 /* If the data is not yet available (i.e. mmap'ed) we try to load
116 if (data
== (struct mo_file_header
*) -1)
121 data
= (struct mo_file_header
*) malloc (st
.st_size
);
125 to_read
= st
.st_size
;
126 read_ptr
= (char *) data
;
129 long int nb
= (long int) read (fd
, read_ptr
, to_read
);
144 /* Using the magic number we can test whether it really is a message
146 if (data
->magic
!= _MAGIC
&& data
->magic
!= _MAGIC_SWAPPED
)
148 /* The magic number is wrong: not a message catalog file. */
149 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
152 munmap ((caddr_t
) data
, st
.st_size
);
159 domain
->data
= (char *) data
;
160 domain
->must_swap
= data
->magic
!= _MAGIC
;
162 /* Fill in the information about the available tables. */
163 switch (W (domain
->must_swap
, data
->revision
))
166 domain
->nstrings
= W (domain
->must_swap
, data
->nstrings
);
167 domain
->orig_tab
= (struct string_desc
*)
168 ((char *) data
+ W (domain
->must_swap
, data
->orig_tab_offset
));
169 domain
->trans_tab
= (struct string_desc
*)
170 ((char *) data
+ W (domain
->must_swap
, data
->trans_tab_offset
));
171 domain
->hash_size
= W (domain
->must_swap
, data
->hash_tab_size
);
172 domain
->hash_tab
= (nls_uint32
*)
173 ((char *) data
+ W (domain
->must_swap
, data
->hash_tab_offset
));
176 /* This is an illegal revision. */
177 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
180 munmap ((caddr_t
) data
, st
.st_size
);
188 /* Show that one domain is changed. This might make some cached
189 translation invalid. */