Code refactoring

Partial refactor of mbox_open(). Opening of file descripts has been
moved to mbox_fdopen().
This commit is contained in:
Daniel Spiljar 2024-09-20 13:11:06 +02:00
parent bd64536e89
commit 631998a849
Signed by: dspiljar
GPG Key ID: A32CE9C59D8003B5
2 changed files with 39 additions and 43 deletions

View File

@ -74,11 +74,6 @@ mbox_open (const char *path, const mbox_mode_t mbox_mode)
if (fd == -1) if (fd == -1)
{ {
if (config.merr)
{
fprintf (stderr, "%s: %s: ", APPNAME, path);
perror (NULL);
}
errno = 0; errno = 0;
return NULL; return NULL;
} }
@ -86,44 +81,7 @@ mbox_open (const char *path, const mbox_mode_t mbox_mode)
if (config.lock > LOCK_NONE) if (config.lock > LOCK_NONE)
mbox_lock (fd, path, mbox_mode); mbox_lock (fd, path, mbox_mode);
if (mbox_mode == r) mp->fp = mbox_fdopen (fd, path, mbox_mode);
{
if (config.format == FORMAT_MBOX)
mp->fp = (FILE *) m_fdopen (fd, "r");
#ifdef HAVE_LIBZ
else if (config.format == FORMAT_ZMBOX)
mp->fp = (gzFile *) m_gzdopen (fd, "rb");
#endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2
else if (config.format == FORMAT_BZ2MBOX)
mp->fp = (BZFILE *) BZ2_bzdopen (fd, "rb");
#endif /* HAVE_LIBBZ2 */
}
else if (mbox_mode == w)
{
if (config.format == FORMAT_MBOX)
mp->fp = (FILE *) m_fdopen (fd, "w");
#ifdef HAVE_LIBZ
else if (config.format == FORMAT_ZMBOX)
mp->fp = (gzFile *) m_gzdopen (fd, "wb");
#endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2
else if (config.format == FORMAT_BZ2MBOX)
mp->fp = (BZFILE *) BZ2_bzdopen (fd, "wb");
#endif /* HAVE_LIBBZ2 */
}
if (mp->fp == NULL)
{
if (config.merr)
{
fprintf (stderr, "%s: %s: ", APPNAME, path);
perror (NULL);
}
errno = 0;
close (fd);
return NULL;
}
} }
memset (buffer, 0, BUFSIZ); memset (buffer, 0, BUFSIZ);
@ -429,3 +387,40 @@ mbox_lock (int fd, const char *path, const mbox_mode_t mbox_mode)
} }
} }
} }
void *
mbox_fdopen (int fd, const char *path, const mbox_mode_t mbox_mode)
{
void *f;
char *file_mode;
if (mbox_mode == w)
file_mode = xstrdup("wb");
else
file_mode = xstrdup("rb");
if (config.format == FORMAT_MBOX)
f = (FILE *) m_fdopen (fd, file_mode);
#ifdef HAVE_LIBZ
else if (config.format == FORMAT_ZMBOX)
f = (gzFile *) m_gzdopen (fd, file_mode);
#endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2
else if (config.format == FORMAT_BZ2MBOX)
f = (BZFILE *) BZ2_bzdopen (fd, file_mode);
#endif /* HAVE_LIBBZ2 */
if (f == NULL)
{
if (config.merr)
{
fprintf (stderr, "%s: %s: ", APPNAME, path);
perror (NULL);
}
errno = 0;
close (fd);
return NULL;
}
return f;
}

View File

@ -45,5 +45,6 @@ void mbox_close (mbox_t * mbp);
message_t *mbox_read_message (mbox_t * mp); message_t *mbox_read_message (mbox_t * mp);
void mbox_write_message (message_t * m, mbox_t * mbox); void mbox_write_message (message_t * m, mbox_t * mbox);
void mbox_lock (int fd, const char *path, const mbox_mode_t mbox_mode); void mbox_lock (int fd, const char *path, const mbox_mode_t mbox_mode);
void *mbox_fdopen (int fd, const char *path, const mbox_mode_t mbox_mode);
#endif /* MBOX_H */ #endif /* MBOX_H */