1 /* Copyright (C) 1996-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper, <drepper@gnu.org>.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
26 #ifdef _POSIX_MAPPED_FILES
27 # include <sys/mman.h>
31 #include "catgetsinfo.h"
32 #include <not-cancel.h>
35 #define SWAPU32(w) bswap_32 (w)
39 __open_catalog (const char *cat_name
, const char *nlspath
, const char *env_var
,
51 if (strchr (cat_name
, '/') != NULL
|| nlspath
== NULL
)
52 fd
= open_not_cancel_2 (cat_name
, O_RDONLY
);
55 const char *run_nlspath
= nlspath
;
57 if (__glibc_unlikely (bufact + (n) >= bufmax)) \
59 char *old_buf = buf; \
60 bufmax += 256 + (n); \
61 buf = (char *) alloca (bufmax); \
62 memcpy (buf, old_buf, bufact); \
65 /* The RUN_NLSPATH variable contains a colon separated list of
66 descriptions where we expect to find catalogs. We have to
67 recognize certain % substitutions and stop when we found the
68 first existing file. */
78 while (*run_nlspath
!= '\0')
82 if (*run_nlspath
== ':')
84 /* Leading colon or adjacent colons - treat same as %N. */
85 len
= strlen (cat_name
);
87 memcpy (&buf
[bufact
], cat_name
, len
);
91 while (*run_nlspath
!= ':' && *run_nlspath
!= '\0')
92 if (*run_nlspath
== '%')
96 ++run_nlspath
; /* We have seen the `%'. */
97 switch (*run_nlspath
++)
100 /* Use the catalog name. */
101 len
= strlen (cat_name
);
103 memcpy (&buf
[bufact
], cat_name
, len
);
107 /* Use the current locale category value. */
108 len
= strlen (env_var
);
110 memcpy (&buf
[bufact
], env_var
, len
);
114 /* Use language element of locale category value. */
119 buf
[bufact
++] = *tmp
++;
121 while (*tmp
!= '\0' && *tmp
!= '_' && *tmp
!= '.');
124 /* Use territory element of locale category value. */
128 while (*tmp
!= '\0' && *tmp
!= '_' && *tmp
!= '.');
135 buf
[bufact
++] = *tmp
++;
137 while (*tmp
!= '\0' && *tmp
!= '.');
141 /* Use code set element of locale category value. */
145 while (*tmp
!= '\0' && *tmp
!= '.');
152 buf
[bufact
++] = *tmp
++;
154 while (*tmp
!= '\0');
162 /* Unknown variable: ignore this path element. */
164 while (*run_nlspath
!= '\0' && *run_nlspath
!= ':')
172 buf
[bufact
++] = *run_nlspath
++;
180 fd
= open_not_cancel_2 (buf
, O_RDONLY
);
189 /* Avoid dealing with directories and block devices */
190 if (__builtin_expect (fd
, 0) < 0)
193 if (__builtin_expect (__fxstat64 (_STAT_VER
, fd
, &st
), 0) < 0)
194 goto close_unlock_return
;
196 if (__builtin_expect (!S_ISREG (st
.st_mode
), 0)
197 || (size_t) st
.st_size
< sizeof (struct catalog_obj
))
199 /* `errno' is not set correctly but the file is not usable.
200 Use an reasonable error value. */
201 __set_errno (EINVAL
);
202 goto close_unlock_return
;
205 catalog
->file_size
= st
.st_size
;
206 #ifdef _POSIX_MAPPED_FILES
208 /* Linux seems to lack read-only copy-on-write. */
209 # define MAP_COPY MAP_PRIVATE
212 /* Some systems do not have this flag; it is superfluous. */
216 (struct catalog_obj
*) __mmap (NULL
, st
.st_size
, PROT_READ
,
217 MAP_FILE
|MAP_COPY
, fd
, 0);
218 if (__builtin_expect (catalog
->file_ptr
!= (struct catalog_obj
*) MAP_FAILED
,
220 /* Tell the world we managed to mmap the file. */
221 catalog
->status
= mmapped
;
223 #endif /* _POSIX_MAPPED_FILES */
225 /* mmap failed perhaps because the system call is not
226 implemented. Try to load the file. */
228 catalog
->file_ptr
= malloc (st
.st_size
);
229 if (catalog
->file_ptr
== NULL
)
230 goto close_unlock_return
;
233 /* Save read, handle partial reads. */
236 size_t now
= read_not_cancel (fd
, (((char *) catalog
->file_ptr
)
237 + (st
.st_size
- todo
)), todo
);
238 if (now
== 0 || now
== (size_t) -1)
241 if (now
== (size_t) -1 && errno
== EINTR
)
244 free ((void *) catalog
->file_ptr
);
245 goto close_unlock_return
;
250 catalog
->status
= malloced
;
253 /* Determine whether the file is a catalog file and if yes whether
254 it is written using the correct byte order. Else we have to swap
256 if (__glibc_likely (catalog
->file_ptr
->magic
== CATGETS_MAGIC
))
258 else if (catalog
->file_ptr
->magic
== SWAPU32 (CATGETS_MAGIC
))
263 /* Invalid file. Free the resources and mark catalog as not
265 #ifdef _POSIX_MAPPED_FILES
266 if (catalog
->status
== mmapped
)
267 __munmap ((void *) catalog
->file_ptr
, catalog
->file_size
);
269 #endif /* _POSIX_MAPPED_FILES */
270 free (catalog
->file_ptr
);
271 goto close_unlock_return
;
274 #define SWAP(x) (swapping ? SWAPU32 (x) : (x))
276 /* Get dimensions of the used hashing table. */
277 catalog
->plane_size
= SWAP (catalog
->file_ptr
->plane_size
);
278 catalog
->plane_depth
= SWAP (catalog
->file_ptr
->plane_depth
);
280 /* The file contains two versions of the pointer tables. Pick the
281 right one for the local byte order. */
282 #if __BYTE_ORDER == __LITTLE_ENDIAN
283 catalog
->name_ptr
= &catalog
->file_ptr
->name_ptr
[0];
284 #elif __BYTE_ORDER == __BIG_ENDIAN
285 catalog
->name_ptr
= &catalog
->file_ptr
->name_ptr
[catalog
->plane_size
286 * catalog
->plane_depth
289 # error Cannot handle __BYTE_ORDER byte order
292 /* The rest of the file contains all the strings. They are
293 addressed relative to the position of the first string. */
295 (const char *) &catalog
->file_ptr
->name_ptr
[catalog
->plane_size
296 * catalog
->plane_depth
* 3 * 2];
298 /* Determine the largest string offset mentioned in the table. */
300 tab_size
= 3 * catalog
->plane_size
* catalog
->plane_depth
;
301 for (cnt
= 2; cnt
< tab_size
; cnt
+= 3)
302 if (catalog
->name_ptr
[cnt
] > max_offset
)
303 max_offset
= catalog
->name_ptr
[cnt
];
305 /* Now we can check whether the file is large enough to contain the
306 tables it says it contains. */
307 if ((size_t) st
.st_size
308 <= (sizeof (struct catalog_obj
) + 2 * tab_size
+ max_offset
))
309 /* The last string is not contained in the file. */
312 lastp
= catalog
->strings
+ max_offset
;
313 max_offset
= (st
.st_size
314 - sizeof (struct catalog_obj
) + 2 * tab_size
+ max_offset
);
315 while (*lastp
!= '\0')
317 if (--max_offset
== 0)
325 /* Release the lock again. */
327 close_not_cancel_no_status (fd
);
331 libc_hidden_def (__open_catalog
)