aboutsummaryrefslogtreecommitdiff
path: root/lib/fstree
diff options
context:
space:
mode:
Diffstat (limited to 'lib/fstree')
-rw-r--r--lib/fstree/fstree_from_file.c76
1 files changed, 24 insertions, 52 deletions
diff --git a/lib/fstree/fstree_from_file.c b/lib/fstree/fstree_from_file.c
index bf11755..678d565 100644
--- a/lib/fstree/fstree_from_file.c
+++ b/lib/fstree/fstree_from_file.c
@@ -7,12 +7,12 @@
#include "config.h"
#include "fstree.h"
+#include "fstream.h"
#include <stdlib.h>
-#include <stdint.h>
#include <string.h>
-#include <ctype.h>
#include <errno.h>
+#include <ctype.h>
static int add_generic(fstree_t *fs, const char *filename, size_t line_num,
const char *path, struct stat *sb, const char *extra)
@@ -93,28 +93,6 @@ static const struct {
#define NUM_HOOKS (sizeof(file_list_hooks) / sizeof(file_list_hooks[0]))
-static void trim_line(char *line)
-{
- size_t i;
-
- for (i = 0; isspace(line[i]); ++i)
- ;
-
- if (line[i] == '#') {
- line[0] = '\0';
- return;
- }
-
- if (i > 0)
- memmove(line, line + i, strlen(line + i) + 1);
-
- i = strlen(line);
- while (i > 0 && isspace(line[i - 1]))
- --i;
-
- line[i] = '\0';
-}
-
static int handle_line(fstree_t *fs, const char *filename,
size_t line_num, char *line)
{
@@ -279,44 +257,38 @@ out_desc:
return -1;
}
-int fstree_from_file(fstree_t *fs, const char *filename, FILE *fp)
+int fstree_from_file(fstree_t *fs, const char *filename)
{
- size_t n, line_num = 0;
- ssize_t ret;
+ size_t line_num = 1;
+ istream_t *fp;
char *line;
+ int ret;
- for (;;) {
- line = NULL;
- n = 0;
- errno = 0;
-
- ret = getline(&line, &n, fp);
- ++line_num;
-
- if (ret < 0) {
- if (errno == 0) {
- free(line);
- break;
- }
-
- perror(filename);
- goto fail_line;
- }
-
- trim_line(line);
+ fp = istream_open_file(filename);
+ if (fp == NULL)
+ return -1;
- if (line[0] == '\0') {
- free(line);
- continue;
+ for (;;) {
+ ret = istream_get_line(fp, &line, &line_num,
+ ISTREAM_LINE_LTRIM | ISTREAM_LINE_SKIP_EMPTY);
+ if (ret < 0)
+ return -1;
+ if (ret > 0)
+ break;
+
+ if (line[0] != '#') {
+ if (handle_line(fs, filename, line_num, line))
+ goto fail_line;
}
- if (handle_line(fs, filename, line_num, line))
- goto fail_line;
-
free(line);
+ ++line_num;
}
+
+ sqfs_destroy(fp);
return 0;
fail_line:
free(line);
+ sqfs_destroy(fp);
return -1;
}