Fixed get_line() bugs in email.c

This commit is contained in:
Bobbi Webber-Manners 2020-07-08 17:05:20 -04:00
parent 27a0a05ae2
commit 9c85bf5d78

View File

@ -4,9 +4,7 @@
// Bobbi June, July 2020 // Bobbi June, July 2020
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// - TODO: BUG- If there is no trailing newlines, the last line of the // - TODO: BUG- Fix get_line() in other apps (email.c has been fixed)
// message seems to be shown twice. Probably get_line() error.
// This is most obvious in SENT box if you don't CR final line.
// - TODO: Get rid of all uses of malloc(). Don't need it. // - TODO: Get rid of all uses of malloc(). Don't need it.
// - TODO: See TODOs further down for error handling // - TODO: See TODOs further down for error handling
// - TODO: Editor for email composition functions // - TODO: Editor for email composition functions
@ -484,36 +482,34 @@ void update_highlighted(void) {
* reset - if 1 then just reset the buffer and return * reset - if 1 then just reset the buffer and return
*/ */
int16_t get_line(FILE *fp, uint8_t reset, uint32_t *pos) { int16_t get_line(FILE *fp, uint8_t reset, uint32_t *pos) {
static uint16_t rd = 0; static uint16_t rd = 0; // Read
static uint16_t buflen = 0; static uint16_t wt = 0; // Write
uint8_t found = 0; uint8_t found = 0;
uint16_t j = 0; uint16_t j = 0;
uint16_t i; uint16_t i;
if (reset) { if (reset) {
rd = buflen = 0; rd = wt = 0;
*pos = 0; *pos = 0;
return 0; return 0;
} }
while (1) { while (1) {
for (i = rd; i < buflen; ++i) { while (rd < wt) {
linebuf[j++] = buf[i]; linebuf[j++] = buf[rd++];
++(*pos); ++(*pos);
if (linebuf[j - 1] == '\r') { if (linebuf[j - 1] == '\r') {
found = 1; found = 1;
break; break;
} }
} }
if (found) { linebuf[j] = '\0';
rd = i + 1; if (rd == wt) // Empty buf[]
linebuf[j] = '\0'; rd = wt = 0;
if (found)
return j; return j;
} if (feof(fp))
buflen = fread(buf, 1, READSZ, fp); return -1;
if (buflen == 0) { i = fread(&buf[wt], 1, READSZ - wt, fp);
rd = 0; wt += i;
return -1; // Hit EOF before we found '\r'
}
rd = 0;
} }
} }