]> git.ipfire.org Git - thirdparty/glibc.git/blob - intl/loadmsgcat.c
* locale/programs/xmalloc.c: Test _LIBC as well as STDC_HEADERS.
[thirdparty/glibc.git] / intl / loadmsgcat.c
1 /* loadmsgcat.c -- load needed message catalogs
2 Copyright (C) 1995 Software Foundation, Inc.
3
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.
6
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.
11
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.
16
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. */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29
30 #if defined STDC_HEADERS || defined _LIBC
31 # include <stdlib.h>
32 #endif
33
34 #if defined HAVE_UNISTD_H || defined _LIBC
35 # include <unistd.h>
36 #endif
37
38 #if (defined HAVE_MMAP && defined HAVE_MUNMAP) || defined _LIBC
39 # include <sys/mman.h>
40 #endif
41
42 #include "gettext.h"
43 #include "gettextP.h"
44
45 /* @@ end of prolog @@ */
46
47 #ifdef _LIBC
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
52 # define open __open
53 # define close __close
54 # define read __read
55 # define mmap __mmap
56 # define munmap __munmap
57 #endif
58
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;
63
64
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)
69 struct loaded_domain *domain;
70 {
71 int fd;
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
79 domain->decided = 1;
80 domain->data = NULL;
81
82 /* If the record does not represent a valid locale the FILENAME
83 might be NULL. This can happen when according to the given
84 specification the locale file name is different for XPG and CEN
85 syntax. */
86 if (domain->filename == NULL)
87 return;
88
89 /* Try to open the addressed file. */
90 fd = open (domain->filename, O_RDONLY);
91 if (fd == -1)
92 return;
93
94 /* We must know about the size of the file. */
95 if (fstat (fd, &st) != 0
96 && st.st_size < (off_t) sizeof (struct mo_file_header))
97 {
98 /* Something went wrong. */
99 close (fd);
100 return;
101 }
102
103 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
104 || defined _LIBC
105 /* Now we are ready to load the file. If mmap() is available we try
106 this first. If not available or it failed we try to load it. */
107 data = (struct mo_file_header *) mmap (NULL, st.st_size, PROT_READ,
108 MAP_PRIVATE, fd, 0);
109
110 if (data != (struct mo_file_header *) -1)
111 {
112 /* mmap() call was successful. */
113 close (fd);
114 use_mmap = 1;
115 }
116 #endif
117
118 /* If the data is not yet available (i.e. mmap'ed) we try to load
119 it manually. */
120 if (data == (struct mo_file_header *) -1)
121 {
122 off_t to_read;
123 char *read_ptr;
124
125 data = (struct mo_file_header *) malloc (st.st_size);
126 if (data == NULL)
127 return;
128
129 to_read = st.st_size;
130 read_ptr = (char *) data;
131 do
132 {
133 long int nb = (long int) read (fd, read_ptr, to_read);
134 if (nb == -1)
135 {
136 close (fd);
137 return;
138 }
139
140 read_ptr += nb;
141 to_read -= nb;
142 }
143 while (to_read > 0);
144
145 close (fd);
146 }
147
148 /* Using the magic number we can test whether it really is a message
149 catalog file. */
150 if (data->magic != _MAGIC && data->magic != _MAGIC_SWAPPED)
151 {
152 /* The magic number is wrong: not a message catalog file. */
153 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
154 || defined _LIBC
155 if (use_mmap)
156 munmap ((caddr_t) data, st.st_size);
157 else
158 #endif
159 free (data);
160 return;
161 }
162
163 domain->data = (char *) data;
164 domain->must_swap = data->magic != _MAGIC;
165
166 /* Fill in the information about the available tables. */
167 switch (W (domain->must_swap, data->revision))
168 {
169 case 0:
170 domain->nstrings = W (domain->must_swap, data->nstrings);
171 domain->orig_tab = (struct string_desc *)
172 ((char *) data + W (domain->must_swap, data->orig_tab_offset));
173 domain->trans_tab = (struct string_desc *)
174 ((char *) data + W (domain->must_swap, data->trans_tab_offset));
175 domain->hash_size = W (domain->must_swap, data->hash_tab_size);
176 domain->hash_tab = (nls_uint32 *)
177 ((char *) data + W (domain->must_swap, data->hash_tab_offset));
178 break;
179 default:
180 /* This is an illegal revision. */
181 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
182 || defined _LIBC
183 if (use_mmap)
184 munmap ((caddr_t) data, st.st_size);
185 else
186 #endif
187 free (data);
188 domain->data = NULL;
189 return;
190 }
191
192 /* Show that one domain is changed. This might make some cached
193 translation invalid. */
194 ++_nl_msg_cat_cntr;
195 }