* catgets/gencat.c: Use GPL, not LGPL.
[platform/upstream/glibc.git] / locale / programs / locfile.c
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.
4
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.
8
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.
13
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.  */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <dirent.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/param.h>
29 #include <sys/stat.h>
30
31 #include "../../crypt/md5.h"
32 #include "localedef.h"
33 #include "locfile.h"
34 #include "simple-hash.h"
35
36 #include "locfile-kw.h"
37
38
39 /* Temporary storage of the locale data before writing it to the archive.  */
40 static locale_data_t to_archive;
41
42
43 int
44 locfile_read (struct localedef_t *result, const struct charmap_t *charmap)
45 {
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;
51
52   /* If no repertoire name was specified use the global one.  */
53   if (repertoire_name == NULL)
54     repertoire_name = repertoire_global;
55
56   /* Open the locale definition file.  */
57   ldfile = lr_open (filename, locfile_hash);
58   if (ldfile == NULL)
59     {
60       if (filename != NULL && filename[0] != '/')
61         {
62           char *i18npath = getenv ("I18NPATH");
63           if (i18npath != NULL && *i18npath != '\0')
64             {
65               const size_t pathlen = strlen (i18npath);
66               char i18npathbuf[pathlen + 1];
67               char path[strlen (filename) + 1 + pathlen
68                         + sizeof ("/locales/") - 1];
69               char *next;
70               i18npath = memcpy (i18npathbuf, i18npath, pathlen + 1);
71
72               while (ldfile == NULL
73                      && (next = strsep (&i18npath, ":")) != NULL)
74                 {
75                   stpcpy (stpcpy (stpcpy (path, next), "/locales/"), filename);
76
77                   ldfile = lr_open (path, locfile_hash);
78
79                   if (ldfile == NULL)
80                     {
81                       stpcpy (stpcpy (path, next), filename);
82
83                       ldfile = lr_open (path, locfile_hash);
84                     }
85                 }
86             }
87
88           /* Test in the default directory.  */
89           if (ldfile == NULL)
90             {
91               char path[strlen (filename) + 1 + sizeof (LOCSRCDIR)];
92
93               stpcpy (stpcpy (stpcpy (path, LOCSRCDIR), "/"), filename);
94               ldfile = lr_open (path, locfile_hash);
95             }
96         }
97
98       if (ldfile == NULL)
99         return 1;
100     }
101
102     /* Parse locale definition file and store result in RESULT.  */
103   while (1)
104     {
105       struct token *now = lr_token (ldfile, charmap, NULL, NULL, verbose);
106       enum token_t nowtok = now->tok;
107       struct token *arg;
108
109       if (nowtok == tok_eof)
110         break;
111
112       if (nowtok == tok_eol)
113         /* Ignore empty lines.  */
114         continue;
115
116       switch (nowtok)
117         {
118         case tok_escape_char:
119         case tok_comment_char:
120           /* We need an argument.  */
121           arg = lr_token (ldfile, charmap, NULL, NULL, verbose);
122
123           if (arg->tok != tok_ident)
124             {
125               SYNTAX_ERROR (_("bad argument"));
126               continue;
127             }
128
129           if (arg->val.str.lenmb != 1)
130             {
131               lr_error (ldfile, _("\
132 argument to `%s' must be a single character"),
133                         nowtok == tok_escape_char
134                         ? "escape_char" : "comment_char");
135
136               lr_ignore_rest (ldfile, 0);
137               continue;
138             }
139
140           if (nowtok == tok_escape_char)
141             ldfile->escape_char = *arg->val.str.startmb;
142           else
143             ldfile->comment_char = *arg->val.str.startmb;
144           break;
145
146         case tok_repertoiremap:
147           /* We need an argument.  */
148           arg = lr_token (ldfile, charmap, NULL, NULL, verbose);
149
150           if (arg->tok != tok_ident)
151             {
152               SYNTAX_ERROR (_("bad argument"));
153               continue;
154             }
155
156           if (repertoire_name == NULL)
157             {
158               repertoire_name = memcpy (xmalloc (arg->val.str.lenmb + 1),
159                                         arg->val.str.startmb,
160                                         arg->val.str.lenmb);
161               ((char *) repertoire_name)[arg->val.str.lenmb] = '\0';
162             }
163           break;
164
165         case tok_lc_ctype:
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;
170           continue;
171
172         case tok_lc_collate:
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;
177           continue;
178
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;
184           continue;
185
186         case tok_lc_numeric:
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;
191           continue;
192
193         case tok_lc_time:
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;
198           continue;
199
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;
205           continue;
206
207         case tok_lc_paper:
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;
212           continue;
213
214         case tok_lc_name:
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;
219           continue;
220
221         case tok_lc_address:
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;
226           continue;
227
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;
233           continue;
234
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;
240           continue;
241
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;
247           continue;
248
249         default:
250           SYNTAX_ERROR (_("\
251 syntax error: not inside a locale definition section"));
252           continue;
253         }
254
255       /* The rest of the line must be empty.  */
256       lr_ignore_rest (ldfile, 1);
257     }
258
259   /* We read all of the file.  */
260   lr_close (ldfile);
261
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;
265
266   return 0;
267 }
268
269
270 /* Semantic checking of locale specifications.  */
271
272 static void (*const check_funcs[]) (struct localedef_t *,
273                                     const struct charmap_t *) =
274 {
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
287 };
288
289 void
290 check_all_categories (struct localedef_t *definitions,
291                       const struct charmap_t *charmap)
292 {
293   int cnt;
294
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);
298 }
299
300
301 /* Writing the locale data files.  All files use the same output_path.  */
302
303 static void (*const write_funcs[]) (struct localedef_t *,
304                                     const struct charmap_t *, const char *) =
305 {
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
318 };
319
320
321 void
322 write_all_categories (struct localedef_t *definitions,
323                       const struct charmap_t *charmap, const char *locname,
324                       const char *output_path)
325 {
326   int cnt;
327
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);
331
332   if (! no_archive)
333     {
334       /* The data has to be added to the archive.  Do this now.  */
335       struct locarhandle ah;
336
337       /* Open the archive.  This call never returns if we cannot
338          successfully open the archive.  */
339       open_archive (&ah, false);
340
341       if (add_locale_to_archive (&ah, locname, to_archive, true) != 0)
342         error (EXIT_FAILURE, errno, _("cannot add to locale archive"));
343
344       /* We are done.  */
345       close_archive (&ah);
346     }
347 }
348
349
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.  */
352 static const char **
353 siblings_uncached (const char *output_path)
354 {
355   size_t len;
356   char *base, *p;
357   struct stat output_stat;
358   DIR *dirp;
359   int nelems;
360   const char **elems;
361
362   /* Remove trailing slashes and trailing pathname component.  */
363   len = strlen (output_path);
364   base = (char *) alloca (len);
365   memcpy (base, output_path, len);
366   p = base + len;
367   while (p > base && p[-1] == '/')
368     p--;
369   if (p == base)
370     return NULL;
371   do
372     p--;
373   while (p > base && p[-1] != '/');
374   if (p == base)
375     return NULL;
376   *--p = '\0';
377   len = p - base;
378
379   /* Get the properties of output_path.  */
380   if (lstat (output_path, &output_stat) < 0 || !S_ISDIR (output_stat.st_mode))
381     return NULL;
382
383   /* Iterate through the directories in base directory.  */
384   dirp = opendir (base);
385   if (dirp == NULL)
386     return NULL;
387   nelems = 0;
388   elems = NULL;
389   for (;;)
390     {
391       struct dirent64 *other_dentry;
392       const char *other_name;
393       char *other_path;
394       struct stat other_stat;
395
396       other_dentry = readdir64 (dirp);
397       if (other_dentry == NULL)
398         break;
399
400       other_name = other_dentry->d_name;
401       if (strcmp (other_name, ".") == 0 || strcmp (other_name, "..") == 0)
402         continue;
403
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);
408
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)
415         {
416           /* Found a subdirectory.  Add a trailing slash and store it.  */
417           p = other_path + len + 1 + strlen (other_name);
418           *p++ = '/';
419           *p = '\0';
420           elems = (const char **) xrealloc ((char *) elems,
421                                             (nelems + 2) * sizeof (char **));
422           elems[nelems++] = other_path;
423         }
424       else
425         free (other_path);
426     }
427   closedir (dirp);
428
429   if (elems != NULL)
430     elems[nelems] = NULL;
431   return elems;
432 }
433
434
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.  */
438 static const char **
439 siblings (const char *output_path)
440 {
441   static const char *last_output_path;
442   static const char **last_result;
443
444   if (output_path != last_output_path)
445     {
446       if (last_result != NULL)
447         {
448           const char **p;
449
450           for (p = last_result; *p != NULL; p++)
451             free ((char *) *p);
452           free (last_result);
453         }
454
455       last_output_path = output_path;
456       last_result = siblings_uncached (output_path);
457     }
458   return last_result;
459 }
460
461
462 /* Read as many bytes from a file descriptor as possible.  */
463 static ssize_t
464 full_read (int fd, void *bufarea, size_t nbyte)
465 {
466   char *buf = (char *) bufarea;
467
468   while (nbyte > 0)
469     {
470       ssize_t retval = read (fd, buf, nbyte);
471
472       if (retval == 0)
473         break;
474       else if (retval > 0)
475         {
476           buf += retval;
477           nbyte -= retval;
478         }
479       else if (errno != EINTR)
480         return retval;
481     }
482   return buf - (char *) bufarea;
483 }
484
485
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.  */
488 static int
489 compare_files (const char *filename1, const char *filename2, size_t size,
490                size_t blocksize)
491 {
492   int fd1, fd2;
493   int ret = -1;
494
495   fd1 = open (filename1, O_RDONLY);
496   if (fd1 >= 0)
497     {
498       fd2 = open (filename2, O_RDONLY);
499       if (fd2 >= 0)
500         {
501           char *buf1 = (char *) xmalloc (2 * blocksize);
502           char *buf2 = buf1 + blocksize;
503
504           ret = 0;
505           while (size > 0)
506             {
507               size_t bytes = (size < blocksize ? size : blocksize);
508
509               if (full_read (fd1, buf1, bytes) < (ssize_t) bytes)
510                 {
511                   ret = -1;
512                   break;
513                 }
514               if (full_read (fd2, buf2, bytes) < (ssize_t) bytes)
515                 {
516                   ret = -1;
517                   break;
518                 }
519               if (memcmp (buf1, buf2, bytes) != 0)
520                 {
521                   ret = 1;
522                   break;
523                 }
524               size -= bytes;
525             }
526
527           free (buf1);
528           close (fd2);
529         }
530       close (fd1);
531     }
532   return ret;
533 }
534
535
536 /* Write a locale file, with contents given by N_ELEM and VEC.  */
537 void
538 write_locale_data (const char *output_path, int catidx, const char *category,
539                    size_t n_elem, struct iovec *vec)
540 {
541   size_t cnt, step, maxiov;
542   int fd;
543   char *fname;
544   const char **other_paths;
545
546   if (! no_archive)
547     {
548       /* The data will be added to the archive.  For now we simply
549          generate the image which will be written.  First determine
550          the size.  */
551       int cnt;
552       void *endp;
553
554       to_archive[catidx].size = 0;
555       for (cnt = 0; cnt < n_elem; ++cnt)
556         to_archive[catidx].size += vec[cnt].iov_len;
557
558       /* Allocate the memory for it.  */
559       to_archive[catidx].addr = xmalloc (to_archive[catidx].size);
560
561       /* Fill it in.  */
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);
564
565       /* Compute the MD5 sum for the data.  */
566       __md5_buffer (to_archive[catidx].addr, to_archive[catidx].size,
567                     to_archive[catidx].sum);
568
569       return;
570     }
571
572   fname = xmalloc (strlen (output_path) + 2 * strlen (category) + 7);
573
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);
579   fd = -2;
580   if (strcmp (category, "LC_MESSAGES") == 0)
581     {
582       struct stat st;
583
584       if (stat (fname, &st) < 0)
585         {
586           if (mkdir (fname, 0777) >= 0)
587             {
588               fd = -1;
589               errno = EISDIR;
590             }
591         }
592       else if (!S_ISREG (st.st_mode))
593         {
594           fd = -1;
595           errno = EISDIR;
596         }
597     }
598
599   /* Create the locale file with nlinks == 1; this avoids crashing processes
600      which currently use the locale and damaging files belonging to other
601      locales as well.  */
602   if (fd == -2)
603     {
604       unlink (fname);
605       fd = creat (fname, 0666);
606     }
607
608   if (fd == -1)
609     {
610       int save_err = errno;
611
612       if (errno == EISDIR)
613         {
614           sprintf (fname, "%1$s%2$s/SYS_%2$s", output_path, category);
615           unlink (fname);
616           fd = creat (fname, 0666);
617           if (fd == -1)
618             save_err = errno;
619         }
620
621       if (fd == -1)
622         {
623           if (!be_quiet)
624             WITH_CUR_LOCALE (error (0, save_err, _("\
625 cannot open output file `%s' for category `%s'"), fname, category));
626           free (fname);
627           return;
628         }
629     }
630
631 #ifdef UIO_MAXIOV
632   maxiov = UIO_MAXIOV;
633 #else
634   maxiov = sysconf (_SC_UIO_MAXIOV);
635 #endif
636
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)
640     {
641       step = n_elem - cnt;
642       if (maxiov > 0)
643         step = MIN (maxiov, step);
644
645       if (writev (fd, &vec[cnt], step) < 0)
646         {
647           if (!be_quiet)
648             WITH_CUR_LOCALE (error (0, errno, _("\
649 failure while writing data for category `%s'"), category));
650           break;
651         }
652     }
653
654   close (fd);
655
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)
660     {
661       struct stat fname_stat;
662
663       if (lstat (fname, &fname_stat) >= 0
664           && S_ISREG (fname_stat.st_mode))
665         {
666           const char *fname_tail = fname + strlen (output_path);
667           const char **other_p;
668           int seen_count;
669           ino_t *seen_inodes;
670
671           seen_count = 0;
672           for (other_p = other_paths; *other_p; other_p++)
673             seen_count++;
674           seen_inodes = (ino_t *) xmalloc (seen_count * sizeof (ino_t));
675           seen_count = 0;
676
677           for (other_p = other_paths; *other_p; other_p++)
678             {
679               const char *other_path = *other_p;
680               size_t other_path_len = strlen (other_path);
681               char *other_fname;
682               struct stat other_fname_stat;
683
684               other_fname =
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);
688
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)
704                 {
705                   /* Skip this file if we have already read it (under a
706                      different name).  */
707                   int i;
708
709                   for (i = seen_count - 1; i >= 0; i--)
710                     if (seen_inodes[i] == other_fname_stat.st_ino)
711                       break;
712                   if (i < 0)
713                     {
714                       /* Now compare fname and other_fname for real.  */
715                       blksize_t blocksize;
716
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;
722 #else
723                       blocksize = 8 * 1024;
724 #endif
725
726                       if (compare_files (fname, other_fname,
727                                          fname_stat.st_size, blocksize) == 0)
728                         {
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.  */
733                           char * tmp_fname =
734                             (char *) xmalloc (strlen (fname) + 4 + 1);
735
736                           strcpy (stpcpy (tmp_fname, fname), ".tmp");
737
738                           if (link (other_fname, tmp_fname) >= 0)
739                             {
740                               unlink (fname);
741                               if (rename (tmp_fname, fname) < 0)
742                                 {
743                                   if (!be_quiet)
744                                     WITH_CUR_LOCALE (error (0, errno, _("\
745 cannot create output file `%s' for category `%s'"), fname, category));
746                                 }
747                               free (tmp_fname);
748                               free (other_fname);
749                               break;
750                             }
751                           free (tmp_fname);
752                         }
753
754                       /* Don't compare with this file a second time.  */
755                       seen_inodes[seen_count++] = other_fname_stat.st_ino;
756                     }
757                 }
758               free (other_fname);
759             }
760           free (seen_inodes);
761         }
762     }
763
764   free (fname);
765 }
766
767
768 /* General handling of `copy'.  */
769 void
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,
773              int ignore_content)
774 {
775   struct token *now;
776   int warned = 0;
777
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)
782     {
783       if (now->val.str.startmb == NULL)
784         lr_error (ldfile, _("\
785 locale name should consist only of portable characters"));
786       else
787         {
788           (void) add_to_readlist (locale, now->val.str.startmb,
789                                   repertoire_name, 1, NULL);
790           result->copy_name[locale] = now->val.str.startmb;
791         }
792     }
793
794   lr_ignore_rest (ldfile, now->tok == tok_string);
795
796   /* The rest of the line must be empty and the next keyword must be
797      `END xxx'.  */
798   while ((now = lr_token (ldfile, charmap, result, NULL, verbose))->tok
799          != tok_end && now->tok != tok_eof)
800     {
801       if (warned == 0)
802         {
803           lr_error (ldfile, _("\
804 no other keyword shall be specified when `copy' is used"));
805           warned = 1;
806         }
807
808       lr_ignore_rest (ldfile, 0);
809     }
810
811   if (now->tok != tok_eof)
812     {
813       /* Handle `END xxx'.  */
814       now = lr_token (ldfile, charmap, result, NULL, verbose);
815
816       if (now->tok != token)
817         lr_error (ldfile, _("\
818 `%1$s' definition does not end with `END %1$s'"), locale_name);
819
820       lr_ignore_rest (ldfile, now->tok == token);
821     }
822   else
823     /* When we come here we reached the end of the file.  */
824     lr_error (ldfile, _("%s: premature end of file"), locale_name);
825 }