Re: [PATCH v2] Let mailsplit and mailinfo handle mails with CRLF line-endings
- From
Alex Riesen <raa.lkml@gmail.com>
- Date
- Aug 4, 2009, 21:58 UTC
- Message-ID
- <81b0412b0908041458w7e76854bn96c4cb71a5e91e74@mail.gmail.com>
- In-Reply-To
- <H9KDJzU0XgndJHC8J4OETn7kxYhGP3gl0YJPR0low0hfIHjZOUsoxw@cipher.nrlssc.navy.mil>
On Tue, Aug 4, 2009 at 23:29, Brandon Casey<brandon.casey.ctr@nrlssc.navy.mil> wrote:
Show 9 quoted lines
> Alex Riesen wrote: >> + if (c == '\n' && len > 1 && buf[len - 2] == '\r') >> + buf[--len - 1] = '\n'; >> buf[len] = '\0'; >> >> return len; > > What if \r lands at character 99 and \n is at character 100? If buf has > exactly 100 characters available for writing. ...
Ah, yes. You're right.
I have strong dislike towards unget, though. How about this, instead:
int read_line_with_nul(char *buf, int size, FILE *in)
{
int len = 0, c; while (len < size) {
c = getc(in);
if (c == EOF)
break;
buf[len++] = c;
if (c == '\n')
break;
else if (len == size)
c = 0;
}
if (c == '\n' && len > 1 && buf[len - 2] == '\r')
buf[--len - 1] = '\n';
buf[len] = '\0';return len; }