summary refs log tree commit diff
path: root/catgets/open_catalog.c
blob: 2a52877bbe2a57878a99353354f5a2d7a538feaf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* Copyright (C) 1996 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper, <drepper@gnu.ai.mit.edu>.

The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.

The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB.  If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

#include <endian.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>

#include "catgetsinfo.h"


#define SWAPU32(w) \
  (((w) << 24) | (((w) & 0xff00) << 8) | (((w) >> 8) & 0xff00) | ((w) >> 24))


void
__open_catalog (__nl_catd catalog, int with_path)
{
  int fd;
  struct stat st;
  int swapping;

  if (strchr (catalog->cat_name, '/') != NULL || !with_path)
    fd = open (catalog->cat_name, O_RDONLY);
  else
    {
      const char *run_nlspath = catalog->nlspath;
#define ENOUGH(n)							      \
  if (bufact + (n) >=bufmax) 						      \
    {									      \
      char *old_buf = buf;						      \
      bufmax += 256 + (n);						      \
      buf = (char *) alloca (bufmax);					      \
      memcpy (buf, old_buf, bufact);					      \
    }

      /* The RUN_NLSPATH variable contains a colon separated list of
	 descriptions where we expect to find catalogs.  We have to
	 recognize certain % substitutions and stop when we found the
	 first existing file.  */
      char *buf;
      size_t bufact;
      size_t bufmax;

      buf = NULL;
      bufmax = 0;

      fd = -1;
      while (*run_nlspath != '\0')
	{
	  bufact = 0;
	  while (*run_nlspath != ':' && *run_nlspath != '\0')
	    if (*run_nlspath == '%')
	      {
		const char *tmp;

		++run_nlspath;	/* We have seen the `%'.  */
		switch (*run_nlspath++)
		  {
		  case 'N':
		    /* Use the catalog name.  */
		    ENOUGH (strlen (catalog->cat_name));
		    bufact = __stpcpy (&buf[bufact], catalog->cat_name) - buf;
		    break;
		  case 'L':
		    /* Use the current locale category value.  */
		    ENOUGH (strlen (catalog->env_var));
		    bufact = __stpcpy (&buf[bufact], catalog->env_var) - buf;
		    break;
		  case 'l':
		    /* Use language element of locale category value.  */
		    tmp = catalog->env_var;
		    do
		      {
			ENOUGH (1);
			buf[bufact++] = *tmp++;
		      }
		    while (*tmp != '\0' && *tmp != '_' && *tmp != '.');
		    break;
		  case 't':
		    /* Use territory element of locale category value.  */
		    tmp = catalog->env_var;
		    do
		      ++tmp;
		    while (*tmp != '\0' && *tmp != '_' && *tmp != '.');
		    if (*tmp == '_')
		      {
			++tmp;
			do
			  {
			    ENOUGH (1);
			    buf[bufact++] = *tmp;
			  }
			while (*tmp != '\0' && *tmp != '.');
		      }
		    break;
		  case 'c':
		    /* Use code set element of locale category value.  */
		    tmp = catalog->env_var;
		    do
		      ++tmp;
		    while (*tmp != '\0' && *tmp != '.');
		    if (*tmp == '.')
		      {
			++tmp;
			do
			  {
			    ENOUGH (1);
			    buf[bufact++] = *tmp;
			  }
			while (*tmp != '\0');
		      }
		    break;
		  case '%':
		    ENOUGH (1);
		    buf[bufact++] = '%';
		    break;
		  default:
		    /* Unknown variable: ignore this path element.  */
		    bufact = 0;
		    while (*run_nlspath != '\0' && *run_nlspath != ':')
		      ++run_nlspath;
		    break;
		  }
	      }
	    else
	      {
		ENOUGH (1);
		buf[bufact++] = *run_nlspath++;
	      }
	  ENOUGH (1);
	  buf[bufact] = '\0';

	  if (bufact != 0)
	    {
	      fd = __open (buf, O_RDONLY);
	      if (fd >= 0)
		break;
	    }

	  ++run_nlspath;
	}
    }

  if (fd < 0 || __fstat (fd, &st) < 0)
    {
      catalog->status = nonexisting;
      return;
    }

#ifndef MAP_COPY
    /* Linux seems to lack read-only copy-on-write.  */
#define MAP_COPY MAP_PRIVATE
#endif
#ifndef MAP_FILE
    /* Some systems do not have this flag; it is superfluous.  */
#define MAP_FILE 0
#endif
#ifndef MAP_INHERIT
    /* Some systems might lack this; they lose.  */
#define MAP_INHERIT 0
#endif
  catalog->file_size = st.st_size;
  catalog->file_ptr =
    (struct catalog_obj *) __mmap (NULL, st.st_size, PROT_READ,
				   MAP_FILE|MAP_COPY|MAP_INHERIT, fd, 0);
  if (catalog->file_ptr != (struct catalog_obj *) -1)
    /* Tell the world we managed to mmap the file.  */
    catalog->status = mmaped;
  else
    {
      /* mmap failed perhaps because the system call is not
	 implemented.  Try to load the file.  */
      size_t todo;
      catalog->file_ptr = malloc (st.st_size);
      if (catalog->file_ptr == NULL)
	{
	  catalog->status = nonexisting;
	  return;
	}
      todo = st.st_size;
      /* Save read, handle partial reads.  */
      do
	{
	  size_t now = __read (fd, (((char *) &catalog->file_ptr)
				    + (st.st_size - todo)), todo);
	  if (now == 0)
	    {
	      free ((void *) catalog->file_ptr);
	      catalog->status = nonexisting;
	      return;
	    }
	  todo -= now;
	}
      while (todo > 0);
      catalog->status = malloced;
    }

  /* We don't need the file anymore.  */
  __close (fd);

  /* Determine whether the file is a catalog file and if yes whether
     it is written using the correct byte order.  Else we have to swap
     the values.  */
  if (catalog->file_ptr->magic == CATGETS_MAGIC)
    swapping = 0;
  else if (catalog->file_ptr->magic == SWAPU32 (CATGETS_MAGIC))
    swapping = 1;
  else
    {
      /* Illegal file.  Free he resources and mark catalog as not
	 usable.  */
      if (catalog->status == mmaped)
	__munmap ((void *) catalog->file_ptr, catalog->file_size);
      else
	free (catalog->file_ptr);
      catalog->status = nonexisting;
      return;
    }

#define SWAP(x) (swapping ? SWAPU32 (x) : (x))

  /* Get dimensions of the used hashing table.  */
  catalog->plane_size = SWAP (catalog->file_ptr->plane_size);
  catalog->plane_depth = SWAP (catalog->file_ptr->plane_depth);

  /* The file contains two versions of the pointer tables.  Pick the
     right one for the local byte order.  */
#if __BYTE_ORDER == __LITTLE_ENDIAN
  catalog->name_ptr = &catalog->file_ptr->name_ptr[0];
#elif __BYTE_ORDER == __BIG_ENDIAN
  catalog->name_ptr = &catalog->file_ptr->name_ptr[catalog->plane_size
						  * catalog->plane_depth
						  * 3];
#else
# error Cannot handle __BYTE_ORDER byte order
#endif

  /* The rest of the file contains all the strings.  They are
     addressed relative to the position of the first string.  */
  catalog->strings =
    (const char *) &catalog->file_ptr->name_ptr[catalog->plane_size
					       * catalog->plane_depth * 3 * 2];
}