Update.
[glibc.git] / intl / loadmsgcat.c
bloba67223ff7e0df27ef2d379431eabf89f40415a9c
1 /* Load needed message catalogs.
2 Copyright (C) 1995, 1996, 1997 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. */
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
30 #if defined STDC_HEADERS || defined _LIBC
31 # include <stdlib.h>
32 #endif
34 #if defined HAVE_UNISTD_H || defined _LIBC
35 # include <unistd.h>
36 #endif
38 #if (defined HAVE_MMAP && defined HAVE_MUNMAP) || defined _LIBC
39 # include <sys/mman.h>
40 #endif
42 #include "gettext.h"
43 #include "gettextP.h"
45 /* @@ end of prolog @@ */
47 #ifdef _LIBC
48 /* Rename the non ISO C functions. This is required by the standard
49 because some ISO C functions will require linking with this object
50 file and the name space must not be polluted. */
51 # define fstat __fstat
52 # define open __open
53 # define close __close
54 # define read __read
55 # define mmap __mmap
56 # define munmap __munmap
57 #endif
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. */
67 void
68 _nl_load_domain (domain_file)
69 struct loaded_l10nfile *domain_file;
71 int fd;
72 size_t size;
73 struct stat st;
74 struct mo_file_header *data = (struct mo_file_header *) -1;
75 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
76 || defined _LIBC
77 int use_mmap = 0;
78 #endif
79 struct loaded_domain *domain;
81 domain_file->decided = 1;
82 domain_file->data = NULL;
84 /* If the record does not represent a valid locale the FILENAME
85 might be NULL. This can happen when according to the given
86 specification the locale file name is different for XPG and CEN
87 syntax. */
88 if (domain_file->filename == NULL)
89 return;
91 /* Try to open the addressed file. */
92 fd = open (domain_file->filename, O_RDONLY);
93 if (fd == -1)
94 return;
96 /* We must know about the size of the file. */
97 if (fstat (fd, &st) != 0
98 || (size = (size_t) st.st_size) != st.st_size
99 || size < sizeof (struct mo_file_header))
101 /* Something went wrong. */
102 close (fd);
103 return;
106 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
107 || defined _LIBC
108 /* Now we are ready to load the file. If mmap() is available we try
109 this first. If not available or it failed we try to load it. */
110 data = (struct mo_file_header *) mmap (NULL, size, PROT_READ,
111 MAP_PRIVATE, fd, 0);
113 if (data != (struct mo_file_header *) -1)
115 /* mmap() call was successful. */
116 close (fd);
117 use_mmap = 1;
119 #endif
121 /* If the data is not yet available (i.e. mmap'ed) we try to load
122 it manually. */
123 if (data == (struct mo_file_header *) -1)
125 size_t to_read;
126 char *read_ptr;
128 data = (struct mo_file_header *) malloc (size);
129 if (data == NULL)
130 return;
132 to_read = size;
133 read_ptr = (char *) data;
136 long int nb = (long int) read (fd, read_ptr, to_read);
137 if (nb == -1)
139 close (fd);
140 return;
143 read_ptr += nb;
144 to_read -= nb;
146 while (to_read > 0);
148 close (fd);
151 /* Using the magic number we can test whether it really is a message
152 catalog file. */
153 if (data->magic != _MAGIC && data->magic != _MAGIC_SWAPPED)
155 /* The magic number is wrong: not a message catalog file. */
156 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
157 || defined _LIBC
158 if (use_mmap)
159 munmap ((caddr_t) data, size);
160 else
161 #endif
162 free (data);
163 return;
166 domain_file->data
167 = (struct loaded_domain *) malloc (sizeof (struct loaded_domain));
168 if (domain_file->data == NULL)
169 return;
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))
180 case 0:
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));
189 break;
190 default:
191 /* This is an illegal revision. */
192 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
193 || defined _LIBC
194 if (use_mmap)
195 munmap ((caddr_t) data, size);
196 else
197 #endif
198 free (data);
199 free (domain);
200 domain_file->data = NULL;
201 return;
204 /* Show that one domain is changed. This might make some cached
205 translations invalid. */
206 ++_nl_msg_cat_cntr;
210 #ifdef _LIBC
211 void
212 _nl_unload_domain (domain)
213 struct loaded_domain *domain;
215 if (domain->use_mmap)
216 munmap ((caddr_t) domain->data, domain->mmap_size);
217 else
218 free ((void *) domain->data);
220 free (domain);
222 #endif