(glob_in_dir): Add support for cases insensitive VMS.
[glibc.git] / intl / loadmsgcat.c
blobde0534269f6ce52662cc8a51ab811f1a34a4e5c1
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 open __open
52 # define close __close
53 # define read __read
54 # define mmap __mmap
55 # define munmap __munmap
56 #endif
58 /* We need a sign, whether a new catalog was loaded, which can be associated
59 with all translations. This is important if the translations are
60 cached by one of GCC's features. */
61 int _nl_msg_cat_cntr = 0;
64 /* Load the message catalogs specified by FILENAME. If it is no valid
65 message catalog do nothing. */
66 void
67 _nl_load_domain (domain_file)
68 struct loaded_l10nfile *domain_file;
70 int fd;
71 size_t size;
72 struct stat st;
73 struct mo_file_header *data = (struct mo_file_header *) -1;
74 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
75 || defined _LIBC
76 int use_mmap = 0;
77 #endif
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
86 syntax. */
87 if (domain_file->filename == NULL)
88 return;
90 /* Try to open the addressed file. */
91 fd = open (domain_file->filename, O_RDONLY);
92 if (fd == -1)
93 return;
95 /* We must know about the size of the file. */
96 if (fstat (fd, &st) != 0
97 || (size = (size_t) st.st_size) != st.st_size
98 || size < sizeof (struct mo_file_header))
100 /* Something went wrong. */
101 close (fd);
102 return;
105 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
106 || defined _LIBC
107 /* Now we are ready to load the file. If mmap() is available we try
108 this first. If not available or it failed we try to load it. */
109 data = (struct mo_file_header *) mmap (NULL, size, PROT_READ,
110 MAP_PRIVATE, fd, 0);
112 if (data != (struct mo_file_header *) -1)
114 /* mmap() call was successful. */
115 close (fd);
116 use_mmap = 1;
118 #endif
120 /* If the data is not yet available (i.e. mmap'ed) we try to load
121 it manually. */
122 if (data == (struct mo_file_header *) -1)
124 size_t to_read;
125 char *read_ptr;
127 data = (struct mo_file_header *) malloc (size);
128 if (data == NULL)
129 return;
131 to_read = size;
132 read_ptr = (char *) data;
135 long int nb = (long int) read (fd, read_ptr, to_read);
136 if (nb == -1)
138 close (fd);
139 return;
142 read_ptr += nb;
143 to_read -= nb;
145 while (to_read > 0);
147 close (fd);
150 /* Using the magic number we can test whether it really is a message
151 catalog file. */
152 if (data->magic != _MAGIC && data->magic != _MAGIC_SWAPPED)
154 /* The magic number is wrong: not a message catalog file. */
155 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
156 || defined _LIBC
157 if (use_mmap)
158 munmap ((caddr_t) data, size);
159 else
160 #endif
161 free (data);
162 return;
165 domain_file->data
166 = (struct loaded_domain *) malloc (sizeof (struct loaded_domain));
167 if (domain_file->data == NULL)
168 return;
170 domain = (struct loaded_domain *) domain_file->data;
171 domain->data = (char *) data;
172 domain->use_mmap = use_mmap;
173 domain->mmap_size = size;
174 domain->must_swap = data->magic != _MAGIC;
176 /* Fill in the information about the available tables. */
177 switch (W (domain->must_swap, data->revision))
179 case 0:
180 domain->nstrings = W (domain->must_swap, data->nstrings);
181 domain->orig_tab = (struct string_desc *)
182 ((char *) data + W (domain->must_swap, data->orig_tab_offset));
183 domain->trans_tab = (struct string_desc *)
184 ((char *) data + W (domain->must_swap, data->trans_tab_offset));
185 domain->hash_size = W (domain->must_swap, data->hash_tab_size);
186 domain->hash_tab = (nls_uint32 *)
187 ((char *) data + W (domain->must_swap, data->hash_tab_offset));
188 break;
189 default:
190 /* This is an illegal revision. */
191 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
192 || defined _LIBC
193 if (use_mmap)
194 munmap ((caddr_t) data, size);
195 else
196 #endif
197 free (data);
198 free (domain);
199 domain_file->data = NULL;
200 return;
203 /* Show that one domain is changed. This might make some cached
204 translations invalid. */
205 ++_nl_msg_cat_cntr;
209 #ifdef _LIBC
210 void
211 _nl_unload_domain (domain)
212 struct loaded_domain *domain;
214 if (domain->use_mmap)
215 munmap ((caddr_t) domain->data, domain->mmap_size);
216 else
217 free ((void *) domain->data);
219 free (domain);
221 #endif