1 /* Copyright (C) 1996 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
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
28 #include "catgetsinfo.h"
32 (((w) << 24) | (((w) & 0xff00) << 8) | (((w) >> 8) & 0xff00) | ((w) >> 24))
36 __open_catalog (__nl_catd catalog
, int with_path
)
42 if (strchr (catalog
->cat_name
, '/') != NULL
|| !with_path
)
43 fd
= open (catalog
->cat_name
, O_RDONLY
);
46 const char *run_nlspath
= catalog
->nlspath
;
48 if (bufact + (n) >=bufmax) \
50 char *old_buf = buf; \
51 bufmax += 256 + (n); \
52 buf = (char *) alloca (bufmax); \
53 memcpy (buf, old_buf, bufact); \
56 /* The RUN_NLSPATH variable contains a colon separated list of
57 descriptions where we expect to find catalogs. We have to
58 recognize certain % substitutions and stop when we found the
59 first existing file. */
68 while (*run_nlspath
!= '\0')
71 while (*run_nlspath
!= ':' && *run_nlspath
!= '\0')
72 if (*run_nlspath
== '%')
76 ++run_nlspath
; /* We have seen the `%'. */
77 switch (*run_nlspath
++)
80 /* Use the catalog name. */
81 ENOUGH (strlen (catalog
->cat_name
));
82 bufact
= __stpcpy (&buf
[bufact
], catalog
->cat_name
) - buf
;
85 /* Use the current locale category value. */
86 ENOUGH (strlen (catalog
->env_var
));
87 bufact
= __stpcpy (&buf
[bufact
], catalog
->env_var
) - buf
;
90 /* Use language element of locale category value. */
91 tmp
= catalog
->env_var
;
95 buf
[bufact
++] = *tmp
++;
97 while (*tmp
!= '\0' && *tmp
!= '_' && *tmp
!= '.');
100 /* Use territory element of locale category value. */
101 tmp
= catalog
->env_var
;
104 while (*tmp
!= '\0' && *tmp
!= '_' && *tmp
!= '.');
111 buf
[bufact
++] = *tmp
;
113 while (*tmp
!= '\0' && *tmp
!= '.');
117 /* Use code set element of locale category value. */
118 tmp
= catalog
->env_var
;
121 while (*tmp
!= '\0' && *tmp
!= '.');
128 buf
[bufact
++] = *tmp
;
130 while (*tmp
!= '\0');
138 /* Unknown variable: ignore this path element. */
140 while (*run_nlspath
!= '\0' && *run_nlspath
!= ':')
148 buf
[bufact
++] = *run_nlspath
++;
155 fd
= __open (buf
, O_RDONLY
);
164 if (fd
< 0 || __fstat (fd
, &st
) < 0)
166 catalog
->status
= nonexisting
;
171 /* Linux seems to lack read-only copy-on-write. */
172 #define MAP_COPY MAP_PRIVATE
175 /* Some systems do not have this flag; it is superfluous. */
179 /* Some systems might lack this; they lose. */
180 #define MAP_INHERIT 0
182 catalog
->file_size
= st
.st_size
;
184 (struct catalog_obj
*) __mmap (NULL
, st
.st_size
, PROT_READ
,
185 MAP_FILE
|MAP_COPY
|MAP_INHERIT
, fd
, 0);
186 if (catalog
->file_ptr
!= (struct catalog_obj
*) -1)
187 /* Tell the world we managed to mmap the file. */
188 catalog
->status
= mmaped
;
191 /* mmap failed perhaps because the system call is not
192 implemented. Try to load the file. */
194 catalog
->file_ptr
= malloc (st
.st_size
);
195 if (catalog
->file_ptr
== NULL
)
197 catalog
->status
= nonexisting
;
201 /* Save read, handle partial reads. */
204 size_t now
= __read (fd
, (((char *) &catalog
->file_ptr
)
205 + (st
.st_size
- todo
)), todo
);
208 free ((void *) catalog
->file_ptr
);
209 catalog
->status
= nonexisting
;
215 catalog
->status
= malloced
;
218 /* We don't need the file anymore. */
221 /* Determine whether the file is a catalog file and if yes whether
222 it is written using the correct byte order. Else we have to swap
224 if (catalog
->file_ptr
->magic
== CATGETS_MAGIC
)
226 else if (catalog
->file_ptr
->magic
== SWAPU32 (CATGETS_MAGIC
))
230 /* Illegal file. Free he resources and mark catalog as not
232 if (catalog
->status
== mmaped
)
233 __munmap ((void *) catalog
->file_ptr
, catalog
->file_size
);
235 free (catalog
->file_ptr
);
236 catalog
->status
= nonexisting
;
240 #define SWAP(x) (swapping ? SWAPU32 (x) : (x))
242 /* Get dimensions of the used hashing table. */
243 catalog
->plane_size
= SWAP (catalog
->file_ptr
->plane_size
);
244 catalog
->plane_depth
= SWAP (catalog
->file_ptr
->plane_depth
);
246 /* The file contains two versions of the pointer tables. Pick the
247 right one for the local byte order. */
248 #if __BYTE_ORDER == __LITTLE_ENDIAN
249 catalog
->name_ptr
= &catalog
->file_ptr
->name_ptr
[0];
250 #elif __BYTE_ORDER == __BIG_ENDIAN
251 catalog
->name_ptr
= &catalog
->file_ptr
->name_ptr
[catalog
->plane_size
252 * catalog
->plane_depth
255 # error Cannot handle __BYTE_ORDER byte order
258 /* The rest of the file contains all the strings. They are
259 addressed relative to the position of the first string. */
261 (const char *) &catalog
->file_ptr
->name_ptr
[catalog
->plane_size
262 * catalog
->plane_depth
* 3 * 2];