1 /* Copyright (C) 1996-2004, 2005, 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1996.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #include <sys/param.h>
31 #include "../../crypt/md5.h"
32 #include "localedef.h"
34 #include "simple-hash.h"
36 #include "locfile-kw.h"
39 /* Temporary storage of the locale data before writing it to the archive. */
40 static locale_data_t to_archive
;
44 locfile_read (struct localedef_t
*result
, const struct charmap_t
*charmap
)
46 const char *filename
= result
->name
;
47 const char *repertoire_name
= result
->repertoire_name
;
48 int locale_mask
= result
->needed
& ~result
->avail
;
49 struct linereader
*ldfile
;
50 int not_here
= ALL_LOCALES
;
52 /* If no repertoire name was specified use the global one. */
53 if (repertoire_name
== NULL
)
54 repertoire_name
= repertoire_global
;
56 /* Open the locale definition file. */
57 ldfile
= lr_open (filename
, locfile_hash
);
60 if (filename
!= NULL
&& filename
[0] != '/')
62 char *i18npath
= getenv ("I18NPATH");
63 if (i18npath
!= NULL
&& *i18npath
!= '\0')
65 const size_t pathlen
= strlen (i18npath
);
66 char i18npathbuf
[pathlen
+ 1];
67 char path
[strlen (filename
) + 1 + pathlen
68 + sizeof ("/locales/") - 1];
70 i18npath
= memcpy (i18npathbuf
, i18npath
, pathlen
+ 1);
73 && (next
= strsep (&i18npath
, ":")) != NULL
)
75 stpcpy (stpcpy (stpcpy (path
, next
), "/locales/"), filename
);
77 ldfile
= lr_open (path
, locfile_hash
);
81 stpcpy (stpcpy (path
, next
), filename
);
83 ldfile
= lr_open (path
, locfile_hash
);
88 /* Test in the default directory. */
91 char path
[strlen (filename
) + 1 + sizeof (LOCSRCDIR
)];
93 stpcpy (stpcpy (stpcpy (path
, LOCSRCDIR
), "/"), filename
);
94 ldfile
= lr_open (path
, locfile_hash
);
102 /* Parse locale definition file and store result in RESULT. */
105 struct token
*now
= lr_token (ldfile
, charmap
, NULL
, NULL
, verbose
);
106 enum token_t nowtok
= now
->tok
;
109 if (nowtok
== tok_eof
)
112 if (nowtok
== tok_eol
)
113 /* Ignore empty lines. */
118 case tok_escape_char
:
119 case tok_comment_char
:
120 /* We need an argument. */
121 arg
= lr_token (ldfile
, charmap
, NULL
, NULL
, verbose
);
123 if (arg
->tok
!= tok_ident
)
125 SYNTAX_ERROR (_("bad argument"));
129 if (arg
->val
.str
.lenmb
!= 1)
131 lr_error (ldfile
, _("\
132 argument to `%s' must be a single character"),
133 nowtok
== tok_escape_char
134 ? "escape_char" : "comment_char");
136 lr_ignore_rest (ldfile
, 0);
140 if (nowtok
== tok_escape_char
)
141 ldfile
->escape_char
= *arg
->val
.str
.startmb
;
143 ldfile
->comment_char
= *arg
->val
.str
.startmb
;
146 case tok_repertoiremap
:
147 /* We need an argument. */
148 arg
= lr_token (ldfile
, charmap
, NULL
, NULL
, verbose
);
150 if (arg
->tok
!= tok_ident
)
152 SYNTAX_ERROR (_("bad argument"));
156 if (repertoire_name
== NULL
)
158 char *newp
= alloca (arg
->val
.str
.lenmb
+ 1);
160 *((char *) mempcpy (newp
, arg
->val
.str
.startmb
,
161 arg
->val
.str
.lenmb
)) = '\0';
162 repertoire_name
= newp
;
167 ctype_read (ldfile
, result
, charmap
, repertoire_name
,
168 (locale_mask
& CTYPE_LOCALE
) == 0);
169 result
->avail
|= locale_mask
& CTYPE_LOCALE
;
170 not_here
^= CTYPE_LOCALE
;
174 collate_read (ldfile
, result
, charmap
, repertoire_name
,
175 (locale_mask
& COLLATE_LOCALE
) == 0);
176 result
->avail
|= locale_mask
& COLLATE_LOCALE
;
177 not_here
^= COLLATE_LOCALE
;
180 case tok_lc_monetary
:
181 monetary_read (ldfile
, result
, charmap
, repertoire_name
,
182 (locale_mask
& MONETARY_LOCALE
) == 0);
183 result
->avail
|= locale_mask
& MONETARY_LOCALE
;
184 not_here
^= MONETARY_LOCALE
;
188 numeric_read (ldfile
, result
, charmap
, repertoire_name
,
189 (locale_mask
& NUMERIC_LOCALE
) == 0);
190 result
->avail
|= locale_mask
& NUMERIC_LOCALE
;
191 not_here
^= NUMERIC_LOCALE
;
195 time_read (ldfile
, result
, charmap
, repertoire_name
,
196 (locale_mask
& TIME_LOCALE
) == 0);
197 result
->avail
|= locale_mask
& TIME_LOCALE
;
198 not_here
^= TIME_LOCALE
;
201 case tok_lc_messages
:
202 messages_read (ldfile
, result
, charmap
, repertoire_name
,
203 (locale_mask
& MESSAGES_LOCALE
) == 0);
204 result
->avail
|= locale_mask
& MESSAGES_LOCALE
;
205 not_here
^= MESSAGES_LOCALE
;
209 paper_read (ldfile
, result
, charmap
, repertoire_name
,
210 (locale_mask
& PAPER_LOCALE
) == 0);
211 result
->avail
|= locale_mask
& PAPER_LOCALE
;
212 not_here
^= PAPER_LOCALE
;
216 name_read (ldfile
, result
, charmap
, repertoire_name
,
217 (locale_mask
& NAME_LOCALE
) == 0);
218 result
->avail
|= locale_mask
& NAME_LOCALE
;
219 not_here
^= NAME_LOCALE
;
223 address_read (ldfile
, result
, charmap
, repertoire_name
,
224 (locale_mask
& ADDRESS_LOCALE
) == 0);
225 result
->avail
|= locale_mask
& ADDRESS_LOCALE
;
226 not_here
^= ADDRESS_LOCALE
;
229 case tok_lc_telephone
:
230 telephone_read (ldfile
, result
, charmap
, repertoire_name
,
231 (locale_mask
& TELEPHONE_LOCALE
) == 0);
232 result
->avail
|= locale_mask
& TELEPHONE_LOCALE
;
233 not_here
^= TELEPHONE_LOCALE
;
236 case tok_lc_measurement
:
237 measurement_read (ldfile
, result
, charmap
, repertoire_name
,
238 (locale_mask
& MEASUREMENT_LOCALE
) == 0);
239 result
->avail
|= locale_mask
& MEASUREMENT_LOCALE
;
240 not_here
^= MEASUREMENT_LOCALE
;
243 case tok_lc_identification
:
244 identification_read (ldfile
, result
, charmap
, repertoire_name
,
245 (locale_mask
& IDENTIFICATION_LOCALE
) == 0);
246 result
->avail
|= locale_mask
& IDENTIFICATION_LOCALE
;
247 not_here
^= IDENTIFICATION_LOCALE
;
252 syntax error: not inside a locale definition section"));
256 /* The rest of the line must be empty. */
257 lr_ignore_rest (ldfile
, 1);
260 /* We read all of the file. */
263 /* Mark the categories which are not contained in the file. We assume
264 them to be available and the default data will be used. */
265 result
->avail
|= not_here
;
271 /* Semantic checking of locale specifications. */
273 static void (*const check_funcs
[]) (struct localedef_t
*,
274 const struct charmap_t
*) =
276 [LC_CTYPE
] = ctype_finish
,
277 [LC_COLLATE
] = collate_finish
,
278 [LC_MESSAGES
] = messages_finish
,
279 [LC_MONETARY
] = monetary_finish
,
280 [LC_NUMERIC
] = numeric_finish
,
281 [LC_TIME
] = time_finish
,
282 [LC_PAPER
] = paper_finish
,
283 [LC_NAME
] = name_finish
,
284 [LC_ADDRESS
] = address_finish
,
285 [LC_TELEPHONE
] = telephone_finish
,
286 [LC_MEASUREMENT
] = measurement_finish
,
287 [LC_IDENTIFICATION
] = identification_finish
291 check_all_categories (struct localedef_t
*definitions
,
292 const struct charmap_t
*charmap
)
296 for (cnt
= 0; cnt
< sizeof (check_funcs
) / sizeof (check_funcs
[0]); ++cnt
)
297 if (check_funcs
[cnt
] != NULL
)
298 check_funcs
[cnt
] (definitions
, charmap
);
302 /* Writing the locale data files. All files use the same output_path. */
304 static void (*const write_funcs
[]) (struct localedef_t
*,
305 const struct charmap_t
*, const char *) =
307 [LC_CTYPE
] = ctype_output
,
308 [LC_COLLATE
] = collate_output
,
309 [LC_MESSAGES
] = messages_output
,
310 [LC_MONETARY
] = monetary_output
,
311 [LC_NUMERIC
] = numeric_output
,
312 [LC_TIME
] = time_output
,
313 [LC_PAPER
] = paper_output
,
314 [LC_NAME
] = name_output
,
315 [LC_ADDRESS
] = address_output
,
316 [LC_TELEPHONE
] = telephone_output
,
317 [LC_MEASUREMENT
] = measurement_output
,
318 [LC_IDENTIFICATION
] = identification_output
323 write_all_categories (struct localedef_t
*definitions
,
324 const struct charmap_t
*charmap
, const char *locname
,
325 const char *output_path
)
329 for (cnt
= 0; cnt
< sizeof (write_funcs
) / sizeof (write_funcs
[0]); ++cnt
)
330 if (write_funcs
[cnt
] != NULL
)
331 write_funcs
[cnt
] (definitions
, charmap
, output_path
);
335 /* The data has to be added to the archive. Do this now. */
336 struct locarhandle ah
;
338 /* Open the archive. This call never returns if we cannot
339 successfully open the archive. */
340 open_archive (&ah
, false);
342 if (add_locale_to_archive (&ah
, locname
, to_archive
, true) != 0)
343 error (EXIT_FAILURE
, errno
, _("cannot add to locale archive"));
351 /* Return a NULL terminated list of the directories next to output_path
352 that have the same owner, group, permissions and device as output_path. */
354 siblings_uncached (const char *output_path
)
358 struct stat output_stat
;
363 /* Remove trailing slashes and trailing pathname component. */
364 len
= strlen (output_path
);
365 base
= (char *) alloca (len
);
366 memcpy (base
, output_path
, len
);
368 while (p
> base
&& p
[-1] == '/')
374 while (p
> base
&& p
[-1] != '/');
380 /* Get the properties of output_path. */
381 if (lstat (output_path
, &output_stat
) < 0 || !S_ISDIR (output_stat
.st_mode
))
384 /* Iterate through the directories in base directory. */
385 dirp
= opendir (base
);
392 struct dirent64
*other_dentry
;
393 const char *other_name
;
395 struct stat other_stat
;
397 other_dentry
= readdir64 (dirp
);
398 if (other_dentry
== NULL
)
401 other_name
= other_dentry
->d_name
;
402 if (strcmp (other_name
, ".") == 0 || strcmp (other_name
, "..") == 0)
405 other_path
= (char *) xmalloc (len
+ 1 + strlen (other_name
) + 2);
406 memcpy (other_path
, base
, len
);
407 other_path
[len
] = '/';
408 strcpy (other_path
+ len
+ 1, other_name
);
410 if (lstat (other_path
, &other_stat
) >= 0
411 && S_ISDIR (other_stat
.st_mode
)
412 && other_stat
.st_uid
== output_stat
.st_uid
413 && other_stat
.st_gid
== output_stat
.st_gid
414 && other_stat
.st_mode
== output_stat
.st_mode
415 && other_stat
.st_dev
== output_stat
.st_dev
)
417 /* Found a subdirectory. Add a trailing slash and store it. */
418 p
= other_path
+ len
+ 1 + strlen (other_name
);
421 elems
= (const char **) xrealloc ((char *) elems
,
422 (nelems
+ 2) * sizeof (char **));
423 elems
[nelems
++] = other_path
;
431 elems
[nelems
] = NULL
;
436 /* Return a NULL terminated list of the directories next to output_path
437 that have the same owner, group, permissions and device as output_path.
438 Cache the result for future calls. */
440 siblings (const char *output_path
)
442 static const char *last_output_path
;
443 static const char **last_result
;
445 if (output_path
!= last_output_path
)
447 if (last_result
!= NULL
)
451 for (p
= last_result
; *p
!= NULL
; p
++)
456 last_output_path
= output_path
;
457 last_result
= siblings_uncached (output_path
);
463 /* Read as many bytes from a file descriptor as possible. */
465 full_read (int fd
, void *bufarea
, size_t nbyte
)
467 char *buf
= (char *) bufarea
;
471 ssize_t retval
= read (fd
, buf
, nbyte
);
480 else if (errno
!= EINTR
)
483 return buf
- (char *) bufarea
;
487 /* Compare the contents of two regular files of the same size. Return 0
488 if they are equal, 1 if they are different, or -1 if an error occurs. */
490 compare_files (const char *filename1
, const char *filename2
, size_t size
,
496 fd1
= open (filename1
, O_RDONLY
);
499 fd2
= open (filename2
, O_RDONLY
);
502 char *buf1
= (char *) xmalloc (2 * blocksize
);
503 char *buf2
= buf1
+ blocksize
;
508 size_t bytes
= (size
< blocksize
? size
: blocksize
);
510 if (full_read (fd1
, buf1
, bytes
) < (ssize_t
) bytes
)
515 if (full_read (fd2
, buf2
, bytes
) < (ssize_t
) bytes
)
520 if (memcmp (buf1
, buf2
, bytes
) != 0)
537 /* Write a locale file, with contents given by N_ELEM and VEC. */
539 write_locale_data (const char *output_path
, int catidx
, const char *category
,
540 size_t n_elem
, struct iovec
*vec
)
542 size_t cnt
, step
, maxiov
;
545 const char **other_paths
;
549 /* The data will be added to the archive. For now we simply
550 generate the image which will be written. First determine
555 to_archive
[catidx
].size
= 0;
556 for (cnt
= 0; cnt
< n_elem
; ++cnt
)
557 to_archive
[catidx
].size
+= vec
[cnt
].iov_len
;
559 /* Allocate the memory for it. */
560 to_archive
[catidx
].addr
= xmalloc (to_archive
[catidx
].size
);
563 for (cnt
= 0, endp
= to_archive
[catidx
].addr
; cnt
< n_elem
; ++cnt
)
564 endp
= mempcpy (endp
, vec
[cnt
].iov_base
, vec
[cnt
].iov_len
);
566 /* Compute the MD5 sum for the data. */
567 __md5_buffer (to_archive
[catidx
].addr
, to_archive
[catidx
].size
,
568 to_archive
[catidx
].sum
);
573 fname
= xmalloc (strlen (output_path
) + 2 * strlen (category
) + 7);
575 /* Normally we write to the directory pointed to by the OUTPUT_PATH.
576 But for LC_MESSAGES we have to take care for the translation
577 data. This means we need to have a directory LC_MESSAGES in
578 which we place the file under the name SYS_LC_MESSAGES. */
579 sprintf (fname
, "%s%s", output_path
, category
);
581 if (strcmp (category
, "LC_MESSAGES") == 0)
585 if (stat (fname
, &st
) < 0)
587 if (mkdir (fname
, 0777) >= 0)
593 else if (!S_ISREG (st
.st_mode
))
600 /* Create the locale file with nlinks == 1; this avoids crashing processes
601 which currently use the locale and damaging files belonging to other
606 fd
= creat (fname
, 0666);
611 int save_err
= errno
;
615 sprintf (fname
, "%1$s%2$s/SYS_%2$s", output_path
, category
);
617 fd
= creat (fname
, 0666);
625 WITH_CUR_LOCALE (error (0, save_err
, _("\
626 cannot open output file `%s' for category `%s'"), fname
, category
));
635 maxiov
= sysconf (_SC_UIO_MAXIOV
);
638 /* Write the data using writev. But we must take care for the
639 limitation of the implementation. */
640 for (cnt
= 0; cnt
< n_elem
; cnt
+= step
)
644 step
= MIN (maxiov
, step
);
646 if (writev (fd
, &vec
[cnt
], step
) < 0)
649 WITH_CUR_LOCALE (error (0, errno
, _("\
650 failure while writing data for category `%s'"), category
));
657 /* Compare the file with the locale data files for the same category in
658 other locales, and see if we can reuse it, to save disk space. */
659 other_paths
= siblings (output_path
);
660 if (other_paths
!= NULL
)
662 struct stat fname_stat
;
664 if (lstat (fname
, &fname_stat
) >= 0
665 && S_ISREG (fname_stat
.st_mode
))
667 const char *fname_tail
= fname
+ strlen (output_path
);
668 const char **other_p
;
673 for (other_p
= other_paths
; *other_p
; other_p
++)
675 seen_inodes
= (ino_t
*) xmalloc (seen_count
* sizeof (ino_t
));
678 for (other_p
= other_paths
; *other_p
; other_p
++)
680 const char *other_path
= *other_p
;
681 size_t other_path_len
= strlen (other_path
);
683 struct stat other_fname_stat
;
686 (char *) xmalloc (other_path_len
+ strlen (fname_tail
) + 1);
687 memcpy (other_fname
, other_path
, other_path_len
);
688 strcpy (other_fname
+ other_path_len
, fname_tail
);
690 if (lstat (other_fname
, &other_fname_stat
) >= 0
691 && S_ISREG (other_fname_stat
.st_mode
)
692 /* Consider only files on the same device.
693 Otherwise hard linking won't work anyway. */
694 && other_fname_stat
.st_dev
== fname_stat
.st_dev
695 /* Consider only files with the same permissions.
696 Otherwise there are security risks. */
697 && other_fname_stat
.st_uid
== fname_stat
.st_uid
698 && other_fname_stat
.st_gid
== fname_stat
.st_gid
699 && other_fname_stat
.st_mode
== fname_stat
.st_mode
700 /* Don't compare fname with itself. */
701 && other_fname_stat
.st_ino
!= fname_stat
.st_ino
702 /* Files must have the same size, otherwise they
703 cannot be the same. */
704 && other_fname_stat
.st_size
== fname_stat
.st_size
)
706 /* Skip this file if we have already read it (under a
710 for (i
= seen_count
- 1; i
>= 0; i
--)
711 if (seen_inodes
[i
] == other_fname_stat
.st_ino
)
715 /* Now compare fname and other_fname for real. */
718 #ifdef _STATBUF_ST_BLKSIZE
719 blocksize
= MAX (fname_stat
.st_blksize
,
720 other_fname_stat
.st_blksize
);
721 if (blocksize
> 8 * 1024)
722 blocksize
= 8 * 1024;
724 blocksize
= 8 * 1024;
727 if (compare_files (fname
, other_fname
,
728 fname_stat
.st_size
, blocksize
) == 0)
730 /* Found! other_fname is identical to fname. */
731 /* Link other_fname to fname. But use a temporary
732 file, in case hard links don't work on the
733 particular filesystem. */
735 (char *) xmalloc (strlen (fname
) + 4 + 1);
737 strcpy (stpcpy (tmp_fname
, fname
), ".tmp");
739 if (link (other_fname
, tmp_fname
) >= 0)
742 if (rename (tmp_fname
, fname
) < 0)
745 WITH_CUR_LOCALE (error (0, errno
, _("\
746 cannot create output file `%s' for category `%s'"), fname
, category
));
755 /* Don't compare with this file a second time. */
756 seen_inodes
[seen_count
++] = other_fname_stat
.st_ino
;
769 /* General handling of `copy'. */
771 handle_copy (struct linereader
*ldfile
, const struct charmap_t
*charmap
,
772 const char *repertoire_name
, struct localedef_t
*result
,
773 enum token_t token
, int locale
, const char *locale_name
,
779 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
780 if (now
->tok
!= tok_string
)
781 lr_error (ldfile
, _("expect string argument for `copy'"));
782 else if (!ignore_content
)
784 if (now
->val
.str
.startmb
== NULL
)
785 lr_error (ldfile
, _("\
786 locale name should consist only of portable characters"));
789 (void) add_to_readlist (locale
, now
->val
.str
.startmb
,
790 repertoire_name
, 1, NULL
);
791 result
->copy_name
[locale
] = now
->val
.str
.startmb
;
795 lr_ignore_rest (ldfile
, now
->tok
== tok_string
);
797 /* The rest of the line must be empty and the next keyword must be
799 while ((now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
))->tok
800 != tok_end
&& now
->tok
!= tok_eof
)
804 lr_error (ldfile
, _("\
805 no other keyword shall be specified when `copy' is used"));
809 lr_ignore_rest (ldfile
, 0);
812 if (now
->tok
!= tok_eof
)
814 /* Handle `END xxx'. */
815 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
817 if (now
->tok
!= token
)
818 lr_error (ldfile
, _("\
819 `%1$s' definition does not end with `END %1$s'"), locale_name
);
821 lr_ignore_rest (ldfile
, now
->tok
== token
);
824 /* When we come here we reached the end of the file. */
825 lr_error (ldfile
, _("%s: premature end of file"), locale_name
);