1 /* Copyright (C) 1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1996.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
30 #include <sys/param.h>
33 #include "localedef.h"
36 #include "locfile-kw.h"
40 locfile_read (struct localedef_t
*result
, struct charmap_t
*charmap
)
42 const char *filename
= result
->name
;
43 const char *repertoire_name
= result
->repertoire_name
;
44 int locale_mask
= result
->needed
^ result
->avail
;
45 struct linereader
*ldfile
;
46 int not_here
= ALL_LOCALES
;
48 /* If no repertoire name was specified use the global one. */
49 if (repertoire_name
== NULL
)
50 repertoire_name
= repertoire_global
;
52 /* Open the locale definition file. */
53 ldfile
= lr_open (filename
, locfile_hash
);
56 if (filename
!= NULL
&& filename
[0] != '/')
58 char *i18npath
= getenv ("I18NPATH");
59 if (i18npath
!= NULL
&& *i18npath
!= '\0')
61 char path
[strlen (filename
) + 1 + strlen (i18npath
)
62 + sizeof ("/locales/") - 1];
64 i18npath
= strdupa (i18npath
);
68 && (next
= strsep (&i18npath
, ":")) != NULL
)
70 stpcpy (stpcpy (stpcpy (path
, next
), "/locales/"), filename
);
72 ldfile
= lr_open (path
, locfile_hash
);
76 stpcpy (stpcpy (path
, next
), filename
);
78 ldfile
= lr_open (path
, locfile_hash
);
83 /* Test in the default directory. */
86 char path
[strlen (filename
) + 1 + sizeof (LOCSRCDIR
)];
88 stpcpy (stpcpy (stpcpy (path
, LOCSRCDIR
), "/"), filename
);
89 ldfile
= lr_open (path
, locfile_hash
);
97 /* Parse locale definition file and store result in RESULT. */
100 struct token
*now
= lr_token (ldfile
, charmap
, NULL
, verbose
);
101 enum token_t nowtok
= now
->tok
;
104 if (nowtok
== tok_eof
)
107 if (nowtok
== tok_eol
)
108 /* Ignore empty lines. */
113 case tok_escape_char
:
114 case tok_comment_char
:
115 /* We need an argument. */
116 arg
= lr_token (ldfile
, charmap
, NULL
, verbose
);
118 if (arg
->tok
!= tok_ident
)
120 SYNTAX_ERROR (_("bad argument"));
124 if (arg
->val
.str
.lenmb
!= 1)
126 lr_error (ldfile
, _("\
127 argument to `%s' must be a single character"),
128 nowtok
== tok_escape_char
129 ? "escape_char" : "comment_char");
131 lr_ignore_rest (ldfile
, 0);
135 if (nowtok
== tok_escape_char
)
136 ldfile
->escape_char
= *arg
->val
.str
.startmb
;
138 ldfile
->comment_char
= *arg
->val
.str
.startmb
;
141 case tok_repertoiremap
:
142 /* We need an argument. */
143 arg
= lr_token (ldfile
, charmap
, NULL
, verbose
);
145 if (arg
->tok
!= tok_ident
)
147 SYNTAX_ERROR (_("bad argument"));
151 if (repertoire_name
== NULL
)
153 repertoire_name
= memcpy (xmalloc (arg
->val
.str
.lenmb
+ 1),
154 arg
->val
.str
.startmb
,
156 ((char *) repertoire_name
)[arg
->val
.str
.lenmb
] = '\0';
161 ctype_read (ldfile
, result
, charmap
, repertoire_name
,
162 (locale_mask
& CTYPE_LOCALE
) == 0);
163 result
->avail
|= locale_mask
& CTYPE_LOCALE
;
164 not_here
^= CTYPE_LOCALE
;
168 collate_read (ldfile
, result
, charmap
, repertoire_name
,
169 (locale_mask
& COLLATE_LOCALE
) == 0);
170 result
->avail
|= locale_mask
& COLLATE_LOCALE
;
171 not_here
^= COLLATE_LOCALE
;
174 case tok_lc_monetary
:
175 monetary_read (ldfile
, result
, charmap
, repertoire_name
,
176 (locale_mask
& MONETARY_LOCALE
) == 0);
177 result
->avail
|= locale_mask
& MONETARY_LOCALE
;
178 not_here
^= MONETARY_LOCALE
;
182 numeric_read (ldfile
, result
, charmap
, repertoire_name
,
183 (locale_mask
& NUMERIC_LOCALE
) == 0);
184 result
->avail
|= locale_mask
& NUMERIC_LOCALE
;
185 not_here
^= NUMERIC_LOCALE
;
189 time_read (ldfile
, result
, charmap
, repertoire_name
,
190 (locale_mask
& TIME_LOCALE
) == 0);
191 result
->avail
|= locale_mask
& TIME_LOCALE
;
192 not_here
^= TIME_LOCALE
;
195 case tok_lc_messages
:
196 messages_read (ldfile
, result
, charmap
, repertoire_name
,
197 (locale_mask
& MESSAGES_LOCALE
) == 0);
198 result
->avail
|= locale_mask
& MESSAGES_LOCALE
;
199 not_here
^= MESSAGES_LOCALE
;
203 paper_read (ldfile
, result
, charmap
, repertoire_name
,
204 (locale_mask
& PAPER_LOCALE
) == 0);
205 result
->avail
|= locale_mask
& PAPER_LOCALE
;
206 not_here
^= PAPER_LOCALE
;
210 name_read (ldfile
, result
, charmap
, repertoire_name
,
211 (locale_mask
& NAME_LOCALE
) == 0);
212 result
->avail
|= locale_mask
& NAME_LOCALE
;
213 not_here
^= NAME_LOCALE
;
217 address_read (ldfile
, result
, charmap
, repertoire_name
,
218 (locale_mask
& ADDRESS_LOCALE
) == 0);
219 result
->avail
|= locale_mask
& ADDRESS_LOCALE
;
220 not_here
^= ADDRESS_LOCALE
;
223 case tok_lc_telephone
:
224 telephone_read (ldfile
, result
, charmap
, repertoire_name
,
225 (locale_mask
& TELEPHONE_LOCALE
) == 0);
226 result
->avail
|= locale_mask
& TELEPHONE_LOCALE
;
227 not_here
^= TELEPHONE_LOCALE
;
230 case tok_lc_measurement
:
231 measurement_read (ldfile
, result
, charmap
, repertoire_name
,
232 (locale_mask
& MEASUREMENT_LOCALE
) == 0);
233 result
->avail
|= locale_mask
& MEASUREMENT_LOCALE
;
234 not_here
^= MEASUREMENT_LOCALE
;
237 case tok_lc_identification
:
238 identification_read (ldfile
, result
, charmap
, repertoire_name
,
239 (locale_mask
& IDENTIFICATION_LOCALE
) == 0);
240 result
->avail
|= locale_mask
& IDENTIFICATION_LOCALE
;
241 not_here
^= IDENTIFICATION_LOCALE
;
246 syntax error: not inside a locale definition section"));
250 /* The rest of the line must be empty. */
251 lr_ignore_rest (ldfile
, 1);
254 /* We read all of the file. */
257 /* Mark the categories which are not contained in the file. We assume
258 them to be available and the default data will be used. */
259 result
->avail
|= not_here
;
265 /* Semantic checking of locale specifications. */
267 static void (*const check_funcs
[]) (struct localedef_t
*,
268 struct charmap_t
*) =
270 [LC_CTYPE
] = ctype_finish
,
271 [LC_COLLATE
] = collate_finish
,
272 [LC_MESSAGES
] = messages_finish
,
273 [LC_MONETARY
] = monetary_finish
,
274 [LC_NUMERIC
] = numeric_finish
,
275 [LC_TIME
] = time_finish
,
276 [LC_PAPER
] = paper_finish
,
277 [LC_NAME
] = name_finish
,
278 [LC_ADDRESS
] = address_finish
,
279 [LC_TELEPHONE
] = telephone_finish
,
280 [LC_MEASUREMENT
] = measurement_finish
,
281 [LC_IDENTIFICATION
] = identification_finish
285 check_all_categories (struct localedef_t
*definitions
,
286 struct charmap_t
*charmap
)
290 for (cnt
= 0; cnt
< sizeof (check_funcs
) / sizeof (check_funcs
[0]); ++cnt
)
291 if (check_funcs
[cnt
] != NULL
)
292 check_funcs
[cnt
] (definitions
, charmap
);
296 /* Writing the locale data files. All files use the same output_path. */
298 static void (*const write_funcs
[]) (struct localedef_t
*, struct charmap_t
*,
301 [LC_CTYPE
] = ctype_output
,
302 [LC_COLLATE
] = collate_output
,
303 [LC_MESSAGES
] = messages_output
,
304 [LC_MONETARY
] = monetary_output
,
305 [LC_NUMERIC
] = numeric_output
,
306 [LC_TIME
] = time_output
,
307 [LC_PAPER
] = paper_output
,
308 [LC_NAME
] = name_output
,
309 [LC_ADDRESS
] = address_output
,
310 [LC_TELEPHONE
] = telephone_output
,
311 [LC_MEASUREMENT
] = measurement_output
,
312 [LC_IDENTIFICATION
] = identification_output
316 write_all_categories (struct localedef_t
*definitions
,
317 struct charmap_t
*charmap
,
318 const char *output_path
)
322 for (cnt
= 0; cnt
< sizeof (write_funcs
) / sizeof (write_funcs
[0]); ++cnt
)
323 if (write_funcs
[cnt
] != NULL
)
324 write_funcs
[cnt
] (definitions
, charmap
, output_path
);
327 /* Return a NULL terminated list of the directories next to output_path
328 that have the same owner, group, permissions and device as output_path. */
330 siblings_uncached (const char *output_path
)
334 struct stat output_stat
;
339 /* Remove trailing slashes and trailing pathname component. */
340 len
= strlen (output_path
);
341 base
= (char *) alloca (len
);
342 memcpy (base
, output_path
, len
);
344 while (p
> base
&& p
[-1] == '/')
350 while (p
> base
&& p
[-1] != '/');
356 /* Get the properties of output_path. */
357 if (lstat (output_path
, &output_stat
) < 0 || !S_ISDIR (output_stat
.st_mode
))
360 /* Iterate through the directories in base directory. */
361 dirp
= opendir (base
);
368 struct dirent
*other_dentry
;
369 const char *other_name
;
371 struct stat other_stat
;
373 other_dentry
= readdir (dirp
);
374 if (other_dentry
== NULL
)
377 other_name
= other_dentry
->d_name
;
378 if (strcmp (other_name
, ".") == 0 || strcmp (other_name
, "..") == 0)
381 other_path
= (char *) xmalloc (len
+ 1 + strlen (other_name
) + 2);
382 memcpy (other_path
, base
, len
);
383 other_path
[len
] = '/';
384 strcpy (other_path
+ len
+ 1, other_name
);
386 if (lstat (other_path
, &other_stat
) >= 0
387 && S_ISDIR (other_stat
.st_mode
)
388 && other_stat
.st_uid
== output_stat
.st_uid
389 && other_stat
.st_gid
== output_stat
.st_gid
390 && other_stat
.st_mode
== output_stat
.st_mode
391 && other_stat
.st_dev
== output_stat
.st_dev
)
393 /* Found a subdirectory. Add a trailing slash and store it. */
394 p
= other_path
+ len
+ 1 + strlen (other_name
);
397 elems
= (const char **) xrealloc ((char *) elems
,
398 (nelems
+ 2) * sizeof (char **));
399 elems
[nelems
++] = other_path
;
407 elems
[nelems
] = NULL
;
411 /* Return a NULL terminated list of the directories next to output_path
412 that have the same owner, group, permissions and device as output_path.
413 Cache the result for future calls. */
415 siblings (const char *output_path
)
417 static const char *last_output_path
;
418 static const char **last_result
;
420 if (output_path
!= last_output_path
)
422 if (last_result
!= NULL
)
426 for (p
= last_result
; *p
!= NULL
; p
++)
431 last_output_path
= output_path
;
432 last_result
= siblings_uncached (output_path
);
437 /* Read as many bytes from a file descriptor as possible. */
439 full_read (int fd
, void *bufarea
, size_t nbyte
)
441 char *buf
= (char *) bufarea
;
445 ssize_t retval
= read (fd
, buf
, nbyte
);
454 else if (errno
!= EINTR
)
457 return buf
- (char *) bufarea
;
460 /* Compare the contents of two regular files of the same size. Return 0
461 if they are equal, 1 if they are different, or -1 if an error occurs. */
463 compare_files (const char *filename1
, const char *filename2
, size_t size
,
469 fd1
= open (filename1
, O_RDONLY
);
472 fd2
= open (filename2
, O_RDONLY
);
475 char *buf1
= (char *) xmalloc (2 * blocksize
);
476 char *buf2
= buf1
+ blocksize
;
481 size_t bytes
= (size
< blocksize
? size
: blocksize
);
483 if (full_read (fd1
, buf1
, bytes
) < (ssize_t
) bytes
)
488 if (full_read (fd2
, buf2
, bytes
) < (ssize_t
) bytes
)
493 if (memcmp (buf1
, buf2
, bytes
) != 0)
509 /* Write a locale file, with contents given by N_ELEM and VEC. */
511 write_locale_data (const char *output_path
, const char *category
,
512 size_t n_elem
, struct iovec
*vec
)
514 size_t cnt
, step
, maxiov
;
517 const char **other_paths
;
519 fname
= xmalloc (strlen (output_path
) + 2 * strlen (category
) + 7);
521 /* Normally we write to the directory pointed to by the OUTPUT_PATH.
522 But for LC_MESSAGES we have to take care for the translation
523 data. This means we need to have a directory LC_MESSAGES in
524 which we place the file under the name SYS_LC_MESSAGES. */
525 sprintf (fname
, "%s%s", output_path
, category
);
527 if (strcmp (category
, "LC_MESSAGES") == 0)
531 if (stat (fname
, &st
) < 0)
533 if (mkdir (fname
, 0777) >= 0)
539 else if (!S_ISREG (st
.st_mode
))
546 /* Create the locale file with nlinks == 1; this avoids crashing processes
547 which currently use the locale and damaging files belonging to other
552 fd
= creat (fname
, 0666);
557 int save_err
= errno
;
561 sprintf (fname
, "%1$s%2$s/SYS_%2$s", output_path
, category
);
563 fd
= creat (fname
, 0666);
571 error (0, save_err
, _("\
572 cannot open output file `%s' for category `%s'"),
582 maxiov
= sysconf (_SC_UIO_MAXIOV
);
585 /* Write the data using writev. But we must take care for the
586 limitation of the implementation. */
587 for (cnt
= 0; cnt
< n_elem
; cnt
+= step
)
591 step
= MIN (maxiov
, step
);
593 if (writev (fd
, &vec
[cnt
], step
) < 0)
596 error (0, errno
, _("failure while writing data for category `%s'"),
604 /* Compare the file with the locale data files for the same category in
605 other locales, and see if we can reuse it, to save disk space. */
606 other_paths
= siblings (output_path
);
607 if (other_paths
!= NULL
)
609 struct stat fname_stat
;
611 if (lstat (fname
, &fname_stat
) >= 0
612 && S_ISREG (fname_stat
.st_mode
))
614 const char *fname_tail
= fname
+ strlen (output_path
);
615 const char **other_p
;
620 for (other_p
= other_paths
; *other_p
; other_p
++)
622 seen_inodes
= (ino_t
*) xmalloc (seen_count
* sizeof (ino_t
));
625 for (other_p
= other_paths
; *other_p
; other_p
++)
627 const char *other_path
= *other_p
;
628 size_t other_path_len
= strlen (other_path
);
630 struct stat other_fname_stat
;
633 (char *) xmalloc (other_path_len
+ strlen (fname_tail
) + 1);
634 memcpy (other_fname
, other_path
, other_path_len
);
635 strcpy (other_fname
+ other_path_len
, fname_tail
);
637 if (lstat (other_fname
, &other_fname_stat
) >= 0
638 && S_ISREG (other_fname_stat
.st_mode
)
639 /* Consider only files on the same device.
640 Otherwise hard linking won't work anyway. */
641 && other_fname_stat
.st_dev
== fname_stat
.st_dev
642 /* Consider only files with the same permissions.
643 Otherwise there are security risks. */
644 && other_fname_stat
.st_uid
== fname_stat
.st_uid
645 && other_fname_stat
.st_gid
== fname_stat
.st_gid
646 && other_fname_stat
.st_mode
== fname_stat
.st_mode
647 /* Don't compare fname with itself. */
648 && other_fname_stat
.st_ino
!= fname_stat
.st_ino
649 /* Files must have the same size, otherwise they
650 cannot be the same. */
651 && other_fname_stat
.st_size
== fname_stat
.st_size
)
653 /* Skip this file if we have already read it (under a
657 for (i
= seen_count
- 1; i
>= 0; i
--)
658 if (seen_inodes
[i
] == other_fname_stat
.st_ino
)
662 /* Now compare fname and other_fname for real. */
665 #ifdef _STATBUF_ST_BLKSIZE
666 blocksize
= MAX (fname_stat
.st_blksize
,
667 other_fname_stat
.st_blksize
);
668 if (blocksize
> 8 * 1024)
669 blocksize
= 8 * 1024;
671 blocksize
= 8 * 1024;
674 if (compare_files (fname
, other_fname
,
675 fname_stat
.st_size
, blocksize
) == 0)
677 /* Found! other_fname is identical to fname. */
678 /* Link other_fname to fname. But use a temporary
679 file, in case hard links don't work on the
680 particular filesystem. */
682 (char *) xmalloc (strlen (fname
) + 4 + 1);
684 strcpy (tmp_fname
, fname
);
685 strcat (tmp_fname
, ".tmp");
687 if (link (other_fname
, tmp_fname
) >= 0)
690 if (rename (tmp_fname
, fname
) < 0)
693 error (0, errno
, _("\
694 cannot create output file `%s' for category `%s'"),
704 /* Don't compare with this file a second time. */
705 seen_inodes
[seen_count
++] = other_fname_stat
.st_ino
;