xref: /netbsd-src/external/gpl2/gettext/dist/gettext-tools/src/file-list.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1*946379e7Schristos /* Reading file lists.
2*946379e7Schristos    Copyright (C) 1995-1998, 2000-2002 Free Software Foundation, Inc.
3*946379e7Schristos 
4*946379e7Schristos    This program is free software; you can redistribute it and/or modify
5*946379e7Schristos    it under the terms of the GNU General Public License as published by
6*946379e7Schristos    the Free Software Foundation; either version 2, or (at your option)
7*946379e7Schristos    any later version.
8*946379e7Schristos 
9*946379e7Schristos    This program is distributed in the hope that it will be useful,
10*946379e7Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
11*946379e7Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*946379e7Schristos    GNU General Public License for more details.
13*946379e7Schristos 
14*946379e7Schristos    You should have received a copy of the GNU General Public License
15*946379e7Schristos    along with this program; if not, write to the Free Software Foundation,
16*946379e7Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17*946379e7Schristos 
18*946379e7Schristos #ifdef HAVE_CONFIG_H
19*946379e7Schristos # include "config.h"
20*946379e7Schristos #endif
21*946379e7Schristos 
22*946379e7Schristos /* Specification.  */
23*946379e7Schristos #include "file-list.h"
24*946379e7Schristos 
25*946379e7Schristos #include <errno.h>
26*946379e7Schristos #include <stdio.h>
27*946379e7Schristos #include <stdlib.h>
28*946379e7Schristos #include <string.h>
29*946379e7Schristos 
30*946379e7Schristos #include "str-list.h"
31*946379e7Schristos #include "error.h"
32*946379e7Schristos #include "exit.h"
33*946379e7Schristos #include "getline.h"
34*946379e7Schristos #include "gettext.h"
35*946379e7Schristos 
36*946379e7Schristos /* A convenience macro.  I don't like writing gettext() every time.  */
37*946379e7Schristos #define _(str) gettext (str)
38*946379e7Schristos 
39*946379e7Schristos 
40*946379e7Schristos /* Read list of filenames from a file.  */
41*946379e7Schristos string_list_ty *
read_names_from_file(const char * file_name)42*946379e7Schristos read_names_from_file (const char *file_name)
43*946379e7Schristos {
44*946379e7Schristos   size_t line_len = 0;
45*946379e7Schristos   char *line_buf = NULL;
46*946379e7Schristos   FILE *fp;
47*946379e7Schristos   string_list_ty *result;
48*946379e7Schristos 
49*946379e7Schristos   if (strcmp (file_name, "-") == 0)
50*946379e7Schristos     fp = stdin;
51*946379e7Schristos   else
52*946379e7Schristos     {
53*946379e7Schristos       fp = fopen (file_name, "r");
54*946379e7Schristos       if (fp == NULL)
55*946379e7Schristos 	error (EXIT_FAILURE, errno,
56*946379e7Schristos 	       _("error while opening \"%s\" for reading"), file_name);
57*946379e7Schristos     }
58*946379e7Schristos 
59*946379e7Schristos   result = string_list_alloc ();
60*946379e7Schristos 
61*946379e7Schristos   while (!feof (fp))
62*946379e7Schristos     {
63*946379e7Schristos       /* Read next line from file.  */
64*946379e7Schristos       int len = getline (&line_buf, &line_len, fp);
65*946379e7Schristos 
66*946379e7Schristos       /* In case of an error leave loop.  */
67*946379e7Schristos       if (len < 0)
68*946379e7Schristos 	break;
69*946379e7Schristos 
70*946379e7Schristos       /* Remove trailing '\n' and trailing whitespace.  */
71*946379e7Schristos       if (len > 0 && line_buf[len - 1] == '\n')
72*946379e7Schristos 	line_buf[--len] = '\0';
73*946379e7Schristos       while (len > 0
74*946379e7Schristos 	     && (line_buf[len - 1] == ' '
75*946379e7Schristos 		 || line_buf[len - 1] == '\t'
76*946379e7Schristos 		 || line_buf[len - 1] == '\r'))
77*946379e7Schristos 	line_buf[--len] = '\0';
78*946379e7Schristos 
79*946379e7Schristos       /* Test if we have to ignore the line.  */
80*946379e7Schristos       if (*line_buf == '\0' || *line_buf == '#')
81*946379e7Schristos 	continue;
82*946379e7Schristos 
83*946379e7Schristos       string_list_append_unique (result, line_buf);
84*946379e7Schristos     }
85*946379e7Schristos 
86*946379e7Schristos   /* Free buffer allocated through getline.  */
87*946379e7Schristos   if (line_buf != NULL)
88*946379e7Schristos     free (line_buf);
89*946379e7Schristos 
90*946379e7Schristos   /* Close input stream.  */
91*946379e7Schristos   if (fp != stdin)
92*946379e7Schristos     fclose (fp);
93*946379e7Schristos 
94*946379e7Schristos   return result;
95*946379e7Schristos }
96