7 static char *file_read(int fd
)
10 char *buf
= xmalloc(size
);
13 while ((c
= read(fd
, buf
+ n
, size
- n
)) > 0) {
16 char *newbuf
= xmalloc(size
* 2);
17 memcpy(newbuf
, buf
, n
);
24 static int newlines(char *s
)
27 for (i
= 0; (s
= strchr(s
, '\n')); i
++, s
++);
31 static void initlines(struct txt
*txt
)
34 txt
->lines
[0] = txt
->txt
;
35 for (i
= 1; i
<= txt
->n
; i
++) {
36 char *s
= strchr(txt
->lines
[i
- 1], '\n');
40 txt
->lines
[i
] = s
+ 1;
44 struct txt
*txt_alloc(int fd
)
46 struct txt
*txt
= xmalloc(sizeof(struct txt
));
47 txt
->txt
= file_read(fd
);
48 txt
->n
= newlines(txt
->txt
);
49 txt
->lines
= xmalloc(sizeof(*txt
->lines
) * txt
->n
);
54 char *txt_line(struct txt
*txt
, int line
)
57 return txt
->lines
[line
];
61 void txt_free(struct txt
*txt
)