1 /* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper, <drepper@gnu.ai.mit.edu>.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
29 #include "catgetsinfo.h"
32 #define SWAPU32(w) bswap_32 (w)
36 __open_catalog (__nl_catd catalog
)
42 /* Make sure we are alone. */
43 __libc_lock_lock (catalog
->lock
);
45 /* Check whether there was no other thread faster. */
46 if (catalog
->status
!= closed
)
47 /* While we waited some other thread tried to open the catalog. */
50 if (strchr (catalog
->cat_name
, '/') != NULL
|| catalog
->nlspath
== NULL
)
51 fd
= __open (catalog
->cat_name
, O_RDONLY
);
54 const char *run_nlspath
= catalog
->nlspath
;
56 if (bufact + (n) >=bufmax) \
58 char *old_buf = buf; \
59 bufmax += 256 + (n); \
60 buf = (char *) alloca (bufmax); \
61 memcpy (buf, old_buf, bufact); \
64 /* The RUN_NLSPATH variable contains a colon separated list of
65 descriptions where we expect to find catalogs. We have to
66 recognize certain % substitutions and stop when we found the
67 first existing file. */
77 while (*run_nlspath
!= '\0')
80 while (*run_nlspath
!= ':' && *run_nlspath
!= '\0')
81 if (*run_nlspath
== '%')
85 ++run_nlspath
; /* We have seen the `%'. */
86 switch (*run_nlspath
++)
89 /* Use the catalog name. */
90 len
= strlen (catalog
->cat_name
);
92 memcpy (&buf
[bufact
], catalog
->cat_name
, len
);
96 /* Use the current locale category value. */
97 len
= strlen (catalog
->env_var
);
99 memcpy (&buf
[bufact
], catalog
->env_var
, len
);
103 /* Use language element of locale category value. */
104 tmp
= catalog
->env_var
;
108 buf
[bufact
++] = *tmp
++;
110 while (*tmp
!= '\0' && *tmp
!= '_' && *tmp
!= '.');
113 /* Use territory element of locale category value. */
114 tmp
= catalog
->env_var
;
117 while (*tmp
!= '\0' && *tmp
!= '_' && *tmp
!= '.');
124 buf
[bufact
++] = *tmp
;
126 while (*tmp
!= '\0' && *tmp
!= '.');
130 /* Use code set element of locale category value. */
131 tmp
= catalog
->env_var
;
134 while (*tmp
!= '\0' && *tmp
!= '.');
141 buf
[bufact
++] = *tmp
;
143 while (*tmp
!= '\0');
151 /* Unknown variable: ignore this path element. */
153 while (*run_nlspath
!= '\0' && *run_nlspath
!= ':')
161 buf
[bufact
++] = *run_nlspath
++;
168 fd
= __open (buf
, O_RDONLY
);
177 /* Avoid dealing with directories and block devices */
178 if (fd
< 0 || __fstat (fd
, &st
) < 0 || !S_ISREG (st
.st_mode
))
180 catalog
->status
= nonexisting
;
185 /* Linux seems to lack read-only copy-on-write. */
186 # define MAP_COPY MAP_PRIVATE
189 /* Some systems do not have this flag; it is superfluous. */
193 /* Some systems might lack this; they lose. */
194 # define MAP_INHERIT 0
196 catalog
->file_size
= st
.st_size
;
198 (struct catalog_obj
*) __mmap (NULL
, st
.st_size
, PROT_READ
,
199 MAP_FILE
|MAP_COPY
|MAP_INHERIT
, fd
, 0);
200 if (catalog
->file_ptr
!= (struct catalog_obj
*) MAP_FAILED
)
201 /* Tell the world we managed to mmap the file. */
202 catalog
->status
= mmapped
;
205 /* mmap failed perhaps because the system call is not
206 implemented. Try to load the file. */
208 catalog
->file_ptr
= malloc (st
.st_size
);
209 if (catalog
->file_ptr
== NULL
)
211 catalog
->status
= nonexisting
;
215 /* Save read, handle partial reads. */
218 size_t now
= __read (fd
, (((char *) &catalog
->file_ptr
)
219 + (st
.st_size
- todo
)), todo
);
222 free ((void *) catalog
->file_ptr
);
223 catalog
->status
= nonexisting
;
229 catalog
->status
= malloced
;
232 /* We don't need the file anymore. */
236 /* Determine whether the file is a catalog file and if yes whether
237 it is written using the correct byte order. Else we have to swap
239 if (catalog
->file_ptr
->magic
== CATGETS_MAGIC
)
241 else if (catalog
->file_ptr
->magic
== SWAPU32 (CATGETS_MAGIC
))
245 /* Invalid file. Free the resources and mark catalog as not
247 if (catalog
->status
== mmapped
)
248 __munmap ((void *) catalog
->file_ptr
, catalog
->file_size
);
250 free (catalog
->file_ptr
);
251 catalog
->status
= nonexisting
;
255 #define SWAP(x) (swapping ? SWAPU32 (x) : (x))
257 /* Get dimensions of the used hashing table. */
258 catalog
->plane_size
= SWAP (catalog
->file_ptr
->plane_size
);
259 catalog
->plane_depth
= SWAP (catalog
->file_ptr
->plane_depth
);
261 /* The file contains two versions of the pointer tables. Pick the
262 right one for the local byte order. */
263 #if __BYTE_ORDER == __LITTLE_ENDIAN
264 catalog
->name_ptr
= &catalog
->file_ptr
->name_ptr
[0];
265 #elif __BYTE_ORDER == __BIG_ENDIAN
266 catalog
->name_ptr
= &catalog
->file_ptr
->name_ptr
[catalog
->plane_size
267 * catalog
->plane_depth
270 # error Cannot handle __BYTE_ORDER byte order
273 /* The rest of the file contains all the strings. They are
274 addressed relative to the position of the first string. */
276 (const char *) &catalog
->file_ptr
->name_ptr
[catalog
->plane_size
277 * catalog
->plane_depth
* 3 * 2];
279 /* Release the lock again. */
283 __libc_lock_unlock (catalog
->lock
);