inserting: strip a carriage return before copying the line

Also, store the input character earlier, so we don't have to use len - 1.
Furthermore, len increments in steps of 1, so it cannot pass the value of
bufx unnoticed, so use a comparison for equality.
master
Benno Schulenberg 2016-04-17 13:02:04 +02:00
parent c5b8f52c07
commit 6f185d3022
1 changed files with 9 additions and 9 deletions

View File

@ -689,15 +689,14 @@ filestruct *read_line(char *buf, size_t buf_len, filestruct *prevnode)
assert(openfile->fileage != NULL && strlen(buf) == buf_len); assert(openfile->fileage != NULL && strlen(buf) == buf_len);
freshline->data = mallocstrcpy(NULL, buf);
#ifndef NANO_TINY #ifndef NANO_TINY
/* If it's a DOS file ("\r\n"), and file conversion isn't disabled, /* If file conversion isn't disabled, strip a '\r' from the line. */
* strip the '\r' part from the data. */ if (buf_len > 0 && buf[buf_len - 1] == '\r' && !ISSET(NO_CONVERT))
if (!ISSET(NO_CONVERT) && buf_len > 0 && buf[buf_len - 1] == '\r') buf[buf_len - 1] = '\0';
freshline->data[buf_len - 1] = '\0';
#endif #endif
freshline->data = mallocstrcpy(NULL, buf);
#ifndef DISABLE_COLOR #ifndef DISABLE_COLOR
freshline->multidata = NULL; freshline->multidata = NULL;
#endif #endif
@ -803,18 +802,19 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable, bool checkw
len = 1; len = 1;
#endif #endif
} else { } else {
/* Store the character. */
buf[len] = input;
/* Keep track of the total length of the line. It might have /* Keep track of the total length of the line. It might have
* nulls in it, so we can't just use strlen() later. */ * nulls in it, so we can't just use strlen() later. */
len++; len++;
/* If needed, increase the buffer size, MAX_BUF_SIZE characters at /* If needed, increase the buffer size, MAX_BUF_SIZE characters at
* a time. Don't bother decreasing it; it is freed at the end. */ * a time. Don't bother decreasing it; it is freed at the end. */
if (len >= bufx) { if (len == bufx) {
bufx += MAX_BUF_SIZE; bufx += MAX_BUF_SIZE;
buf = charealloc(buf, bufx); buf = charealloc(buf, bufx);
} }
buf[len - 1] = input;
} }
} }