Update.
[glibc.git] / locale / programs / locfile.c
blob07a65bbb54d022bb7efbdd9135222d4b0bc00a48
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 "localedef.h"
34 #include "locfile.h"
36 #include "locfile-kw.h"
39 int
40 locfile_read (struct localedef_t *result, const 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);
54 if (ldfile == NULL)
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];
63 char *next;
64 i18npath = strdupa (i18npath);
67 while (ldfile == NULL
68 && (next = strsep (&i18npath, ":")) != NULL)
70 stpcpy (stpcpy (stpcpy (path, next), "/locales/"), filename);
72 ldfile = lr_open (path, locfile_hash);
74 if (ldfile == NULL)
76 stpcpy (stpcpy (path, next), filename);
78 ldfile = lr_open (path, locfile_hash);
83 /* Test in the default directory. */
84 if (ldfile == NULL)
86 char path[strlen (filename) + 1 + sizeof (LOCSRCDIR)];
88 stpcpy (stpcpy (stpcpy (path, LOCSRCDIR), "/"), filename);
89 ldfile = lr_open (path, locfile_hash);
93 if (ldfile == NULL)
94 return 1;
97 /* Parse locale definition file and store result in RESULT. */
98 while (1)
100 struct token *now = lr_token (ldfile, charmap, NULL, NULL, verbose);
101 enum token_t nowtok = now->tok;
102 struct token *arg;
104 if (nowtok == tok_eof)
105 break;
107 if (nowtok == tok_eol)
108 /* Ignore empty lines. */
109 continue;
111 switch (nowtok)
113 case tok_escape_char:
114 case tok_comment_char:
115 /* We need an argument. */
116 arg = lr_token (ldfile, charmap, NULL, NULL, verbose);
118 if (arg->tok != tok_ident)
120 SYNTAX_ERROR (_("bad argument"));
121 continue;
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);
132 continue;
135 if (nowtok == tok_escape_char)
136 ldfile->escape_char = *arg->val.str.startmb;
137 else
138 ldfile->comment_char = *arg->val.str.startmb;
139 break;
141 case tok_repertoiremap:
142 /* We need an argument. */
143 arg = lr_token (ldfile, charmap, NULL, NULL, verbose);
145 if (arg->tok != tok_ident)
147 SYNTAX_ERROR (_("bad argument"));
148 continue;
151 if (repertoire_name == NULL)
153 repertoire_name = memcpy (xmalloc (arg->val.str.lenmb + 1),
154 arg->val.str.startmb,
155 arg->val.str.lenmb);
156 ((char *) repertoire_name)[arg->val.str.lenmb] = '\0';
158 break;
160 case tok_lc_ctype:
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;
165 continue;
167 case tok_lc_collate:
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;
172 continue;
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;
179 continue;
181 case tok_lc_numeric:
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;
186 continue;
188 case tok_lc_time:
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;
193 continue;
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;
200 continue;
202 case tok_lc_paper:
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;
207 continue;
209 case tok_lc_name:
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;
214 continue;
216 case tok_lc_address:
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;
221 continue;
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;
228 continue;
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;
235 continue;
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;
242 continue;
244 default:
245 SYNTAX_ERROR (_("\
246 syntax error: not inside a locale definition section"));
247 continue;
250 /* The rest of the line must be empty. */
251 lr_ignore_rest (ldfile, 1);
254 /* We read all of the file. */
255 lr_close (ldfile);
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;
261 return 0;
265 /* Semantic checking of locale specifications. */
267 static void (*const check_funcs[]) (struct localedef_t *,
268 const 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
284 void
285 check_all_categories (struct localedef_t *definitions,
286 const struct charmap_t *charmap)
288 int cnt;
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 *,
299 const struct charmap_t *, const char *) =
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
315 void
316 write_all_categories (struct localedef_t *definitions,
317 const struct charmap_t *charmap,
318 const char *output_path)
320 int cnt;
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. */
329 static const char **
330 siblings_uncached (const char *output_path)
332 size_t len;
333 char *base, *p;
334 struct stat output_stat;
335 DIR *dirp;
336 int nelems;
337 const char **elems;
339 /* Remove trailing slashes and trailing pathname component. */
340 len = strlen (output_path);
341 base = (char *) alloca (len);
342 memcpy (base, output_path, len);
343 p = base + len;
344 while (p > base && p[-1] == '/')
345 p--;
346 if (p == base)
347 return NULL;
349 p--;
350 while (p > base && p[-1] != '/');
351 if (p == base)
352 return NULL;
353 *--p = '\0';
354 len = p - base;
356 /* Get the properties of output_path. */
357 if (lstat (output_path, &output_stat) < 0 || !S_ISDIR (output_stat.st_mode))
358 return NULL;
360 /* Iterate through the directories in base directory. */
361 dirp = opendir (base);
362 if (dirp == NULL)
363 return NULL;
364 nelems = 0;
365 elems = NULL;
366 for (;;)
368 struct dirent *other_dentry;
369 const char *other_name;
370 char *other_path;
371 struct stat other_stat;
373 other_dentry = readdir (dirp);
374 if (other_dentry == NULL)
375 break;
377 other_name = other_dentry->d_name;
378 if (strcmp (other_name, ".") == 0 || strcmp (other_name, "..") == 0)
379 continue;
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);
395 *p++ = '/';
396 *p = '\0';
397 elems = (const char **) xrealloc ((char *) elems,
398 (nelems + 2) * sizeof (char **));
399 elems[nelems++] = other_path;
401 else
402 free (other_path);
404 closedir (dirp);
406 if (elems != NULL)
407 elems[nelems] = NULL;
408 return elems;
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. */
414 static const char **
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)
424 const char **p;
426 for (p = last_result; *p != NULL; p++)
427 free ((char *) *p);
428 free (last_result);
431 last_output_path = output_path;
432 last_result = siblings_uncached (output_path);
434 return last_result;
437 /* Read as many bytes from a file descriptor as possible. */
438 static ssize_t
439 full_read (int fd, void *bufarea, size_t nbyte)
441 char *buf = (char *) bufarea;
443 while (nbyte > 0)
445 ssize_t retval = read (fd, buf, nbyte);
447 if (retval == 0)
448 break;
449 else if (retval > 0)
451 buf += retval;
452 nbyte -= retval;
454 else if (errno != EINTR)
455 return retval;
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. */
462 static int
463 compare_files (const char *filename1, const char *filename2, size_t size,
464 size_t blocksize)
466 int fd1, fd2;
467 int ret = -1;
469 fd1 = open (filename1, O_RDONLY);
470 if (fd1 >= 0)
472 fd2 = open (filename2, O_RDONLY);
473 if (fd2 >= 0)
475 char *buf1 = (char *) xmalloc (2 * blocksize);
476 char *buf2 = buf1 + blocksize;
478 ret = 0;
479 while (size > 0)
481 size_t bytes = (size < blocksize ? size : blocksize);
483 if (full_read (fd1, buf1, bytes) < (ssize_t) bytes)
485 ret = -1;
486 break;
488 if (full_read (fd2, buf2, bytes) < (ssize_t) bytes)
490 ret = -1;
491 break;
493 if (memcmp (buf1, buf2, bytes) != 0)
495 ret = 1;
496 break;
498 size -= bytes;
501 free (buf1);
502 close (fd2);
504 close (fd1);
506 return ret;
509 /* Write a locale file, with contents given by N_ELEM and VEC. */
510 void
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;
515 int fd;
516 char *fname;
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);
526 fd = -2;
527 if (strcmp (category, "LC_MESSAGES") == 0)
529 struct stat st;
531 if (stat (fname, &st) < 0)
533 if (mkdir (fname, 0777) >= 0)
535 fd = -1;
536 errno = EISDIR;
539 else if (!S_ISREG (st.st_mode))
541 fd = -1;
542 errno = EISDIR;
546 /* Create the locale file with nlinks == 1; this avoids crashing processes
547 which currently use the locale and damaging files belonging to other
548 locales as well. */
549 if (fd == -2)
551 unlink (fname);
552 fd = creat (fname, 0666);
555 if (fd == -1)
557 int save_err = errno;
559 if (errno == EISDIR)
561 sprintf (fname, "%1$s%2$s/SYS_%2$s", output_path, category);
562 unlink (fname);
563 fd = creat (fname, 0666);
564 if (fd == -1)
565 save_err = errno;
568 if (fd == -1)
570 if (!be_quiet)
571 WITH_CUR_LOCALE (error (0, save_err, _("\
572 cannot open output file `%s' for category `%s'"), fname, category));
573 free (fname);
574 return;
578 #ifdef UIO_MAXIOV
579 maxiov = UIO_MAXIOV;
580 #else
581 maxiov = sysconf (_SC_UIO_MAXIOV);
582 #endif
584 /* Write the data using writev. But we must take care for the
585 limitation of the implementation. */
586 for (cnt = 0; cnt < n_elem; cnt += step)
588 step = n_elem - cnt;
589 if (maxiov > 0)
590 step = MIN (maxiov, step);
592 if (writev (fd, &vec[cnt], step) < 0)
594 if (!be_quiet)
595 WITH_CUR_LOCALE (error (0, errno, _("\
596 failure while writing data for category `%s'"), category));
597 break;
601 close (fd);
603 /* Compare the file with the locale data files for the same category in
604 other locales, and see if we can reuse it, to save disk space. */
605 other_paths = siblings (output_path);
606 if (other_paths != NULL)
608 struct stat fname_stat;
610 if (lstat (fname, &fname_stat) >= 0
611 && S_ISREG (fname_stat.st_mode))
613 const char *fname_tail = fname + strlen (output_path);
614 const char **other_p;
615 int seen_count;
616 ino_t *seen_inodes;
618 seen_count = 0;
619 for (other_p = other_paths; *other_p; other_p++)
620 seen_count++;
621 seen_inodes = (ino_t *) xmalloc (seen_count * sizeof (ino_t));
622 seen_count = 0;
624 for (other_p = other_paths; *other_p; other_p++)
626 const char *other_path = *other_p;
627 size_t other_path_len = strlen (other_path);
628 char *other_fname;
629 struct stat other_fname_stat;
631 other_fname =
632 (char *) xmalloc (other_path_len + strlen (fname_tail) + 1);
633 memcpy (other_fname, other_path, other_path_len);
634 strcpy (other_fname + other_path_len, fname_tail);
636 if (lstat (other_fname, &other_fname_stat) >= 0
637 && S_ISREG (other_fname_stat.st_mode)
638 /* Consider only files on the same device.
639 Otherwise hard linking won't work anyway. */
640 && other_fname_stat.st_dev == fname_stat.st_dev
641 /* Consider only files with the same permissions.
642 Otherwise there are security risks. */
643 && other_fname_stat.st_uid == fname_stat.st_uid
644 && other_fname_stat.st_gid == fname_stat.st_gid
645 && other_fname_stat.st_mode == fname_stat.st_mode
646 /* Don't compare fname with itself. */
647 && other_fname_stat.st_ino != fname_stat.st_ino
648 /* Files must have the same size, otherwise they
649 cannot be the same. */
650 && other_fname_stat.st_size == fname_stat.st_size)
652 /* Skip this file if we have already read it (under a
653 different name). */
654 int i;
656 for (i = seen_count - 1; i >= 0; i--)
657 if (seen_inodes[i] == other_fname_stat.st_ino)
658 break;
659 if (i < 0)
661 /* Now compare fname and other_fname for real. */
662 blksize_t blocksize;
664 #ifdef _STATBUF_ST_BLKSIZE
665 blocksize = MAX (fname_stat.st_blksize,
666 other_fname_stat.st_blksize);
667 if (blocksize > 8 * 1024)
668 blocksize = 8 * 1024;
669 #else
670 blocksize = 8 * 1024;
671 #endif
673 if (compare_files (fname, other_fname,
674 fname_stat.st_size, blocksize) == 0)
676 /* Found! other_fname is identical to fname. */
677 /* Link other_fname to fname. But use a temporary
678 file, in case hard links don't work on the
679 particular filesystem. */
680 char * tmp_fname =
681 (char *) xmalloc (strlen (fname) + 4 + 1);
683 strcpy (tmp_fname, fname);
684 strcat (tmp_fname, ".tmp");
686 if (link (other_fname, tmp_fname) >= 0)
688 unlink (fname);
689 if (rename (tmp_fname, fname) < 0)
691 if (!be_quiet)
692 WITH_CUR_LOCALE (error (0, errno, _("\
693 cannot create output file `%s' for category `%s'"), fname, category));
695 free (tmp_fname);
696 free (other_fname);
697 break;
699 free (tmp_fname);
702 /* Don't compare with this file a second time. */
703 seen_inodes[seen_count++] = other_fname_stat.st_ino;
706 free (other_fname);
708 free (seen_inodes);
712 free (fname);