13 #define MAX_BUF ((512 * 1024) - 1)
15 int lparse_fd(int fd
, size_t size
, int (*parse
)(char *, size_t))
17 size_t tot_rd
= 0, parsed
= 0;
19 if (!buf
&& !(buf
= calloc(1, MAX_BUF
+ 1)))
22 while (tot_rd
< size
) {
27 bytes_rd
= read(fd
, buf
, MAX_BUF
< size
? MAX_BUF
: size
);
31 if (tot_rd
+ bytes_rd
< size
) {
32 /* rewind the file to the last found newline */
33 while (buf
[bytes_rd
- 1] != '\n')
35 lseek(fd
, tot_rd
+ bytes_rd
, SEEK_SET
);
39 /* set a sentinel for memchr() */
43 for (blkparsed
= 0; blkparsed
< bytes_rd
; cur
= next
+ 1) {
45 next
= memchr(cur
, '\n', bytes_rd
+ 1 - blkparsed
);
58 int lparse_path(const char *path
, size_t size
, int (*parse
)(char *, size_t))
62 /* zero size files are never interesting */
66 if ((fd
= open(path
, O_RDONLY
)) < 0)
69 result
= lparse_fd(fd
, size
, parse
);