Thu Mar 16 00:04:41 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[glibc.git] / locale / loadlocale.c
blob7ad7a21b53e66357d4effa64eaf94b0eabcf9e67
1 /* Functions to read locale data files.
2 Copyright (C) 1995 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <sys/stat.h>
27 #include <sys/mman.h>
28 #include "localeinfo.h"
30 const size_t _nl_category_num_items[] =
32 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
33 [category] = _NL_ITEM_INDEX (_NL_NUM_##category),
34 #include "categories.def"
35 #undef DEFINE_CATEGORY
38 struct locale_data *
39 _nl_load_locale (int category, char **name)
41 char *file;
42 int fd;
43 struct
45 unsigned int magic;
46 unsigned int nstrings;
47 unsigned int strindex[0];
48 } *filedata;
49 struct stat st;
50 struct locale_data *newdata;
51 int swap = 0;
52 inline unsigned int SWAP (const unsigned int *inw)
54 const unsigned char *inc = (const unsigned char *) inw;
55 if (!swap)
56 return *inw;
57 return (inc[3] << 24) | (inc[2] << 16) | (inc[1] << 8) | inc[0];
59 unsigned int i;
61 if ((*name)[0] == '\0')
63 *name = getenv ("LC_ALL");
64 if (! *name || (*name)[0] == '\0')
65 *name = getenv (_nl_category_names[category]);
66 if (! *name || (*name)[0] == '\0')
67 *name = getenv ("LANG");
68 if (! *name || (*name)[0] == '\0')
69 *name = (char *) "local";
72 /* XXX can't use asprintf here */
73 if (asprintf (&file, "%s%s/%s",
74 strchr (*name, '/') != NULL ? "" : "/share/locale/", /* XXX */
75 *name, _nl_category_names[category]) == -1)
76 return NULL;
78 fd = __open (file, O_RDONLY);
79 free (file);
80 if (fd < 0)
81 return NULL;
82 if (__fstat (fd, &st) < 0)
83 goto puntfd;
86 /* Map in the file's data. */
87 int save = errno;
88 #ifndef MAP_COPY
89 /* Linux seems to lack read-only copy-on-write. */
90 #define MAP_COPY MAP_PRIVATE
91 #endif
92 filedata = (void *) __mmap ((caddr_t) 0, st.st_size,
93 PROT_READ, MAP_FILE|MAP_COPY, fd, 0);
94 if (filedata == (void *) -1)
96 if (errno == ENOSYS)
98 /* No mmap; allocate a buffer and read from the file. */
99 filedata = malloc (st.st_size);
100 if (filedata)
102 off_t to_read = st.st_size;
103 ssize_t nread;
104 char *p = (char *) filedata;
105 while (to_read > 0)
107 nread = __read (fd, p, to_read);
108 if (nread <= 0)
110 free (filedata);
111 if (nread == 0)
112 errno = EINVAL; /* Bizarreness going on. */
113 goto puntfd;
115 p += nread;
116 to_read -= nread;
119 else
120 goto puntfd;
121 errno = save;
123 else
124 goto puntfd;
128 if (filedata->magic == LIMAGIC (category))
129 /* Good data file in our byte order. */
130 swap = 0;
131 else
133 /* Try the other byte order. */
134 swap = 1;
135 if (SWAP (&filedata->magic) != LIMAGIC (category))
136 /* Bad data file in either byte order. */
138 puntmap:
139 __munmap ((caddr_t) filedata, st.st_size);
140 puntfd:
141 __close (fd);
142 return NULL;
146 #define W(word) SWAP (&(word))
148 if (W (filedata->nstrings) < _nl_category_num_items[category] ||
149 (sizeof *filedata + W (filedata->nstrings) * sizeof (unsigned int)
150 >= st.st_size))
152 /* Insufficient data. */
153 errno = EINVAL;
154 goto puntmap;
157 newdata = malloc (sizeof *newdata +
158 W (filedata->nstrings) * sizeof (char *));
159 if (! newdata)
160 goto puntmap;
162 newdata->filedata = (void *) filedata;
163 newdata->filesize = st.st_size;
164 newdata->nstrings = W (filedata->nstrings);
165 for (i = 0; i < newdata->nstrings; ++i)
167 unsigned int idx = W (filedata->strindex[i]);
168 if (idx >= newdata->filesize)
170 free (newdata);
171 errno = EINVAL;
172 goto puntmap;
174 newdata->strings[i] = newdata->filedata + idx;
177 __close (fd);
178 return newdata;
181 void
182 _nl_free_locale (struct locale_data *data)
184 int save = errno;
185 if (__munmap ((caddr_t) data->filedata, data->filesize) < 0)
187 if (errno == ENOSYS)
188 free ((void *) data->filedata);
189 errno = save;
191 free (data);