Commit d58c35e4 d58c35e44f56d769ee56385856f95768c4d8fb83 by Jakob Kaivo

Added cleanup patches from Shaleh

1 parent 2ee4719d
......@@ -23,14 +23,22 @@
#endif
int _mbox_dummy1 (mailbox * mbox);
int _mbox_dummy2 (mailbox * mbox, int num);
int _mbox_dummy2 (mailbox * mbox, unsigned int num);
int _mbox_dummy3 (mailbox * mbox, char *c);
char *_mbox_dummy4 (mailbox * mbox, int num);
char *_mbox_dummy4 (mailbox * mbox, unsigned int num);
mailbox *
mbox_open (const char *name)
{
mailbox *mbox = malloc (sizeof (mailbox));
mailbox *mbox;
if ( name == NULL )
{
errno = EINVAL;
return NULL;
}
mbox = malloc (sizeof (mailbox));
if (mbox == NULL)
{
errno = ENOMEM;
......@@ -87,18 +95,28 @@ mbox_open (const char *name)
* Gets the contents of a header field
*/
char *
mbox_header_line (mailbox *mbox, int num, const char *header)
mbox_header_line (mailbox *mbox, unsigned int num, const char *header)
{
char *full, *tmp, *line;
int i = 0, j=0, try = 1, len;
int lh = strlen (header);
int i = 0, j = 0, try = 1;
unsigned int len, lh;
if ( mbox == NULL || header == NULL )
{
errno = EINVAL;
return NULL;
}
full = mbox_get_header (mbox, num);
if (full == NULL)
return NULL;
lh = strlen (header);
len = strlen (full);
tmp = NULL;
/* First get the appropriate line at the beginning */
/* FIXME: hmm len - (lh + 2) *COULD* be negative, but should never be */
for (i=0; i < len-(lh+2); i++)
{
if (try == 1)
......@@ -107,6 +125,11 @@ mbox_header_line (mailbox *mbox, int num, const char *header)
{
full[len-i] = '\0';
tmp = strdup (&full[i+lh+2]);
if (tmp == NULL)
{
free(full);
return NULL;
}
i = len;
}
else
......@@ -118,6 +141,12 @@ mbox_header_line (mailbox *mbox, int num, const char *header)
try = 0;
}
/* FIXME: hmm, no valid header found, what should errno be? */
if (tmp == NULL)
{
free(full);
return NULL;
}
/* Now trim the fat */
len = strlen (tmp);
for (i = 0; i < len; i++)
......@@ -145,11 +174,16 @@ mbox_header_line (mailbox *mbox, int num, const char *header)
* Gets first LINES lines from message body
*/
char *
mbox_body_lines (mailbox *mbox, int num, int lines)
mbox_body_lines (mailbox *mbox, unsigned int num, unsigned int lines)
{
char *full, *buf = NULL;
int i=0, line = 0, len;
if (lines < 1)
if (mbox == NULL)
{
errno = EINVAL;
return NULL;
}
if (lines == 0)
return strdup ("");
full = mbox_get_body (mbox, num);
if (full == NULL)
......@@ -185,7 +219,7 @@ _mbox_dummy1 (mailbox * mbox)
}
int
_mbox_dummy2 (mailbox * mbox, int num)
_mbox_dummy2 (mailbox * mbox, unsigned int num)
{
return _mbox_dummy1 (mbox);
}
......@@ -199,7 +233,7 @@ _mbox_dummy3 (mailbox * mbox, char *c)
* Bogus function for unimplemented functions that return char *
*/
char *
_mbox_dummy4 (mailbox * mbox, int num)
_mbox_dummy4 (mailbox * mbox, unsigned int num)
{
errno = ENOSYS;
return NULL;
......
......@@ -46,28 +46,28 @@ typedef struct _mailbox
{
/* Data */
char *name;
int messages;
int num_deleted;
int *sizes;
unsigned int messages;
unsigned int num_deleted;
unsigned int *sizes;
void *_data;
/* Functions */
int (*_close) __P ((struct _mailbox *));
int (*_delete) __P ((struct _mailbox *, int));
int (*_undelete) __P ((struct _mailbox *, int));
int (*_delete) __P ((struct _mailbox *, unsigned int));
int (*_undelete) __P ((struct _mailbox *, unsigned int));
int (*_expunge) __P ((struct _mailbox *));
int (*_add_message) __P ((struct _mailbox *, char *));
int (*_is_deleted) __P ((struct _mailbox *, int));
int (*_is_deleted) __P ((struct _mailbox *, unsigned int));
int (*_lock) __P((struct _mailbox *, int));
char *(*_get_body) __P ((struct _mailbox *, int));
char *(*_get_header) __P ((struct _mailbox *, int));
char *(*_get_body) __P ((struct _mailbox *, unsigned int));
char *(*_get_header) __P ((struct _mailbox *, unsigned int));
}
mailbox;
mailbox *mbox_open __P ((const char *name));
char *mbox_header_line __P ((mailbox *mbox, int num, const char *header));
char *mbox_body_lines __P ((mailbox *mbox, int num, int lines));
char *mbox_header_line __P ((mailbox *mbox, unsigned int num, const char *header));
char *mbox_body_lines __P ((mailbox *mbox, unsigned int num, unsigned int lines));
#endif
......
......@@ -26,8 +26,15 @@ unixmbox_open (mailbox * mbox)
char buf[80];
unsigned int max_count = 10;
int mess = 0;
unixmbox_data *data = malloc (sizeof (unixmbox_data));
unixmbox_data *data;
if (mbox == NULL)
{
errno = EINVAL;
return -1;
}
data = malloc (sizeof (unixmbox_data));
if (data == NULL)
{
errno = ENOMEM;
......@@ -61,16 +68,14 @@ unixmbox_open (mailbox * mbox)
errno = 0;
return -1;
}
else
rewind (data->file);
while (fgets (buf, 80, data->file))
do
{
if (!strncmp (buf, "From ", 5))
{
/* Beginning of a header */
while (strchr (buf, '\n') == NULL)
fgets (buf, 80, data->file);
fgets (buf, 80, data->file); /* eat the From line */
mbox->messages++;
......@@ -106,6 +111,7 @@ unixmbox_open (mailbox * mbox)
fgetpos (data->file, &(data->messages[mbox->messages - 1].end));
}
}
while (fgets (buf, 80, data->file));
mbox->_close = unixmbox_close;
mbox->_delete = unixmbox_delete;
......@@ -126,7 +132,14 @@ unixmbox_open (mailbox * mbox)
int
unixmbox_close (mailbox * mbox)
{
unixmbox_data *data = mbox->_data;
unixmbox_data *data;
if (mbox == NULL)
{
errno = EINVAL;
return -1;
}
data = mbox->_data;
unixmbox_lock (mbox, MO_ULOCK);
fclose (data->file);
free (data->messages);
......@@ -140,19 +153,26 @@ unixmbox_close (mailbox * mbox)
* Marks a message for deletion
*/
int
unixmbox_delete (mailbox * mbox, int num)
unixmbox_delete (mailbox * mbox, unsigned int num)
{
unixmbox_data *data = mbox->_data;
if (num > mbox->messages || mbox_is_deleted (mbox, num))
unixmbox_data *data;
if (mbox == NULL)
{
errno = ERANGE;
errno = EINVAL;
return -1;
}
else
if (num > mbox->messages || mbox_is_deleted (mbox, num))
{
data->messages[num].deleted = 1;
mbox->num_deleted++;
errno = ERANGE;
return -1;
}
data = mbox->_data;
data->messages[num].deleted = 1;
mbox->num_deleted++;
return 0;
}
......@@ -160,19 +180,26 @@ unixmbox_delete (mailbox * mbox, int num)
* Unmark a message for deletion
*/
int
unixmbox_undelete (mailbox * mbox, int num)
unixmbox_undelete (mailbox * mbox, unsigned int num)
{
unixmbox_data *data = mbox->_data;
if (num > mbox->messages || !mbox_is_deleted (mbox, num))
unixmbox_data *data;
if (mbox == NULL)
{
errno = ERANGE;
errno = EINVAL;
return -1;
}
else
if (num > mbox->messages || !mbox_is_deleted (mbox, num))
{
data->messages[num].deleted = 0;
mbox->num_deleted--;
errno = ERANGE;
return -1;
}
data = mbox->_data;
data->messages[num].deleted = 0;
mbox->num_deleted--;
return 0;
}
......@@ -183,11 +210,17 @@ unixmbox_undelete (mailbox * mbox, int num)
int
unixmbox_expunge (mailbox * mbox)
{
unixmbox_data *data = mbox->_data;
unixmbox_data *data;
int i = 0, size = 0;
char *buf;
char *buf = NULL;
fpos_t lastpos;
if (mbox == NULL)
{
errno = EINVAL;
return -1;
}
data = mbox->_data;
data->file = freopen (mbox->name, "r+", data->file);
if (data->file == NULL)
{
......@@ -216,9 +249,16 @@ unixmbox_expunge (mailbox * mbox)
* Determines whether or a not a message is marked for deletion
*/
int
unixmbox_is_deleted (mailbox * mbox, int num)
unixmbox_is_deleted (mailbox * mbox, unsigned int num)
{
unixmbox_data *data = mbox->_data;
unixmbox_data *data;
if (mbox == NULL)
{
errno = EINVAL;
return -1;
}
data = mbox->_data;
return (data->messages[num].deleted == 1);
}
......@@ -241,21 +281,29 @@ unixmbox_add_message (mailbox * mbox, char *message)
* Returns a message body
*/
char *
unixmbox_get_body (mailbox * mbox, int num)
unixmbox_get_body (mailbox * mbox, unsigned int num)
{
unixmbox_data *data = mbox->_data;
int size = data->messages[num].end - data->messages[num].body;
char *buf = malloc ((1 + size) * sizeof (char));
if (buf == NULL)
unixmbox_data *data;
unsigned int size;
char *buf;
if (mbox == NULL)
{
errno = ENOMEM;
free (buf);
return NULL;
errno = EINVAL;
return NULL;
}
else if (num > mbox->messages || num < 0)
if (num > mbox->messages || num < 0)
{
errno = ERANGE;
free (buf);
return NULL;
}
data = mbox->_data;
size = data->messages[num].end - data->messages[num].body;
buf = malloc ((1 + size) * sizeof (char));
if (buf == NULL)
{
errno = ENOMEM;
return NULL;
}
......@@ -269,25 +317,35 @@ unixmbox_get_body (mailbox * mbox, int num)
* Returns just the header of a message
*/
char *
unixmbox_get_header (mailbox * mbox, int num)
unixmbox_get_header (mailbox * mbox, unsigned int num)
{
unixmbox_data *data = mbox->_data;
int size = (data->messages[num].body - 1) - data->messages[num].header ;
char *buf = malloc ((1 + size) * sizeof (char));
if (buf == NULL)
unixmbox_data *data;
unsigned int size;
char *buf;
if ( mbox == NULL )
{
errno = ENOMEM;
free (buf);
return NULL;
errno = EINVAL;
return NULL;
}
else if (num > mbox->messages || num < 0)
if (num > mbox->messages)
{
errno = ERANGE;
free (buf);
return NULL;
}
data = mbox->_data;
size = (data->messages[num].body - 1) - data->messages[num].header;
buf = malloc ((1 + size) * sizeof (char));
if (buf == NULL)
{
errno = ENOMEM;
return NULL;
}
memset (buf, 0, size + 1);
fsetpos (data->file, &(data->messages[num].header));
fread (buf, size, sizeof (char), data->file);
return buf;
......
......@@ -61,13 +61,13 @@ unixmbox_data;
int unixmbox_open (mailbox *mbox);
int unixmbox_close (mailbox *mbox);
int unixmbox_delete (mailbox *mbox, int num);
int unixmbox_undelete (mailbox *mbox, int num);
int unixmbox_delete (mailbox *mbox, unsigned int num);
int unixmbox_undelete (mailbox *mbox, unsigned int num);
int unixmbox_expunge (mailbox *mbox);
int unixmbox_is_deleted (mailbox *mbox, int num);
int unixmbox_is_deleted (mailbox *mbox, unsigned int num);
int unixmbox_lock (mailbox *mbox, int mode);
int unixmbox_add_message (mailbox *mbox, char *message);
char *unixmbox_get_body (mailbox *mbox, int num);
char *unixmbox_get_header (mailbox *mbox, int num);
char *unixmbox_get_body (mailbox *mbox, unsigned int num);
char *unixmbox_get_header (mailbox *mbox, unsigned int num);
#endif
......