Commit 400fdaac 400fdaaca90616a996f9a6fd230eb084aebb1bb8 by Sergey Poznyakoff

amd, pop3d: bugfixes.

* mailbox/amd.c (amd_body_stream_readdelim): Fix the logic.
* mailbox/message.c (_message_stream_seek): Fix seeks in
backward direction.
* pop3d/top.c: Fix output of the body.
1 parent c1d1ab07
......@@ -1648,7 +1648,6 @@ amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen,
mu_body_t body = amdstr->body;
mu_message_t msg = mu_body_get_owner (body);
struct _amd_message *mhm = mu_message_get_owner (msg);
size_t nread = 0;
int status = 0;
amd_pool_open (mhm);
......@@ -1656,7 +1655,7 @@ amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen,
if (buffer == NULL || buflen == 0)
{
if (pnread)
*pnread = nread;
*pnread = 0;
return 0;
}
......@@ -1669,27 +1668,22 @@ amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen,
status = mu_stream_seek (mhm->stream, mhm->body_start + amdstr->off,
MU_SEEK_SET, NULL);
if (status)
{
buflen--;
while (buflen)
if (status == 0)
{
size_t ln, rdsize;
size_t nread = 0;
size_t ln;
ln = mhm->body_end - (mhm->body_start + amdstr->off);
ln = mhm->body_end - (mhm->body_start + amdstr->off) + 1;
if (ln > 0)
{
rdsize = ((size_t)ln < buflen) ? (size_t)ln : buflen;
size_t rdsize = ((size_t)ln < buflen) ? (size_t)ln : buflen;
status = mu_stream_readdelim (mhm->stream, buffer, rdsize,
delim, &rdsize);
amdstr->off += nread;
nread += rdsize;
if (status)
break;
buflen -= rdsize;
buffer += rdsize;
}
delim, &nread);
amdstr->off += rdsize;
}
if (pnread)
*pnread = nread;
}
mu_monitor_unlock (mhm->amd->mailbox->monitor);
......@@ -1697,8 +1691,6 @@ amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen,
pthread_cleanup_pop (0);
#endif
if (pnread)
*pnread = nread;
return status;
}
......
......@@ -182,7 +182,17 @@ _message_stream_seek (struct _mu_stream *str, mu_off_t off, mu_off_t *ppos)
return rc;
/* fall through */
case _mss_body:
if (off > hsize)
off -= hsize;
else
{
mu_stream_destroy (&sp->transport);
sp->state = _mss_init;
rc = _check_stream_state (sp);
if (rc)
return rc;
}
break;
default:
......
......@@ -71,13 +71,13 @@ pop3d_top (char *arg)
{
char *buf = NULL;
size_t size = 0, n;
while (lines > 0 &&
for (; lines > 0 &&
mu_stream_getline (stream, &buf, &size, &n) == 0 &&
n > 0)
n > 0; lines--)
{
if (buf[0] == '.')
pop3d_outf (".");
pop3d_outf ("%s\n", buf);
pop3d_outf ("%s", buf);
}
mu_stream_destroy (&stream);
free (buf);
......