Fri Nov 3 17:27:49 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[glibc.git] / intl / loadmsgcat.c
blobb05c6307243eaecf8e1bc117ddd7ebaba5eee898
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)
7 any later version.
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. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include <fcntl.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
26 #if defined STDC_HEADERS || defined _LIBC
27 # include <stdlib.h>
28 #endif
30 #if defined HAVE_UNISTD_H || defined _LIBC
31 # include <unistd.h>
32 #endif
34 #if (defined HAVE_MMAP && defined HAVE_MUNMAP) || defined _LIBC
35 # include <sys/mman.h>
36 #endif
38 #include "gettext.h"
39 #include "gettextP.h"
41 /* @@ end of prolog @@ */
43 #ifdef _LIBC
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
48 # define open __open
49 # define close __close
50 # define read __read
51 # define mmap __mmap
52 # define munmap __munmap
53 #endif
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. */
58 int _nl_msg_cat_cntr;
61 /* Load the message catalogs specified by FILENAME. If it is no valid
62 message catalog do nothing. */
63 void
64 _nl_load_domain (domain)
65 struct loaded_domain *domain;
67 int fd;
68 struct stat st;
69 struct mo_file_header *data = (struct mo_file_header *) -1;
70 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
71 || defined _LIBC
72 int use_mmap = 0;
73 #endif
75 domain->decided = 1;
76 domain->data = NULL;
78 /* Try to open the addressed file. */
79 fd = open (domain->filename, O_RDONLY);
80 if (fd == -1)
81 return;
83 /* We must know about the size of the file. */
84 if (fstat (fd, &st) != 0
85 && st.st_size < (off_t) sizeof (struct mo_file_header))
87 /* Something went wrong. */
88 close (fd);
89 return;
92 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
93 || defined _LIBC
94 /* Now we are ready to load the file. If mmap() is available we try
95 this first. If not available or it failed we try to load it. */
96 data = (struct mo_file_header *) mmap (NULL, st.st_size, PROT_READ,
97 MAP_PRIVATE, fd, 0);
99 if (data != (struct mo_file_header *) -1)
101 /* mmap() call was successful. */
102 close (fd);
103 use_mmap = 1;
105 #endif
107 /* If the data is not yet available (i.e. mmap'ed) we try to load
108 it manually. */
109 if (data == (struct mo_file_header *) -1)
111 off_t to_read;
112 char *read_ptr;
114 data = (struct mo_file_header *) malloc (st.st_size);
115 if (data == NULL)
116 return;
118 to_read = st.st_size;
119 read_ptr = (char *) data;
122 long int nb = (long int) read (fd, read_ptr, to_read);
123 if (nb == -1)
125 close (fd);
126 return;
129 read_ptr += nb;
130 to_read -= nb;
132 while (to_read > 0);
134 close (fd);
137 /* Using the magic number we can test whether it really is a message
138 catalog file. */
139 if (data->magic != _MAGIC && data->magic != _MAGIC_SWAPPED)
141 /* The magic number is wrong: not a message catalog file. */
142 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
143 || defined _LIBC
144 if (use_mmap)
145 munmap ((caddr_t) data, st.st_size);
146 else
147 #endif
148 free (data);
149 return;
152 domain->data = (char *) data;
153 domain->must_swap = data->magic != _MAGIC;
155 /* Fill in the information about the available tables. */
156 switch (W (domain->must_swap, data->revision))
158 case 0:
159 domain->nstrings = W (domain->must_swap, data->nstrings);
160 domain->orig_tab = (struct string_desc *)
161 ((char *) data + W (domain->must_swap, data->orig_tab_offset));
162 domain->trans_tab = (struct string_desc *)
163 ((char *) data + W (domain->must_swap, data->trans_tab_offset));
164 domain->hash_size = W (domain->must_swap, data->hash_tab_size);
165 domain->hash_tab = (nls_uint32 *)
166 ((char *) data + W (domain->must_swap, data->hash_tab_offset));
167 break;
168 default:
169 /* This is an illegal revision. */
170 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
171 || defined _LIBC
172 if (use_mmap)
173 munmap ((caddr_t) data, st.st_size);
174 else
175 #endif
176 free (data);
177 domain->data = NULL;
178 return;
181 /* Show that one domain is changed. This might make some cached
182 translation invalid. */
183 ++_nl_msg_cat_cntr;