- char buf[FILE_READ_LINE_BUF_SIZE];
- int buf_len;
- char *line = NULL;
- int line_size = 0;
-
- memset(buf, 0, FILE_READ_LINE_BUF_SIZE);
- while (fgets(buf, FILE_READ_LINE_BUF_SIZE, file)) {
- buf_len = strlen(buf);
- if (line) {
- line_size += buf_len;
- line = xrealloc(line, line_size);
- strcat(line, buf);
- } else {
- line_size = buf_len + 1;
- line = xstrdup(buf);
- }
- if (buf[buf_len - 1] == '\n') {
- break;
+ char buf[BUFSIZ];
+ unsigned int buf_len;
+ char *line = NULL;
+ unsigned int line_size = 0;
+ int got_nl = 0;
+
+ buf[0] = '\0';
+
+ while (fgets(buf, BUFSIZ, fp)) {
+ buf_len = strlen(buf);
+ if (buf[buf_len - 1] == '\n') {
+ buf_len--;
+ buf[buf_len] = '\0';
+ got_nl = 1;
+ }
+ if (line) {
+ line_size += buf_len;
+ line = xrealloc(line, line_size+1);
+ strncat(line, buf, line_size);
+ } else {
+ line_size = buf_len + 1;
+ line = xstrdup(buf);
+ }
+ if (got_nl)
+ break;