Update.
[glibc.git] / locale / programs / locfile.c
blobc5e5581ac509d0fb5dbe28b215696715b1a475d8
1 /* Copyright (C) 1996-2001, 2002 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
18 02111-1307 USA. */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <dirent.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/param.h>
31 #include <sys/stat.h>
33 #include "../../crypt/md5.h"
34 #include "localedef.h"
35 #include "locfile.h"
36 #include "simple-hash.h"
38 #include "locfile-kw.h"
41 /* Temporary storage of the locale data before writing it to the archive. */
42 static locale_data_t to_archive;
45 int
46 locfile_read (struct localedef_t *result, const struct charmap_t *charmap)
48 const char *filename = result->name;
49 const char *repertoire_name = result->repertoire_name;
50 int locale_mask = result->needed & ~result->avail;
51 struct linereader *ldfile;
52 int not_here = ALL_LOCALES;
54 /* If no repertoire name was specified use the global one. */
55 if (repertoire_name == NULL)
56 repertoire_name = repertoire_global;
58 /* Open the locale definition file. */
59 ldfile = lr_open (filename, locfile_hash);
60 if (ldfile == NULL)
62 if (filename != NULL && filename[0] != '/')
64 char *i18npath = getenv ("I18NPATH");
65 if (i18npath != NULL && *i18npath != '\0')
67 char path[strlen (filename) + 1 + strlen (i18npath)
68 + sizeof ("/locales/") - 1];
69 char *next;
70 i18npath = strdupa (i18npath);
73 while (ldfile == NULL
74 && (next = strsep (&i18npath, ":")) != NULL)
76 stpcpy (stpcpy (stpcpy (path, next), "/locales/"), filename);
78 ldfile = lr_open (path, locfile_hash);
80 if (ldfile == NULL)
82 stpcpy (stpcpy (path, next), filename);
84 ldfile = lr_open (path, locfile_hash);
89 /* Test in the default directory. */
90 if (ldfile == NULL)
92 char path[strlen (filename) + 1 + sizeof (LOCSRCDIR)];
94 stpcpy (stpcpy (stpcpy (path, LOCSRCDIR), "/"), filename);
95 ldfile = lr_open (path, locfile_hash);
99 if (ldfile == NULL)
100 return 1;
103 /* Parse locale definition file and store result in RESULT. */
104 while (1)
106 struct token *now = lr_token (ldfile, charmap, NULL, NULL, verbose);
107 enum token_t nowtok = now->tok;
108 struct token *arg;
110 if (nowtok == tok_eof)
111 break;
113 if (nowtok == tok_eol)
114 /* Ignore empty lines. */
115 continue;
117 switch (nowtok)
119 case tok_escape_char:
120 case tok_comment_char:
121 /* We need an argument. */
122 arg = lr_token (ldfile, charmap, NULL, NULL, verbose);
124 if (arg->tok != tok_ident)
126 SYNTAX_ERROR (_("bad argument"));
127 continue;
130 if (arg->val.str.lenmb != 1)
132 lr_error (ldfile, _("\
133 argument to `%s' must be a single character"),
134 nowtok == tok_escape_char
135 ? "escape_char" : "comment_char");
137 lr_ignore_rest (ldfile, 0);
138 continue;
141 if (nowtok == tok_escape_char)
142 ldfile->escape_char = *arg->val.str.startmb;
143 else
144 ldfile->comment_char = *arg->val.str.startmb;
145 break;
147 case tok_repertoiremap:
148 /* We need an argument. */
149 arg = lr_token (ldfile, charmap, NULL, NULL, verbose);
151 if (arg->tok != tok_ident)
153 SYNTAX_ERROR (_("bad argument"));
154 continue;
157 if (repertoire_name == NULL)
159 repertoire_name = memcpy (xmalloc (arg->val.str.lenmb + 1),
160 arg->val.str.startmb,
161 arg->val.str.lenmb);
162 ((char *) repertoire_name)[arg->val.str.lenmb] = '\0';
164 break;
166 case tok_lc_ctype:
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;
171 continue;
173 case tok_lc_collate:
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;
178 continue;
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;
185 continue;
187 case tok_lc_numeric:
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;
192 continue;
194 case tok_lc_time:
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;
199 continue;
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;
206 continue;
208 case tok_lc_paper:
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;
213 continue;
215 case tok_lc_name:
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;
220 continue;
222 case tok_lc_address:
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;
227 continue;
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;
234 continue;
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;
241 continue;
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;
248 continue;
250 default:
251 SYNTAX_ERROR (_("\
252 syntax error: not inside a locale definition section"));
253 continue;
256 /* The rest of the line must be empty. */
257 lr_ignore_rest (ldfile, 1);
260 /* We read all of the file. */
261 lr_close (ldfile);
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;
267 return 0;
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
290 void
291 check_all_categories (struct localedef_t *definitions,
292 const struct charmap_t *charmap)
294 int cnt;
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
322 void
323 write_all_categories (struct localedef_t *definitions,
324 const struct charmap_t *charmap, const char *locname,
325 const char *output_path)
327 int cnt;
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);
333 if (! no_archive)
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"));
345 /* We are done. */
346 close_archive (&ah);
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. */
353 static const char **
354 siblings_uncached (const char *output_path)
356 size_t len;
357 char *base, *p;
358 struct stat output_stat;
359 DIR *dirp;
360 int nelems;
361 const char **elems;
363 /* Remove trailing slashes and trailing pathname component. */
364 len = strlen (output_path);
365 base = (char *) alloca (len);
366 memcpy (base, output_path, len);
367 p = base + len;
368 while (p > base && p[-1] == '/')
369 p--;
370 if (p == base)
371 return NULL;
373 p--;
374 while (p > base && p[-1] != '/');
375 if (p == base)
376 return NULL;
377 *--p = '\0';
378 len = p - base;
380 /* Get the properties of output_path. */
381 if (lstat (output_path, &output_stat) < 0 || !S_ISDIR (output_stat.st_mode))
382 return NULL;
384 /* Iterate through the directories in base directory. */
385 dirp = opendir (base);
386 if (dirp == NULL)
387 return NULL;
388 nelems = 0;
389 elems = NULL;
390 for (;;)
392 struct dirent *other_dentry;
393 const char *other_name;
394 char *other_path;
395 struct stat other_stat;
397 other_dentry = readdir (dirp);
398 if (other_dentry == NULL)
399 break;
401 other_name = other_dentry->d_name;
402 if (strcmp (other_name, ".") == 0 || strcmp (other_name, "..") == 0)
403 continue;
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);
419 *p++ = '/';
420 *p = '\0';
421 elems = (const char **) xrealloc ((char *) elems,
422 (nelems + 2) * sizeof (char **));
423 elems[nelems++] = other_path;
425 else
426 free (other_path);
428 closedir (dirp);
430 if (elems != NULL)
431 elems[nelems] = NULL;
432 return elems;
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. */
439 static const char **
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)
449 const char **p;
451 for (p = last_result; *p != NULL; p++)
452 free ((char *) *p);
453 free (last_result);
456 last_output_path = output_path;
457 last_result = siblings_uncached (output_path);
459 return last_result;
463 /* Read as many bytes from a file descriptor as possible. */
464 static ssize_t
465 full_read (int fd, void *bufarea, size_t nbyte)
467 char *buf = (char *) bufarea;
469 while (nbyte > 0)
471 ssize_t retval = read (fd, buf, nbyte);
473 if (retval == 0)
474 break;
475 else if (retval > 0)
477 buf += retval;
478 nbyte -= retval;
480 else if (errno != EINTR)
481 return retval;
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. */
489 static int
490 compare_files (const char *filename1, const char *filename2, size_t size,
491 size_t blocksize)
493 int fd1, fd2;
494 int ret = -1;
496 fd1 = open (filename1, O_RDONLY);
497 if (fd1 >= 0)
499 fd2 = open (filename2, O_RDONLY);
500 if (fd2 >= 0)
502 char *buf1 = (char *) xmalloc (2 * blocksize);
503 char *buf2 = buf1 + blocksize;
505 ret = 0;
506 while (size > 0)
508 size_t bytes = (size < blocksize ? size : blocksize);
510 if (full_read (fd1, buf1, bytes) < (ssize_t) bytes)
512 ret = -1;
513 break;
515 if (full_read (fd2, buf2, bytes) < (ssize_t) bytes)
517 ret = -1;
518 break;
520 if (memcmp (buf1, buf2, bytes) != 0)
522 ret = 1;
523 break;
525 size -= bytes;
528 free (buf1);
529 close (fd2);
531 close (fd1);
533 return ret;
537 /* Write a locale file, with contents given by N_ELEM and VEC. */
538 void
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;
543 int fd;
544 char *fname;
545 const char **other_paths;
547 if (! no_archive)
549 /* The data will be added to the archive. For now we simply
550 generate the image which will be written. First determine
551 the size. */
552 int cnt;
553 void *endp;
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);
562 /* Fill it in. */
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);
570 return;
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);
580 fd = -2;
581 if (strcmp (category, "LC_MESSAGES") == 0)
583 struct stat st;
585 if (stat (fname, &st) < 0)
587 if (mkdir (fname, 0777) >= 0)
589 fd = -1;
590 errno = EISDIR;
593 else if (!S_ISREG (st.st_mode))
595 fd = -1;
596 errno = EISDIR;
600 /* Create the locale file with nlinks == 1; this avoids crashing processes
601 which currently use the locale and damaging files belonging to other
602 locales as well. */
603 if (fd == -2)
605 unlink (fname);
606 fd = creat (fname, 0666);
609 if (fd == -1)
611 int save_err = errno;
613 if (errno == EISDIR)
615 sprintf (fname, "%1$s%2$s/SYS_%2$s", output_path, category);
616 unlink (fname);
617 fd = creat (fname, 0666);
618 if (fd == -1)
619 save_err = errno;
622 if (fd == -1)
624 if (!be_quiet)
625 WITH_CUR_LOCALE (error (0, save_err, _("\
626 cannot open output file `%s' for category `%s'"), fname, category));
627 free (fname);
628 return;
632 #ifdef UIO_MAXIOV
633 maxiov = UIO_MAXIOV;
634 #else
635 maxiov = sysconf (_SC_UIO_MAXIOV);
636 #endif
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)
642 step = n_elem - cnt;
643 if (maxiov > 0)
644 step = MIN (maxiov, step);
646 if (writev (fd, &vec[cnt], step) < 0)
648 if (!be_quiet)
649 WITH_CUR_LOCALE (error (0, errno, _("\
650 failure while writing data for category `%s'"), category));
651 break;
655 close (fd);
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;
669 int seen_count;
670 ino_t *seen_inodes;
672 seen_count = 0;
673 for (other_p = other_paths; *other_p; other_p++)
674 seen_count++;
675 seen_inodes = (ino_t *) xmalloc (seen_count * sizeof (ino_t));
676 seen_count = 0;
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);
682 char *other_fname;
683 struct stat other_fname_stat;
685 other_fname =
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
707 different name). */
708 int i;
710 for (i = seen_count - 1; i >= 0; i--)
711 if (seen_inodes[i] == other_fname_stat.st_ino)
712 break;
713 if (i < 0)
715 /* Now compare fname and other_fname for real. */
716 blksize_t blocksize;
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;
723 #else
724 blocksize = 8 * 1024;
725 #endif
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. */
734 char * tmp_fname =
735 (char *) xmalloc (strlen (fname) + 4 + 1);
737 strcpy (stpcpy (tmp_fname, fname), ".tmp");
739 if (link (other_fname, tmp_fname) >= 0)
741 unlink (fname);
742 if (rename (tmp_fname, fname) < 0)
744 if (!be_quiet)
745 WITH_CUR_LOCALE (error (0, errno, _("\
746 cannot create output file `%s' for category `%s'"), fname, category));
748 free (tmp_fname);
749 free (other_fname);
750 break;
752 free (tmp_fname);
755 /* Don't compare with this file a second time. */
756 seen_inodes[seen_count++] = other_fname_stat.st_ino;
759 free (other_fname);
761 free (seen_inodes);
765 free (fname);