forked from chris-se/tiny-initramfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
86 lines (78 loc) · 2.16 KB
/
io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* tiny_initramfs - Minimalistic initramfs implementation
* Copyright (C) 2016 Christian Seiler <[email protected]>
*
* io.c: I/O helper functions
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "tiny_initramfs.h"
int traverse_file_by_line(const char *filename, traverse_line_t fn, void *data)
{
int fd;
ssize_t r;
char buf[MAX_LINE_LEN] = { 0 };
char *pos;
char *oldpos;
int more_data = 1, line_is_incomplete = 0;
int e;
fd = open(filename, O_RDONLY | O_CLOEXEC);
if (fd < 0)
return -errno;
pos = buf;
while (more_data) {
read_more_data:
memset(pos, 0, MAX_LINE_LEN - 1 - (pos - buf));
r = read(fd, pos, MAX_LINE_LEN - 1 - (pos - buf));
if (r < 0) {
e = -errno;
close(fd);
return e;
}
more_data = (r == MAX_LINE_LEN - 1 - (pos - buf));
oldpos = buf;
do {
pos = strchr(oldpos, '\n');
if (!pos && more_data) {
if (oldpos == buf) {
line_is_incomplete = 1;
pos = oldpos = buf;
} else {
memmove(buf, oldpos, MAX_LINE_LEN - (oldpos - buf));
pos = buf + MAX_LINE_LEN - 1 - (oldpos - buf);
oldpos = buf;
}
goto read_more_data;
}
if (pos) {
*pos = '\0';
pos++;
}
e = fn(data, oldpos, line_is_incomplete);
if (e) {
close(fd);
return e < 0 ? e : 0;
}
line_is_incomplete = 0;
oldpos = pos;
} while (oldpos);
}
close(fd);
return 0;
}