All apps: fixed get_line() to handle final line without CR termination

This commit is contained in:
Bobbi Webber-Manners 2020-09-17 18:49:04 -04:00
parent b54f32779a
commit de5520203f
7 changed files with 75 additions and 71 deletions

View File

@ -155,13 +155,13 @@ void readconfigfile(void) {
/*
* Read a text file a line at a time
* Returns number of chars in the line, or -1 if EOF.
* Returns number of chars in the line, or 0 if EOF.
* Expects Apple ][ style line endings (CR) and does no conversion
* fp - file to read from
* writep - Pointer to buffer into which line will be written
* n - length of buffer. Longer lines will be truncated and terminated with CR.
*/
int16_t get_line(FILE *fp, char *writep, uint16_t n) {
uint16_t get_line(FILE *fp, char *writep, uint16_t n) {
static uint16_t rd = 0; // Read
static uint16_t end = 0; // End of valid data in buf
uint16_t i = 0;
@ -171,18 +171,18 @@ int16_t get_line(FILE *fp, char *writep, uint16_t n) {
rd = 0;
}
if (end == 0)
return -1; // EOF
goto done;
if (i == n - 1) {
writep[i - 1] = '\r';
writep[i] = '\0';
return i;
goto done;
}
writep[i++] = buf[rd++];
if (writep[i - 1] == '\r') {
writep[i] = '\0';
return i;
}
if (writep[i - 1] == '\r')
goto done;
}
done:
writep[i] = '\0';
return i;
}
/*
@ -593,7 +593,7 @@ void attach(char *fname) {
printf(" Copying email content ... "); // Space is for spinner to eat
size = 0;
while ((chars = get_line(fp, linebuf, LINEBUFSZ)) != -1) {
while ((chars = get_line(fp, linebuf, LINEBUFSZ)) != 0) {
size += chars;
if (linebuf[0] == '\r')
break;
@ -606,7 +606,7 @@ void attach(char *fname) {
fprintf(destfp, "--a2forever\r");
fprintf(destfp, "Content-Type: text/plain; charset=US-ASCII\r");
fprintf(destfp, "Content-Transfer-Encoding: 7bit\r\r");
while ((chars = get_line(fp, linebuf, LINEBUFSZ)) != -1) {
while ((chars = get_line(fp, linebuf, LINEBUFSZ)) != 0) {
size += chars;
fputs(linebuf, destfp);
spinner(size, 0);

View File

@ -627,7 +627,7 @@ void update_highlighted(void) {
/*
* Read a text file a line at a time
* Returns number of chars in the line, or -1 if EOF.
* Returns number of chars in the line, or 0 if EOF.
* Expects Apple ][ style line endings (CR) and does no conversion
* fp - file to read from
* reset - if 1 then just reset the buffer and return
@ -635,7 +635,7 @@ void update_highlighted(void) {
* n - length of buffer. Longer lines will be truncated and terminated with CR.
* pos - position in file is updated via this pointer
*/
int16_t get_line(FILE *fp, uint8_t reset, char *writep, uint16_t n, uint32_t *pos) {
uint16_t get_line(FILE *fp, uint8_t reset, char *writep, uint16_t n, uint32_t *pos) {
static uint16_t rd = 0; // Read
static uint16_t end = 0; // End of valid data in buf
uint16_t i = 0;
@ -650,19 +650,19 @@ int16_t get_line(FILE *fp, uint8_t reset, char *writep, uint16_t n, uint32_t *po
rd = 0;
}
if (end == 0)
return -1; // EOF
goto done;
if (i == n - 1) {
writep[i - 1] = '\r';
writep[i] = '\0';
return i;
goto done;
}
writep[i++] = buf[rd++];
++(*pos);
if (writep[i - 1] == '\r') {
writep[i] = '\0';
return i;
}
if (writep[i - 1] == '\r')
goto done;
}
done:
writep[i] = '\0';
return i;
}
/*
@ -1066,7 +1066,7 @@ restart:
readp = linebuf;
if (!writep)
writep = linebuf;
if (get_line(fp, 0, writep, (LINEBUFSZ - (writep - linebuf)), &pos) == -1) {
if (get_line(fp, 0, writep, (LINEBUFSZ - (writep - linebuf)), &pos) == 0) {
eof = 1;
goto endscreen;
}
@ -1760,7 +1760,7 @@ void get_email_body(struct emailhdrs *h, FILE *f, char mode) {
readp = linebuf;
if (!writep)
writep = linebuf;
if (get_line(fp, 0, writep, (LINEBUFSZ - (writep - linebuf)), &pos) == -1)
if (get_line(fp, 0, writep, (LINEBUFSZ - (writep - linebuf)), &pos) == 0)
break;
if ((mime >= 1) && (!strncmp(writep, "--", 2))) {
if ((mime == 4) && !mime_binary) // End of Text/Plain MIME section

View File

@ -336,13 +336,13 @@ void readconfigfile(void) {
/*
* Read a text file a line at a time
* Returns number of chars in the line, or -1 if EOF.
* Returns number of chars in the line, or 0 if EOF.
* Expects CRLF line endings (CRLF) and converts to Apple II (CR) convention
* fp - file to read from
* writep - Pointer to buffer into which line will be written
* n - length of buffer. Longer lines will be truncated and terminated with CR.
*/
int16_t get_line(FILE *fp, char *writep, uint16_t n) {
uint16_t get_line(FILE *fp, char *writep, uint16_t n) {
static uint16_t rd = 0; // Read
static uint16_t end = 0; // End of valid data in buf
uint16_t i = 0;
@ -352,12 +352,11 @@ int16_t get_line(FILE *fp, char *writep, uint16_t n) {
rd = 0;
}
if (end == 0)
return -1; // EOF
goto done;
writep[i++] = buf[rd++];
if (i == n - 1) {
writep[i - 1] = '\r';
writep[i] = '\0';
return i;
goto done;
}
// The following line is safe because of linebuf_pad[]
if ((writep[i - 1] == '\n') && (writep[i - 2] == '\r')) {
@ -365,6 +364,9 @@ int16_t get_line(FILE *fp, char *writep, uint16_t n) {
return i - 1;
}
}
done:
writep[i] = '\0';
return i;
}
/*
@ -459,7 +461,7 @@ void update_mailbox(char *mbox) {
// Store News:newsgroup in TO field
strcpy(hdrs.to, "News:");
strcat(hdrs.to, newsgroup);
while ((chars = get_line(fp, linebuf, LINEBUFSZ)) != -1) {
while ((chars = get_line(fp, linebuf, LINEBUFSZ)) != 0) {
if (headers) {
headerchars += chars;
if (!strncmp(linebuf, "Date: ", 6)) {

View File

@ -110,14 +110,14 @@ void spinner(uint32_t sz, uint8_t final) {
/*
* Read a text file a line at a time
* Returns number of chars in the line, or -1 if EOF.
* Returns number of chars in the line, or 0 if EOF.
* Expects Apple ][ style line endings (CR) and does no conversion
* fp - file to read from
* reset - if 1 then just reset the buffer and return
* writep - Pointer to buffer into which line will be written
* n - length of buffer. Longer lines will be truncated and terminated with CR.
*/
int16_t get_line(FILE *fp, uint8_t reset, char *writep, uint16_t n) {
uint16_t get_line(FILE *fp, uint8_t reset, char *writep, uint16_t n) {
static uint16_t rd = 0; // Read
static uint16_t end = 0; // End of valid data in buf
uint16_t i = 0;
@ -131,18 +131,18 @@ int16_t get_line(FILE *fp, uint8_t reset, char *writep, uint16_t n) {
rd = 0;
}
if (end == 0)
return -1; // EOF
goto done;
if (i == n - 1) {
writep[i - 1] = '\r';
writep[i] = '\0';
return i;
goto done;
}
writep[i++] = buf[rd++];
if (writep[i - 1] == '\r') {
writep[i] = '\0';
return i;
}
if (writep[i - 1] == '\r')
goto done;
}
done:
writep[i] = '\0';
return i;
}
#define DO_SEND 1 // For do_send param
@ -176,10 +176,10 @@ bool w5100_tcp_send_recv(char* sendbuf, char* recvbuf, size_t length,
while (cont) {
len = get_line(fp, 0, linebuf, LINEBUFSZ);
len = get_line(fp, 0, linebuf, LINEBUFSZ - 1);
pos = 0;
if (len == -1) {
if (len == 0) {
strcpy(linebuf, "\r\n.\r\n");
len = 5;
cont = 0;
@ -465,7 +465,7 @@ void update_sent_mbox(char *name) {
hdrs.status = 'N';
hdrs.tag = ' ';
get_line(fp, 1, linebuf, LINEBUFSZ); // Reset buffer
while ((chars = get_line(fp, 0, linebuf, LINEBUFSZ)) != -1) {
while ((chars = get_line(fp, 0, linebuf, LINEBUFSZ)) != 0) {
if (headers) {
headerchars += chars;
if (!strncmp(linebuf, "Date: ", 6)) {
@ -575,7 +575,7 @@ void main(int argc, char *argv[]) {
linecount = 0;
while (1) {
if ((get_line(fp, 0, linebuf, LINEBUFSZ) == -1) || (linecount == 20))
if ((get_line(fp, 0, linebuf, LINEBUFSZ) == 0) || (linecount == 20))
break;
++linecount;
if (!strncmp(linebuf, "Newsgroups: ", 12))

View File

@ -330,13 +330,13 @@ void readconfigfile(void) {
/*
* Read a text file a line at a time
* Returns number of chars in the line, or -1 if EOF.
* Returns number of chars in the line, or 0 if EOF.
* Expects CRLF line endings (CRLF) and converts to Apple II (CR) convention
* fp - file to read from
* writep - Pointer to buffer into which line will be written
* n - length of buffer. Longer lines will be truncated and terminated with CR.
*/
int16_t get_line(FILE *fp, char *writep, uint16_t n) {
uint16_t get_line(FILE *fp, char *writep, uint16_t n) {
static uint16_t rd = 0; // Read
static uint16_t end = 0; // End of valid data in buf
uint16_t i = 0;
@ -346,11 +346,10 @@ int16_t get_line(FILE *fp, char *writep, uint16_t n) {
rd = 0;
}
if (end == 0)
return -1; // EOF
goto done;
if (i == n - 1) {
writep[i - 1] = '\r';
writep[i] = '\0';
return i;
goto done;
}
writep[i++] = buf[rd++];
// The following line is safe because of linebuf_pad[]
@ -359,6 +358,9 @@ int16_t get_line(FILE *fp, char *writep, uint16_t n) {
return i - 1;
}
}
done:
writep[i] = '\0';
return i;
}
/*
@ -451,7 +453,7 @@ void update_inbox(uint16_t nummsgs) {
hdrs.skipbytes = 0; // Just in case it doesn't get set
hdrs.status = 'N';
hdrs.tag = ' ';
while ((chars = get_line(fp, linebuf, LINEBUFSZ)) != -1) {
while ((chars = get_line(fp, linebuf, LINEBUFSZ)) != 0) {
if (headers) {
headerchars += chars;
if (!strncmp(linebuf, "Date: ", 6)) {

View File

@ -45,13 +45,13 @@ void error_exit() {
/*
* Read a text file a line at a time
* Returns number of chars in the line, or -1 if EOF.
* Returns number of chars in the line, or 0 if EOF.
* Expects Apple ][ style line endings (CR) and does no conversion
* fp - file to read from
* writep - Pointer to buffer into which line will be written
* n - length of buffer. Longer lines will be truncated and terminated with CR.
*/
int16_t get_line(FILE *fp, char *writep, uint16_t n) {
uint16_t get_line(FILE *fp, char *writep, uint16_t n) {
static uint16_t rd = 0; // Read
static uint16_t end = 0; // End of valid data in buf
uint16_t i = 0;
@ -61,18 +61,18 @@ int16_t get_line(FILE *fp, char *writep, uint16_t n) {
rd = 0;
}
if (end == 0)
return -1; // EOF
goto done;
if (i == n - 1) {
writep[i - 1] = '\r';
writep[i] = '\0';
return i;
goto done;
}
writep[i++] = buf[rd++];
if (writep[i - 1] == '\r') {
writep[i] = '\0';
return i;
}
if (writep[i - 1] == '\r')
goto done;
}
done:
writep[i] = '\0';
return i;
}
/*
@ -193,7 +193,7 @@ void repair_mailbox(void) {
hdrs.skipbytes = 0; // Just in case it doesn't get set
hdrs.status = 'R';
hdrs.tag = ' ';
while ((chars = get_line(fp, linebuf, LINEBUFSZ)) != -1) {
while ((chars = get_line(fp, linebuf, LINEBUFSZ)) != 0) {
if (headers) {
headerchars += chars;
if (!strncmp(linebuf, "Date: ", 6)) {

View File

@ -110,14 +110,14 @@ void spinner(uint32_t sz, uint8_t final) {
/*
* Read a text file a line at a time
* Returns number of chars in the line, or -1 if EOF.
* Returns number of chars in the line, or 0 if EOF.
* Expects Apple ][ style line endings (CR) and does no conversion
* fp - file to read from
* reset - if 1 then just reset the buffer and return
* writep - Pointer to buffer into which line will be written
* n - length of buffer. Longer lines will be truncated and terminated with CR.
*/
int16_t get_line(FILE *fp, uint8_t reset, char *writep, uint16_t n) {
uint16_t get_line(FILE *fp, uint8_t reset, char *writep, uint16_t n) {
static uint16_t rd = 0; // Read
static uint16_t end = 0; // End of valid data in buf
uint16_t i = 0;
@ -131,18 +131,18 @@ int16_t get_line(FILE *fp, uint8_t reset, char *writep, uint16_t n) {
rd = 0;
}
if (end == 0)
return -1; // EOF
goto done;
if (i == n - 1) {
writep[i - 1] = '\r';
writep[i] = '\0';
return i;
goto done;
}
writep[i++] = buf[rd++];
if (writep[i - 1] == '\r') {
writep[i] = '\0';
return i;
}
if (writep[i - 1] == '\r')
goto done;
}
done:
writep[i] = '\0';
return i;
}
#define DO_SEND 1 // For do_send param
@ -169,17 +169,17 @@ bool w5100_tcp_send_recv(char* sendbuf, char* recvbuf, size_t length,
uint16_t pos = 0;
uint8_t cont = 1;
uint16_t snd;
int16_t len;
uint16_t len;
filesize = 0;
len = get_line(fp, 1, linebuf, LINEBUFSZ); // Reset buffer
while (cont) {
len = get_line(fp, 0, linebuf, LINEBUFSZ);
len = get_line(fp, 0, linebuf, LINEBUFSZ - 1);
pos = 0;
if (len == -1) {
if (len == 0) {
strcpy(linebuf, "\r\n.\r\n");
len = 5;
cont = 0;
@ -464,7 +464,7 @@ void update_sent_mbox(char *name) {
hdrs.status = 'N';
hdrs.tag = ' ';
get_line(fp, 1, linebuf, LINEBUFSZ); // Reset buffer
while ((chars = get_line(fp, 0, linebuf, LINEBUFSZ)) != -1) {
while ((chars = get_line(fp, 0, linebuf, LINEBUFSZ)) != 0) {
if (headers) {
headerchars += chars;
if (!strncmp(linebuf, "Date: ", 6)) {
@ -575,7 +575,7 @@ void main(int argc, char *argv[]) {
strcpy(recipients, "");
while (1) {
if ((get_line(fp, 0, linebuf, LINEBUFSZ) == -1) || (linecount == 20)) {
if ((get_line(fp, 0, linebuf, LINEBUFSZ) == 0) || (linecount == 20)) {
if (strlen(recipients) == 0) {
printf("No recipients (To or Cc) in %s. Skipping msg.\n", d->d_name);
goto skiptonext;