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
30 #include <sys/param.h>
33 #include "../../crypt/md5.h"
34 #include "localedef.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
;
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
);
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];
70 i18npath
= strdupa (i18npath
);
74 && (next
= strsep (&i18npath
, ":")) != NULL
)
76 stpcpy (stpcpy (stpcpy (path
, next
), "/locales/"), filename
);
78 ldfile
= lr_open (path
, locfile_hash
);
82 stpcpy (stpcpy (path
, next
), filename
);
84 ldfile
= lr_open (path
, locfile_hash
);
89 /* Test in the default directory. */
92 char path
[strlen (filename
) + 1 + sizeof (LOCSRCDIR
)];
94 stpcpy (stpcpy (stpcpy (path
, LOCSRCDIR
), "/"), filename
);
95 ldfile
= lr_open (path
, locfile_hash
);
103 /* Parse locale definition file and store result in RESULT. */
106 struct token
*now
= lr_token (ldfile
, charmap
, NULL
, NULL
, verbose
);
107 enum token_t nowtok
= now
->tok
;
110 if (nowtok
== tok_eof
)
113 if (nowtok
== tok_eol
)
114 /* Ignore empty lines. */
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"));
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);
141 if (nowtok
== tok_escape_char
)
142 ldfile
->escape_char
= *arg
->val
.str
.startmb
;
144 ldfile
->comment_char
= *arg
->val
.str
.startmb
;
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"));
157 if (repertoire_name
== NULL
)
159 repertoire_name
= memcpy (xmalloc (arg
->val
.str
.lenmb
+ 1),
160 arg
->val
.str
.startmb
,
162 ((char *) repertoire_name
)[arg
->val
.str
.lenmb
] = '\0';
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 dirent
*other_dentry
;
393 const char *other_name
;
395 struct stat other_stat
;
397 other_dentry
= readdir (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
;