summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: f058adf)
raw | patch | inline | side by side (parent: f058adf)
author | Sebastian Harl <sh@tokkee.org> | |
Fri, 8 Dec 2006 14:13:26 +0000 (15:13 +0100) | ||
committer | Florian Forster <octo@huhu.verplant.org> | |
Fri, 8 Dec 2006 14:29:34 +0000 (15:29 +0100) |
The read_line () function now uses the provided buffer to save the return
value to. In case no complete line could be read, read_line () is called
recursively until '\n' has been found or end-of-file has been reached.
Signed-off-by: Sebastian Harl <sh@tokkee.org>
value to. In case no complete line could be read, read_line () is called
recursively until '\n' has been found or end-of-file has been reached.
Signed-off-by: Sebastian Harl <sh@tokkee.org>
src/email.c | patch | blob | history |
diff --git a/src/email.c b/src/email.c
index 401ac928108ec6db4e7b8b1067de20c2dbea27ef..377a6b1ea6a5c6faac4fb3462b60fe6f8731e95c 100644 (file)
--- a/src/email.c
+++ b/src/email.c
/* buffer to read data to */
char *buffer;
- int idx; /* current position in buffer */
+ int idx; /* current write position in buffer */
+ int length; /* length of the current line, i.e. index of '\0' */
struct conn *next;
} conn_t;
/* Read a single line (terminated by '\n') from the the socket.
*
* The return value is zero terminated and does not contain any newline
- * characters. In case that no complete line is available (non-blocking mode
- * should be enabled) an empty string is returned.
+ * characters.
*
* If an error occurs or end-of-file is reached return NULL.
*
* acceptable in this case ;-) */
char *read_line (conn_t *src)
{
- int i = 0;
- char *ret;
+ int i = 0;
+
+ assert ((BUFSIZE >= src->idx) && (src->idx >= 0));
+ assert ((src->idx > src->length) || (src->length == 0));
- assert (BUFSIZE > src->idx);
+ if (src->length > 0) { /* remove old line */
+ src->idx -= (src->length + 1);
+ memmove (src->buffer, src->buffer + src->length + 1, src->idx);
+ src->length = 0;
+ }
for (i = 0; i < src->idx; ++i) {
if ('\n' == src->buffer[i])
}
if (i == src->idx) {
- ret = (char *)smalloc (1);
-
- ret[0] = '\0';
+ src->length = 0;
if (BUFSIZE == src->idx) { /* no space left in buffer */
while ('\n' != read_char (src))
src->idx = 0;
}
- return ret;
+ return read_line (src);
}
}
- ret = (char *)smalloc (i + 1);
- memcpy (ret, src->buffer, i + 1);
- ret[i] = '\0';
-
- src->idx -= (i + 1);
+ src->buffer[i] = '\0';
+ src->length = i;
- if (0 == src->idx)
- src->buffer[0] = '\0';
- else
- memmove (src->buffer, src->buffer + i + 1, src->idx);
- return ret;
+ return src->buffer;
} /* char *read_line (conn_t *) */
static void *collect (void *arg)
connection->buffer = buffer;
connection->idx = 0;
+ connection->length = 0;
{ /* put the socket in non-blocking mode */
int flags = 0;
break;
}
- if ('\0' == line[0]) {
- free (line);
- continue;
- }
-
if (':' != line[1]) {
syslog (LOG_ERR, "email: syntax error in line '%s'", line);
- free (line);
continue;
}
if (NULL == tmp) {
syslog (LOG_ERR, "email: syntax error in line '%s'", line);
- free (line);
continue;
}
else {
syslog (LOG_ERR, "email: unknown type '%c'", line[0]);
}
-
- free (line);
} /* while (loop) */
close (connection->socket);