xref: /openbsd-src/gnu/usr.bin/binutils-2.17/intl/loadmsgcat.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* Load needed message catalogs.
2*3d8817e4Smiod    Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3*3d8817e4Smiod 
4*3d8817e4Smiod    This program is free software; you can redistribute it and/or modify
5*3d8817e4Smiod    it under the terms of the GNU General Public License as published by
6*3d8817e4Smiod    the Free Software Foundation; either version 2, or (at your option)
7*3d8817e4Smiod    any later version.
8*3d8817e4Smiod 
9*3d8817e4Smiod    This program is distributed in the hope that it will be useful,
10*3d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
11*3d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*3d8817e4Smiod    GNU General Public License for more details.
13*3d8817e4Smiod 
14*3d8817e4Smiod    You should have received a copy of the GNU General Public License
15*3d8817e4Smiod    along with this program; if not, write to the Free Software Foundation,
16*3d8817e4Smiod    Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
17*3d8817e4Smiod 
18*3d8817e4Smiod #ifdef HAVE_CONFIG_H
19*3d8817e4Smiod # include <config.h>
20*3d8817e4Smiod #endif
21*3d8817e4Smiod 
22*3d8817e4Smiod #include <fcntl.h>
23*3d8817e4Smiod #include <sys/types.h>
24*3d8817e4Smiod #include <sys/stat.h>
25*3d8817e4Smiod 
26*3d8817e4Smiod #if defined STDC_HEADERS || defined _LIBC
27*3d8817e4Smiod # include <stdlib.h>
28*3d8817e4Smiod #endif
29*3d8817e4Smiod 
30*3d8817e4Smiod #if defined HAVE_UNISTD_H || defined _LIBC
31*3d8817e4Smiod # include <unistd.h>
32*3d8817e4Smiod #endif
33*3d8817e4Smiod 
34*3d8817e4Smiod #if (defined HAVE_MMAP && defined HAVE_MUNMAP) || defined _LIBC
35*3d8817e4Smiod # include <sys/mman.h>
36*3d8817e4Smiod #endif
37*3d8817e4Smiod 
38*3d8817e4Smiod #include "gettext.h"
39*3d8817e4Smiod #include "gettextP.h"
40*3d8817e4Smiod 
41*3d8817e4Smiod /* @@ end of prolog @@ */
42*3d8817e4Smiod 
43*3d8817e4Smiod #ifdef _LIBC
44*3d8817e4Smiod /* Rename the non ISO C functions.  This is required by the standard
45*3d8817e4Smiod    because some ISO C functions will require linking with this object
46*3d8817e4Smiod    file and the name space must not be polluted.  */
47*3d8817e4Smiod # define open   __open
48*3d8817e4Smiod # define close  __close
49*3d8817e4Smiod # define read   __read
50*3d8817e4Smiod # define mmap   __mmap
51*3d8817e4Smiod # define munmap __munmap
52*3d8817e4Smiod #endif
53*3d8817e4Smiod 
54*3d8817e4Smiod /* We need a sign, whether a new catalog was loaded, which can be associated
55*3d8817e4Smiod    with all translations.  This is important if the translations are
56*3d8817e4Smiod    cached by one of GCC's features.  */
57*3d8817e4Smiod int _nl_msg_cat_cntr = 0;
58*3d8817e4Smiod 
59*3d8817e4Smiod 
60*3d8817e4Smiod /* Load the message catalogs specified by FILENAME.  If it is no valid
61*3d8817e4Smiod    message catalog do nothing.  */
62*3d8817e4Smiod void
63*3d8817e4Smiod internal_function
_nl_load_domain(domain_file)64*3d8817e4Smiod _nl_load_domain (domain_file)
65*3d8817e4Smiod      struct loaded_l10nfile *domain_file;
66*3d8817e4Smiod {
67*3d8817e4Smiod   int fd;
68*3d8817e4Smiod   size_t size;
69*3d8817e4Smiod   struct stat st;
70*3d8817e4Smiod   struct mo_file_header *data = (struct mo_file_header *) -1;
71*3d8817e4Smiod #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
72*3d8817e4Smiod     || defined _LIBC
73*3d8817e4Smiod   int use_mmap = 0;
74*3d8817e4Smiod #endif
75*3d8817e4Smiod   struct loaded_domain *domain;
76*3d8817e4Smiod 
77*3d8817e4Smiod   domain_file->decided = 1;
78*3d8817e4Smiod   domain_file->data = NULL;
79*3d8817e4Smiod 
80*3d8817e4Smiod   /* If the record does not represent a valid locale the FILENAME
81*3d8817e4Smiod      might be NULL.  This can happen when according to the given
82*3d8817e4Smiod      specification the locale file name is different for XPG and CEN
83*3d8817e4Smiod      syntax.  */
84*3d8817e4Smiod   if (domain_file->filename == NULL)
85*3d8817e4Smiod     return;
86*3d8817e4Smiod 
87*3d8817e4Smiod   /* Try to open the addressed file.  */
88*3d8817e4Smiod   fd = open (domain_file->filename, O_RDONLY);
89*3d8817e4Smiod   if (fd == -1)
90*3d8817e4Smiod     return;
91*3d8817e4Smiod 
92*3d8817e4Smiod   /* We must know about the size of the file.  */
93*3d8817e4Smiod   if (fstat (fd, &st) != 0
94*3d8817e4Smiod       || (size = (size_t) st.st_size) != st.st_size
95*3d8817e4Smiod       || size < sizeof (struct mo_file_header))
96*3d8817e4Smiod     {
97*3d8817e4Smiod       /* Something went wrong.  */
98*3d8817e4Smiod       close (fd);
99*3d8817e4Smiod       return;
100*3d8817e4Smiod     }
101*3d8817e4Smiod 
102*3d8817e4Smiod #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
103*3d8817e4Smiod     || defined _LIBC
104*3d8817e4Smiod   /* Now we are ready to load the file.  If mmap() is available we try
105*3d8817e4Smiod      this first.  If not available or it failed we try to load it.  */
106*3d8817e4Smiod   data = (struct mo_file_header *) mmap (NULL, size, PROT_READ,
107*3d8817e4Smiod 					 MAP_PRIVATE, fd, 0);
108*3d8817e4Smiod 
109*3d8817e4Smiod   if (data != (struct mo_file_header *) -1)
110*3d8817e4Smiod     {
111*3d8817e4Smiod       /* mmap() call was successful.  */
112*3d8817e4Smiod       close (fd);
113*3d8817e4Smiod       use_mmap = 1;
114*3d8817e4Smiod     }
115*3d8817e4Smiod #endif
116*3d8817e4Smiod 
117*3d8817e4Smiod   /* If the data is not yet available (i.e. mmap'ed) we try to load
118*3d8817e4Smiod      it manually.  */
119*3d8817e4Smiod   if (data == (struct mo_file_header *) -1)
120*3d8817e4Smiod     {
121*3d8817e4Smiod       size_t to_read;
122*3d8817e4Smiod       char *read_ptr;
123*3d8817e4Smiod 
124*3d8817e4Smiod       data = (struct mo_file_header *) malloc (size);
125*3d8817e4Smiod       if (data == NULL)
126*3d8817e4Smiod 	return;
127*3d8817e4Smiod 
128*3d8817e4Smiod       to_read = size;
129*3d8817e4Smiod       read_ptr = (char *) data;
130*3d8817e4Smiod       do
131*3d8817e4Smiod 	{
132*3d8817e4Smiod 	  long int nb = (long int) read (fd, read_ptr, to_read);
133*3d8817e4Smiod 	  if (nb == -1)
134*3d8817e4Smiod 	    {
135*3d8817e4Smiod 	      close (fd);
136*3d8817e4Smiod 	      return;
137*3d8817e4Smiod 	    }
138*3d8817e4Smiod 
139*3d8817e4Smiod 	  read_ptr += nb;
140*3d8817e4Smiod 	  to_read -= nb;
141*3d8817e4Smiod 	}
142*3d8817e4Smiod       while (to_read > 0);
143*3d8817e4Smiod 
144*3d8817e4Smiod       close (fd);
145*3d8817e4Smiod     }
146*3d8817e4Smiod 
147*3d8817e4Smiod   /* Using the magic number we can test whether it really is a message
148*3d8817e4Smiod      catalog file.  */
149*3d8817e4Smiod   if (data->magic != _MAGIC && data->magic != _MAGIC_SWAPPED)
150*3d8817e4Smiod     {
151*3d8817e4Smiod       /* The magic number is wrong: not a message catalog file.  */
152*3d8817e4Smiod #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
153*3d8817e4Smiod     || defined _LIBC
154*3d8817e4Smiod       if (use_mmap)
155*3d8817e4Smiod 	munmap ((caddr_t) data, size);
156*3d8817e4Smiod       else
157*3d8817e4Smiod #endif
158*3d8817e4Smiod 	free (data);
159*3d8817e4Smiod       return;
160*3d8817e4Smiod     }
161*3d8817e4Smiod 
162*3d8817e4Smiod   domain_file->data
163*3d8817e4Smiod     = (struct loaded_domain *) malloc (sizeof (struct loaded_domain));
164*3d8817e4Smiod   if (domain_file->data == NULL)
165*3d8817e4Smiod     return;
166*3d8817e4Smiod 
167*3d8817e4Smiod   domain = (struct loaded_domain *) domain_file->data;
168*3d8817e4Smiod   domain->data = (char *) data;
169*3d8817e4Smiod #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
170*3d8817e4Smiod     || defined _LIBC
171*3d8817e4Smiod   domain->use_mmap = use_mmap;
172*3d8817e4Smiod #endif
173*3d8817e4Smiod   domain->mmap_size = size;
174*3d8817e4Smiod   domain->must_swap = data->magic != _MAGIC;
175*3d8817e4Smiod 
176*3d8817e4Smiod   /* Fill in the information about the available tables.  */
177*3d8817e4Smiod   switch (W (domain->must_swap, data->revision))
178*3d8817e4Smiod     {
179*3d8817e4Smiod     case 0:
180*3d8817e4Smiod       domain->nstrings = W (domain->must_swap, data->nstrings);
181*3d8817e4Smiod       domain->orig_tab = (struct string_desc *)
182*3d8817e4Smiod 	((char *) data + W (domain->must_swap, data->orig_tab_offset));
183*3d8817e4Smiod       domain->trans_tab = (struct string_desc *)
184*3d8817e4Smiod 	((char *) data + W (domain->must_swap, data->trans_tab_offset));
185*3d8817e4Smiod       domain->hash_size = W (domain->must_swap, data->hash_tab_size);
186*3d8817e4Smiod       domain->hash_tab = (nls_uint32 *)
187*3d8817e4Smiod 	((char *) data + W (domain->must_swap, data->hash_tab_offset));
188*3d8817e4Smiod       break;
189*3d8817e4Smiod     default:
190*3d8817e4Smiod       /* This is an illegal revision.  */
191*3d8817e4Smiod #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
192*3d8817e4Smiod     || defined _LIBC
193*3d8817e4Smiod       if (use_mmap)
194*3d8817e4Smiod 	munmap ((caddr_t) data, size);
195*3d8817e4Smiod       else
196*3d8817e4Smiod #endif
197*3d8817e4Smiod 	free (data);
198*3d8817e4Smiod       free (domain);
199*3d8817e4Smiod       domain_file->data = NULL;
200*3d8817e4Smiod       return;
201*3d8817e4Smiod     }
202*3d8817e4Smiod 
203*3d8817e4Smiod   /* Show that one domain is changed.  This might make some cached
204*3d8817e4Smiod      translations invalid.  */
205*3d8817e4Smiod   ++_nl_msg_cat_cntr;
206*3d8817e4Smiod }
207*3d8817e4Smiod 
208*3d8817e4Smiod 
209*3d8817e4Smiod #ifdef _LIBC
210*3d8817e4Smiod void
211*3d8817e4Smiod internal_function
_nl_unload_domain(domain)212*3d8817e4Smiod _nl_unload_domain (domain)
213*3d8817e4Smiod      struct loaded_domain *domain;
214*3d8817e4Smiod {
215*3d8817e4Smiod   if (domain->use_mmap)
216*3d8817e4Smiod     munmap ((caddr_t) domain->data, domain->mmap_size);
217*3d8817e4Smiod   else
218*3d8817e4Smiod     free ((void *) domain->data);
219*3d8817e4Smiod 
220*3d8817e4Smiod   free (domain);
221*3d8817e4Smiod }
222*3d8817e4Smiod #endif
223