2 Copyright (C) 1995-1998, 2000-2002, 2007, 2015 Free Software
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "file-list.h"
34 /* A convenience macro. I don't like writing gettext() every time. */
35 #define _(str) gettext (str)
38 /* Read list of filenames from a file. */
40 read_names_from_file (const char *file_name)
43 char *line_buf = NULL;
45 string_list_ty *result;
47 if (strcmp (file_name, "-") == 0)
51 fp = fopen (file_name, "r");
53 error (EXIT_FAILURE, errno,
54 _("error while opening \"%s\" for reading"), file_name);
57 result = string_list_alloc ();
61 /* Read next line from file. */
62 int len = getline (&line_buf, &line_len, fp);
64 /* In case of an error leave loop. */
68 /* Remove trailing '\n' and trailing whitespace. */
69 if (len > 0 && line_buf[len - 1] == '\n')
70 line_buf[--len] = '\0';
72 && (line_buf[len - 1] == ' '
73 || line_buf[len - 1] == '\t'
74 || line_buf[len - 1] == '\r'))
75 line_buf[--len] = '\0';
77 /* Test if we have to ignore the line. */
78 if (*line_buf == '\0' || *line_buf == '#')
81 string_list_append_unique (result, line_buf);
84 /* Free buffer allocated through getline. */
88 /* Close input stream. */