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
);
26 static int newlines(char *s
)
29 for (i
= 0; (s
= strchr(s
, '\n')); i
++, s
++);
33 static void initlines(struct txt
*txt
)
36 txt
->lines
[0] = txt
->txt
;
37 for (i
= 1; i
<= txt
->n
; i
++) {
38 char *s
= strchr(txt
->lines
[i
- 1], '\n');
42 txt
->lines
[i
] = s
+ 1;
46 struct txt
*txt_alloc(int fd
)
48 struct txt
*txt
= xmalloc(sizeof(struct txt
));
49 txt
->txt
= file_read(fd
);
50 txt
->n
= newlines(txt
->txt
);
51 txt
->lines
= xmalloc(sizeof(*txt
->lines
) * txt
->n
);
56 char *txt_line(struct txt
*txt
, int line
)
59 return txt
->lines
[line
];
63 void txt_free(struct txt
*txt
)