Limited tsort replacement.
[glibc.git] / catgets / open_catalog.c
blobe4b61d779af7fe45229d7840414050a4e84ddc19
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. */
20 #include <byteswap.h>
21 #include <endian.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #ifdef _POSIX_MAPPED_FILES
28 # include <sys/mman.h>
29 #endif
30 #include <sys/stat.h>
32 #include "catgetsinfo.h"
35 #define SWAPU32(w) bswap_32 (w)
38 void
39 __open_catalog (__nl_catd catalog)
41 int fd = -1;
42 struct stat st;
43 int swapping;
44 size_t cnt;
45 size_t max_offset;
46 size_t tab_size;
47 const char *lastp;
49 /* Make sure we are alone. */
50 __libc_lock_lock (catalog->lock);
52 /* Check whether there was no other thread faster. */
53 if (catalog->status != closed)
54 /* While we waited some other thread tried to open the catalog. */
55 goto unlock_return;
57 if (strchr (catalog->cat_name, '/') != NULL || catalog->nlspath == NULL)
58 fd = __open (catalog->cat_name, O_RDONLY);
59 else
61 const char *run_nlspath = catalog->nlspath;
62 #define ENOUGH(n) \
63 if (bufact + (n) >=bufmax) \
64 { \
65 char *old_buf = buf; \
66 bufmax += 256 + (n); \
67 buf = (char *) alloca (bufmax); \
68 memcpy (buf, old_buf, bufact); \
71 /* The RUN_NLSPATH variable contains a colon separated list of
72 descriptions where we expect to find catalogs. We have to
73 recognize certain % substitutions and stop when we found the
74 first existing file. */
75 char *buf;
76 size_t bufact;
77 size_t bufmax;
78 size_t len;
80 buf = NULL;
81 bufmax = 0;
83 fd = -1;
84 while (*run_nlspath != '\0')
86 bufact = 0;
87 while (*run_nlspath != ':' && *run_nlspath != '\0')
88 if (*run_nlspath == '%')
90 const char *tmp;
92 ++run_nlspath; /* We have seen the `%'. */
93 switch (*run_nlspath++)
95 case 'N':
96 /* Use the catalog name. */
97 len = strlen (catalog->cat_name);
98 ENOUGH (len);
99 memcpy (&buf[bufact], catalog->cat_name, len);
100 bufact += len;
101 break;
102 case 'L':
103 /* Use the current locale category value. */
104 len = strlen (catalog->env_var);
105 ENOUGH (len);
106 memcpy (&buf[bufact], catalog->env_var, len);
107 bufact += len;
108 break;
109 case 'l':
110 /* Use language element of locale category value. */
111 tmp = catalog->env_var;
114 ENOUGH (1);
115 buf[bufact++] = *tmp++;
117 while (*tmp != '\0' && *tmp != '_' && *tmp != '.');
118 break;
119 case 't':
120 /* Use territory element of locale category value. */
121 tmp = catalog->env_var;
123 ++tmp;
124 while (*tmp != '\0' && *tmp != '_' && *tmp != '.');
125 if (*tmp == '_')
127 ++tmp;
130 ENOUGH (1);
131 buf[bufact++] = *tmp;
133 while (*tmp != '\0' && *tmp != '.');
135 break;
136 case 'c':
137 /* Use code set element of locale category value. */
138 tmp = catalog->env_var;
140 ++tmp;
141 while (*tmp != '\0' && *tmp != '.');
142 if (*tmp == '.')
144 ++tmp;
147 ENOUGH (1);
148 buf[bufact++] = *tmp;
150 while (*tmp != '\0');
152 break;
153 case '%':
154 ENOUGH (1);
155 buf[bufact++] = '%';
156 break;
157 default:
158 /* Unknown variable: ignore this path element. */
159 bufact = 0;
160 while (*run_nlspath != '\0' && *run_nlspath != ':')
161 ++run_nlspath;
162 break;
165 else
167 ENOUGH (1);
168 buf[bufact++] = *run_nlspath++;
170 ENOUGH (1);
171 buf[bufact] = '\0';
173 if (bufact != 0)
175 fd = __open (buf, O_RDONLY);
176 if (fd >= 0)
177 break;
180 ++run_nlspath;
184 /* Avoid dealing with directories and block devices */
185 if (fd < 0 || __fstat (fd, &st) < 0)
187 catalog->status = nonexisting;
188 goto unlock_return;
190 if (!S_ISREG (st.st_mode) || st.st_size < sizeof (struct catalog_obj))
192 /* `errno' is not set correctly but the file is not usable.
193 Use an reasonable error value. */
194 __set_errno (EINVAL);
195 catalog->status = nonexisting;
196 goto unlock_return;
199 catalog->file_size = st.st_size;
200 #ifdef _POSIX_MAPPED_FILES
201 # ifndef MAP_COPY
202 /* Linux seems to lack read-only copy-on-write. */
203 # define MAP_COPY MAP_PRIVATE
204 # endif
205 # ifndef MAP_FILE
206 /* Some systems do not have this flag; it is superfluous. */
207 # define MAP_FILE 0
208 # endif
209 # ifndef MAP_INHERIT
210 /* Some systems might lack this; they lose. */
211 # define MAP_INHERIT 0
212 # endif
213 catalog->file_ptr =
214 (struct catalog_obj *) __mmap (NULL, st.st_size, PROT_READ,
215 MAP_FILE|MAP_COPY|MAP_INHERIT, fd, 0);
216 if (catalog->file_ptr != (struct catalog_obj *) MAP_FAILED)
217 /* Tell the world we managed to mmap the file. */
218 catalog->status = mmapped;
219 else
220 #endif /* _POSIX_MAPPED_FILES */
222 /* mmap failed perhaps because the system call is not
223 implemented. Try to load the file. */
224 size_t todo;
225 catalog->file_ptr = malloc (st.st_size);
226 if (catalog->file_ptr == NULL)
228 catalog->status = nonexisting;
229 goto unlock_return;
231 todo = st.st_size;
232 /* Save read, handle partial reads. */
235 size_t now = __read (fd, (((char *) &catalog->file_ptr)
236 + (st.st_size - todo)), todo);
237 if (now == 0)
239 free ((void *) catalog->file_ptr);
240 catalog->status = nonexisting;
241 goto unlock_return;
243 todo -= now;
245 while (todo > 0);
246 catalog->status = malloced;
249 /* We don't need the file anymore. */
250 __close (fd);
251 fd = -1;
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
255 the values. */
256 if (catalog->file_ptr->magic == CATGETS_MAGIC)
257 swapping = 0;
258 else if (catalog->file_ptr->magic == SWAPU32 (CATGETS_MAGIC))
259 swapping = 1;
260 else
262 invalid_file:
263 /* Invalid file. Free the resources and mark catalog as not
264 usable. */
265 #ifdef _POSIX_MAPPED_FILES
266 if (catalog->status == mmapped)
267 __munmap ((void *) catalog->file_ptr, catalog->file_size);
268 else
269 #endif /* _POSIX_MAPPED_FILES */
270 free (catalog->file_ptr);
271 catalog->status = nonexisting;
272 goto unlock_return;
275 #define SWAP(x) (swapping ? SWAPU32 (x) : (x))
277 /* Get dimensions of the used hashing table. */
278 catalog->plane_size = SWAP (catalog->file_ptr->plane_size);
279 catalog->plane_depth = SWAP (catalog->file_ptr->plane_depth);
281 /* The file contains two versions of the pointer tables. Pick the
282 right one for the local byte order. */
283 #if __BYTE_ORDER == __LITTLE_ENDIAN
284 catalog->name_ptr = &catalog->file_ptr->name_ptr[0];
285 #elif __BYTE_ORDER == __BIG_ENDIAN
286 catalog->name_ptr = &catalog->file_ptr->name_ptr[catalog->plane_size
287 * catalog->plane_depth
288 * 3];
289 #else
290 # error Cannot handle __BYTE_ORDER byte order
291 #endif
293 /* The rest of the file contains all the strings. They are
294 addressed relative to the position of the first string. */
295 catalog->strings =
296 (const char *) &catalog->file_ptr->name_ptr[catalog->plane_size
297 * catalog->plane_depth * 3 * 2];
299 /* Determine the largest string offset mentioned in the table. */
300 max_offset = 0;
301 tab_size = 3 * catalog->plane_size * catalog->plane_depth;
302 for (cnt = 2; cnt < tab_size; cnt += 3)
303 if (catalog->name_ptr[cnt] > max_offset)
304 max_offset = catalog->name_ptr[cnt];
306 /* Now we can check whether the file is large enough to contain the
307 tables it says it contains. */
308 if (st.st_size <= (sizeof (struct catalog_obj) + 2 * tab_size + max_offset))
309 /* The last string is not contained in the file. */
310 goto invalid_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)
318 goto invalid_file;
319 ++lastp;
322 /* Release the lock again. */
323 unlock_return:
324 if (fd != -1)
325 __close (fd);
326 __libc_lock_unlock (catalog->lock);