6 static char *file_read(int fd
)
9 char *buf
= xmalloc(size
);
12 while ((c
= read(fd
, buf
+ n
, size
- n
)) > 0) {
15 char *newbuf
= xmalloc(size
* 2);
16 memcpy(newbuf
, buf
, n
);
25 static int newlines(char *s
)
28 for (i
= 0; (s
= strchr(s
, '\n')); i
++, s
++);
32 static void initlines(struct txt
*txt
)
35 txt
->lines
[0] = txt
->txt
;
36 for (i
= 1; i
<= txt
->n
; i
++) {
37 char *s
= strchr(txt
->lines
[i
- 1], '\n');
41 txt
->lines
[i
] = s
+ 1;
45 struct txt
*txt_alloc(int fd
)
47 struct txt
*txt
= xmalloc(sizeof(struct txt
));
48 txt
->txt
= file_read(fd
);
49 txt
->n
= newlines(txt
->txt
);
50 txt
->lines
= xmalloc(sizeof(*txt
->lines
) * txt
->n
);
55 char *txt_line(struct txt
*txt
, int line
)
58 return txt
->lines
[line
];
62 void txt_free(struct txt
*txt
)