1 /* Copyright (C) 1996-2004, 2005 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 repertoire_name = memcpy (xmalloc (arg->val.str.lenmb + 1),
159 arg->val.str.startmb,
161 ((char *) repertoire_name)[arg->val.str.lenmb] = '\0';
166 ctype_read (ldfile, result, charmap, repertoire_name,
167 (locale_mask & CTYPE_LOCALE) == 0);
168 result->avail |= locale_mask & CTYPE_LOCALE;
169 not_here ^= CTYPE_LOCALE;
173 collate_read (ldfile, result, charmap, repertoire_name,
174 (locale_mask & COLLATE_LOCALE) == 0);
175 result->avail |= locale_mask & COLLATE_LOCALE;
176 not_here ^= COLLATE_LOCALE;
179 case tok_lc_monetary:
180 monetary_read (ldfile, result, charmap, repertoire_name,
181 (locale_mask & MONETARY_LOCALE) == 0);
182 result->avail |= locale_mask & MONETARY_LOCALE;
183 not_here ^= MONETARY_LOCALE;
187 numeric_read (ldfile, result, charmap, repertoire_name,
188 (locale_mask & NUMERIC_LOCALE) == 0);
189 result->avail |= locale_mask & NUMERIC_LOCALE;
190 not_here ^= NUMERIC_LOCALE;
194 time_read (ldfile, result, charmap, repertoire_name,
195 (locale_mask & TIME_LOCALE) == 0);
196 result->avail |= locale_mask & TIME_LOCALE;
197 not_here ^= TIME_LOCALE;
200 case tok_lc_messages:
201 messages_read (ldfile, result, charmap, repertoire_name,
202 (locale_mask & MESSAGES_LOCALE) == 0);
203 result->avail |= locale_mask & MESSAGES_LOCALE;
204 not_here ^= MESSAGES_LOCALE;
208 paper_read (ldfile, result, charmap, repertoire_name,
209 (locale_mask & PAPER_LOCALE) == 0);
210 result->avail |= locale_mask & PAPER_LOCALE;
211 not_here ^= PAPER_LOCALE;
215 name_read (ldfile, result, charmap, repertoire_name,
216 (locale_mask & NAME_LOCALE) == 0);
217 result->avail |= locale_mask & NAME_LOCALE;
218 not_here ^= NAME_LOCALE;
222 address_read (ldfile, result, charmap, repertoire_name,
223 (locale_mask & ADDRESS_LOCALE) == 0);
224 result->avail |= locale_mask & ADDRESS_LOCALE;
225 not_here ^= ADDRESS_LOCALE;
228 case tok_lc_telephone:
229 telephone_read (ldfile, result, charmap, repertoire_name,
230 (locale_mask & TELEPHONE_LOCALE) == 0);
231 result->avail |= locale_mask & TELEPHONE_LOCALE;
232 not_here ^= TELEPHONE_LOCALE;
235 case tok_lc_measurement:
236 measurement_read (ldfile, result, charmap, repertoire_name,
237 (locale_mask & MEASUREMENT_LOCALE) == 0);
238 result->avail |= locale_mask & MEASUREMENT_LOCALE;
239 not_here ^= MEASUREMENT_LOCALE;
242 case tok_lc_identification:
243 identification_read (ldfile, result, charmap, repertoire_name,
244 (locale_mask & IDENTIFICATION_LOCALE) == 0);
245 result->avail |= locale_mask & IDENTIFICATION_LOCALE;
246 not_here ^= IDENTIFICATION_LOCALE;
251 syntax error: not inside a locale definition section"));
255 /* The rest of the line must be empty. */
256 lr_ignore_rest (ldfile, 1);
259 /* We read all of the file. */
262 /* Mark the categories which are not contained in the file. We assume
263 them to be available and the default data will be used. */
264 result->avail |= not_here;
270 /* Semantic checking of locale specifications. */
272 static void (*const check_funcs[]) (struct localedef_t *,
273 const struct charmap_t *) =
275 [LC_CTYPE] = ctype_finish,
276 [LC_COLLATE] = collate_finish,
277 [LC_MESSAGES] = messages_finish,
278 [LC_MONETARY] = monetary_finish,
279 [LC_NUMERIC] = numeric_finish,
280 [LC_TIME] = time_finish,
281 [LC_PAPER] = paper_finish,
282 [LC_NAME] = name_finish,
283 [LC_ADDRESS] = address_finish,
284 [LC_TELEPHONE] = telephone_finish,
285 [LC_MEASUREMENT] = measurement_finish,
286 [LC_IDENTIFICATION] = identification_finish
290 check_all_categories (struct localedef_t *definitions,
291 const struct charmap_t *charmap)
295 for (cnt = 0; cnt < sizeof (check_funcs) / sizeof (check_funcs[0]); ++cnt)
296 if (check_funcs[cnt] != NULL)
297 check_funcs[cnt] (definitions, charmap);
301 /* Writing the locale data files. All files use the same output_path. */
303 static void (*const write_funcs[]) (struct localedef_t *,
304 const struct charmap_t *, const char *) =
306 [LC_CTYPE] = ctype_output,
307 [LC_COLLATE] = collate_output,
308 [LC_MESSAGES] = messages_output,
309 [LC_MONETARY] = monetary_output,
310 [LC_NUMERIC] = numeric_output,
311 [LC_TIME] = time_output,
312 [LC_PAPER] = paper_output,
313 [LC_NAME] = name_output,
314 [LC_ADDRESS] = address_output,
315 [LC_TELEPHONE] = telephone_output,
316 [LC_MEASUREMENT] = measurement_output,
317 [LC_IDENTIFICATION] = identification_output
322 write_all_categories (struct localedef_t *definitions,
323 const struct charmap_t *charmap, const char *locname,
324 const char *output_path)
328 for (cnt = 0; cnt < sizeof (write_funcs) / sizeof (write_funcs[0]); ++cnt)
329 if (write_funcs[cnt] != NULL)
330 write_funcs[cnt] (definitions, charmap, output_path);
334 /* The data has to be added to the archive. Do this now. */
335 struct locarhandle ah;
337 /* Open the archive. This call never returns if we cannot
338 successfully open the archive. */
339 open_archive (&ah, false);
341 if (add_locale_to_archive (&ah, locname, to_archive, true) != 0)
342 error (EXIT_FAILURE, errno, _("cannot add to locale archive"));
350 /* Return a NULL terminated list of the directories next to output_path
351 that have the same owner, group, permissions and device as output_path. */
353 siblings_uncached (const char *output_path)
357 struct stat output_stat;
362 /* Remove trailing slashes and trailing pathname component. */
363 len = strlen (output_path);
364 base = (char *) alloca (len);
365 memcpy (base, output_path, len);
367 while (p > base && p[-1] == '/')
373 while (p > base && p[-1] != '/');
379 /* Get the properties of output_path. */
380 if (lstat (output_path, &output_stat) < 0 || !S_ISDIR (output_stat.st_mode))
383 /* Iterate through the directories in base directory. */
384 dirp = opendir (base);
391 struct dirent64 *other_dentry;
392 const char *other_name;
394 struct stat other_stat;
396 other_dentry = readdir64 (dirp);
397 if (other_dentry == NULL)
400 other_name = other_dentry->d_name;
401 if (strcmp (other_name, ".") == 0 || strcmp (other_name, "..") == 0)
404 other_path = (char *) xmalloc (len + 1 + strlen (other_name) + 2);
405 memcpy (other_path, base, len);
406 other_path[len] = '/';
407 strcpy (other_path + len + 1, other_name);
409 if (lstat (other_path, &other_stat) >= 0
410 && S_ISDIR (other_stat.st_mode)
411 && other_stat.st_uid == output_stat.st_uid
412 && other_stat.st_gid == output_stat.st_gid
413 && other_stat.st_mode == output_stat.st_mode
414 && other_stat.st_dev == output_stat.st_dev)
416 /* Found a subdirectory. Add a trailing slash and store it. */
417 p = other_path + len + 1 + strlen (other_name);
420 elems = (const char **) xrealloc ((char *) elems,
421 (nelems + 2) * sizeof (char **));
422 elems[nelems++] = other_path;
430 elems[nelems] = NULL;
435 /* Return a NULL terminated list of the directories next to output_path
436 that have the same owner, group, permissions and device as output_path.
437 Cache the result for future calls. */
439 siblings (const char *output_path)
441 static const char *last_output_path;
442 static const char **last_result;
444 if (output_path != last_output_path)
446 if (last_result != NULL)
450 for (p = last_result; *p != NULL; p++)
455 last_output_path = output_path;
456 last_result = siblings_uncached (output_path);
462 /* Read as many bytes from a file descriptor as possible. */
464 full_read (int fd, void *bufarea, size_t nbyte)
466 char *buf = (char *) bufarea;
470 ssize_t retval = read (fd, buf, nbyte);
479 else if (errno != EINTR)
482 return buf - (char *) bufarea;
486 /* Compare the contents of two regular files of the same size. Return 0
487 if they are equal, 1 if they are different, or -1 if an error occurs. */
489 compare_files (const char *filename1, const char *filename2, size_t size,
495 fd1 = open (filename1, O_RDONLY);
498 fd2 = open (filename2, O_RDONLY);
501 char *buf1 = (char *) xmalloc (2 * blocksize);
502 char *buf2 = buf1 + blocksize;
507 size_t bytes = (size < blocksize ? size : blocksize);
509 if (full_read (fd1, buf1, bytes) < (ssize_t) bytes)
514 if (full_read (fd2, buf2, bytes) < (ssize_t) bytes)
519 if (memcmp (buf1, buf2, bytes) != 0)
536 /* Write a locale file, with contents given by N_ELEM and VEC. */
538 write_locale_data (const char *output_path, int catidx, const char *category,
539 size_t n_elem, struct iovec *vec)
541 size_t cnt, step, maxiov;
544 const char **other_paths;
548 /* The data will be added to the archive. For now we simply
549 generate the image which will be written. First determine
554 to_archive[catidx].size = 0;
555 for (cnt = 0; cnt < n_elem; ++cnt)
556 to_archive[catidx].size += vec[cnt].iov_len;
558 /* Allocate the memory for it. */
559 to_archive[catidx].addr = xmalloc (to_archive[catidx].size);
562 for (cnt = 0, endp = to_archive[catidx].addr; cnt < n_elem; ++cnt)
563 endp = mempcpy (endp, vec[cnt].iov_base, vec[cnt].iov_len);
565 /* Compute the MD5 sum for the data. */
566 __md5_buffer (to_archive[catidx].addr, to_archive[catidx].size,
567 to_archive[catidx].sum);
572 fname = xmalloc (strlen (output_path) + 2 * strlen (category) + 7);
574 /* Normally we write to the directory pointed to by the OUTPUT_PATH.
575 But for LC_MESSAGES we have to take care for the translation
576 data. This means we need to have a directory LC_MESSAGES in
577 which we place the file under the name SYS_LC_MESSAGES. */
578 sprintf (fname, "%s%s", output_path, category);
580 if (strcmp (category, "LC_MESSAGES") == 0)
584 if (stat (fname, &st) < 0)
586 if (mkdir (fname, 0777) >= 0)
592 else if (!S_ISREG (st.st_mode))
599 /* Create the locale file with nlinks == 1; this avoids crashing processes
600 which currently use the locale and damaging files belonging to other
605 fd = creat (fname, 0666);
610 int save_err = errno;
614 sprintf (fname, "%1$s%2$s/SYS_%2$s", output_path, category);
616 fd = creat (fname, 0666);
624 WITH_CUR_LOCALE (error (0, save_err, _("\
625 cannot open output file `%s' for category `%s'"), fname, category));
634 maxiov = sysconf (_SC_UIO_MAXIOV);
637 /* Write the data using writev. But we must take care for the
638 limitation of the implementation. */
639 for (cnt = 0; cnt < n_elem; cnt += step)
643 step = MIN (maxiov, step);
645 if (writev (fd, &vec[cnt], step) < 0)
648 WITH_CUR_LOCALE (error (0, errno, _("\
649 failure while writing data for category `%s'"), category));
656 /* Compare the file with the locale data files for the same category in
657 other locales, and see if we can reuse it, to save disk space. */
658 other_paths = siblings (output_path);
659 if (other_paths != NULL)
661 struct stat fname_stat;
663 if (lstat (fname, &fname_stat) >= 0
664 && S_ISREG (fname_stat.st_mode))
666 const char *fname_tail = fname + strlen (output_path);
667 const char **other_p;
672 for (other_p = other_paths; *other_p; other_p++)
674 seen_inodes = (ino_t *) xmalloc (seen_count * sizeof (ino_t));
677 for (other_p = other_paths; *other_p; other_p++)
679 const char *other_path = *other_p;
680 size_t other_path_len = strlen (other_path);
682 struct stat other_fname_stat;
685 (char *) xmalloc (other_path_len + strlen (fname_tail) + 1);
686 memcpy (other_fname, other_path, other_path_len);
687 strcpy (other_fname + other_path_len, fname_tail);
689 if (lstat (other_fname, &other_fname_stat) >= 0
690 && S_ISREG (other_fname_stat.st_mode)
691 /* Consider only files on the same device.
692 Otherwise hard linking won't work anyway. */
693 && other_fname_stat.st_dev == fname_stat.st_dev
694 /* Consider only files with the same permissions.
695 Otherwise there are security risks. */
696 && other_fname_stat.st_uid == fname_stat.st_uid
697 && other_fname_stat.st_gid == fname_stat.st_gid
698 && other_fname_stat.st_mode == fname_stat.st_mode
699 /* Don't compare fname with itself. */
700 && other_fname_stat.st_ino != fname_stat.st_ino
701 /* Files must have the same size, otherwise they
702 cannot be the same. */
703 && other_fname_stat.st_size == fname_stat.st_size)
705 /* Skip this file if we have already read it (under a
709 for (i = seen_count - 1; i >= 0; i--)
710 if (seen_inodes[i] == other_fname_stat.st_ino)
714 /* Now compare fname and other_fname for real. */
717 #ifdef _STATBUF_ST_BLKSIZE
718 blocksize = MAX (fname_stat.st_blksize,
719 other_fname_stat.st_blksize);
720 if (blocksize > 8 * 1024)
721 blocksize = 8 * 1024;
723 blocksize = 8 * 1024;
726 if (compare_files (fname, other_fname,
727 fname_stat.st_size, blocksize) == 0)
729 /* Found! other_fname is identical to fname. */
730 /* Link other_fname to fname. But use a temporary
731 file, in case hard links don't work on the
732 particular filesystem. */
734 (char *) xmalloc (strlen (fname) + 4 + 1);
736 strcpy (stpcpy (tmp_fname, fname), ".tmp");
738 if (link (other_fname, tmp_fname) >= 0)
741 if (rename (tmp_fname, fname) < 0)
744 WITH_CUR_LOCALE (error (0, errno, _("\
745 cannot create output file `%s' for category `%s'"), fname, category));
754 /* Don't compare with this file a second time. */
755 seen_inodes[seen_count++] = other_fname_stat.st_ino;
768 /* General handling of `copy'. */
770 handle_copy (struct linereader *ldfile, const struct charmap_t *charmap,
771 const char *repertoire_name, struct localedef_t *result,
772 enum token_t token, int locale, const char *locale_name,
778 now = lr_token (ldfile, charmap, result, NULL, verbose);
779 if (now->tok != tok_string)
780 lr_error (ldfile, _("expect string argument for `copy'"));
781 else if (!ignore_content)
783 if (now->val.str.startmb == NULL)
784 lr_error (ldfile, _("\
785 locale name should consist only of portable characters"));
788 (void) add_to_readlist (locale, now->val.str.startmb,
789 repertoire_name, 1, NULL);
790 result->copy_name[locale] = now->val.str.startmb;
794 lr_ignore_rest (ldfile, now->tok == tok_string);
796 /* The rest of the line must be empty and the next keyword must be
798 while ((now = lr_token (ldfile, charmap, result, NULL, verbose))->tok
799 != tok_end && now->tok != tok_eof)
803 lr_error (ldfile, _("\
804 no other keyword shall be specified when `copy' is used"));
808 lr_ignore_rest (ldfile, 0);
811 if (now->tok != tok_eof)
813 /* Handle `END xxx'. */
814 now = lr_token (ldfile, charmap, result, NULL, verbose);
816 if (now->tok != token)
817 lr_error (ldfile, _("\
818 `%1$s' definition does not end with `END %1$s'"), locale_name);
820 lr_ignore_rest (ldfile, now->tok == token);
823 /* When we come here we reached the end of the file. */
824 lr_error (ldfile, _("%s: premature end of file"), locale_name);