]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - SQUASHFS/squashfs-tools-4.4/squashfs-tools/read_file.c
2 * Create a squashfs filesystem. This is a highly compressed read only
6 * Phillip Lougher <phillip@squashfs.org.uk>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2,
11 * or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #define MAX_LINE 16384
38 * Read file, passing each line to parse_line() for
41 * Lines can be split across multiple lines using "\".
43 * Blank lines and comment lines indicated by # are supported.
45 int read_file(char *filename
, char *type
, int (parse_line
)(char *))
48 char *def
, *err
, *line
= NULL
;
51 fd
= fopen(filename
, "r");
53 ERROR("Could not open %s device file \"%s\" because %s\n",
54 type
, filename
, strerror(errno
));
64 if(total
+ (MAX_LINE
+ 1) > size
) {
65 line
= realloc(line
, size
+= (MAX_LINE
+ 1));
70 err
= fgets(line
+ total
, MAX_LINE
+ 1, fd
);
74 len
= strlen(line
+ total
);
77 if(len
== MAX_LINE
&& line
[total
- 1] != '\n') {
79 ERROR("Line too long when reading "
80 "%s file \"%s\", larger than "
81 "%d bytes\n", type
, filename
, MAX_LINE
);
86 * Remove '\n' terminator if it exists (the last line
87 * in the file may not be '\n' terminated)
89 if(len
&& line
[total
- 1] == '\n') {
90 line
[-- total
] = '\0';
95 * If no line continuation then jump out to
96 * process line. Note, we have to be careful to
97 * check for "\\" (backslashed backslash) and to
98 * ensure we don't look at the previous line
100 if(len
== 0 || line
[total
- 1] != '\\' || (len
>= 2 &&
101 strcmp(line
+ total
- 2, "\\\\") == 0))
109 ERROR("Reading %s file \"%s\" failed "
110 "because %s\n", type
, filename
,
116 * At EOF, normally we'll be finished, but, have to
117 * check for special case where we had "\" line
118 * continuation and then hit EOF immediately afterwards
126 /* Skip any leading whitespace */
127 for(def
= line
; isspace(*def
); def
++);
129 /* if line is now empty after skipping characters, skip it */
133 /* if comment line, skip */
137 res
= parse_line(def
);