Function prototype

Use an enum type instead of a string to define the file opening mode.
This commit is contained in:
Daniel Spiljar 2024-09-19 22:40:20 +02:00
parent b30f2f2362
commit bd64536e89
Signed by: dspiljar
GPG Key ID: A32CE9C59D8003B5
4 changed files with 22 additions and 24 deletions

View File

@ -1,6 +1,6 @@
/*
mboxgrep - scan mailbox for messages matching a regular expression
Copyright (C) 2000 - 2004, 2006, 2023 Daniel Spiljar
Copyright (C) 2000 - 2004, 2006, 2023 - 2024 Daniel Spiljar
Mboxgrep is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
@ -117,7 +117,7 @@ main (int argc, char **argv)
if (config.action == ACTION_DELETE)
{
tmpmbox_create (argv[optind]);
runtime.tmp_mbox = (mbox_t *) mbox_open (config.tmpfilename, "w");
runtime.tmp_mbox = (mbox_t *) mbox_open (config.tmpfilename, w);
}
config.boxname = xstrdup (argv[optind]);

View File

@ -50,7 +50,7 @@
#endif /* HAVE_LIBDMALLOC */
mbox_t *
mbox_open (const char *path, const char *mode)
mbox_open (const char *path, const mbox_mode_t mbox_mode)
{
mbox_t *mp;
static int fd;
@ -66,17 +66,11 @@ mbox_open (const char *path, const char *mode)
mp->fp = stdin;
else
{
if (mode[0] == 'r')
fd = m_open (path, O_RDONLY, 0);
else if (mode[0] == 'w')
if (mbox_mode == w)
fd = m_open (path, (O_WRONLY | O_CREAT | O_APPEND),
(S_IWUSR | S_IRUSR));
else
{
fprintf (stderr, "%s: mbox.c: Unknown mode %c. You shouldn't "
"get this error...", APPNAME, mode[0]);
exit (2);
}
fd = m_open (path, O_RDONLY, 0);
if (fd == -1)
{
@ -90,9 +84,9 @@ mbox_open (const char *path, const char *mode)
}
if (config.lock > LOCK_NONE)
mbox_lock (fd, path, mode);
mbox_lock (fd, path, mbox_mode);
if (mode[0] == 'r')
if (mbox_mode == r)
{
if (config.format == FORMAT_MBOX)
mp->fp = (FILE *) m_fdopen (fd, "r");
@ -105,7 +99,7 @@ mbox_open (const char *path, const char *mode)
mp->fp = (BZFILE *) BZ2_bzdopen (fd, "rb");
#endif /* HAVE_LIBBZ2 */
}
else if (mode[0] == 'w')
else if (mbox_mode == w)
{
if (config.format == FORMAT_MBOX)
mp->fp = (FILE *) m_fdopen (fd, "w");
@ -134,7 +128,7 @@ mbox_open (const char *path, const char *mode)
memset (buffer, 0, BUFSIZ);
if (mode[0] == 'r')
if (mbox_mode == r)
{
if (config.format == FORMAT_MBOX)
fgets (buffer, BUFSIZ, mp->fp);
@ -406,12 +400,12 @@ tmpfile_create (void)
}
void
mbox_lock (int fd, const char *path, const char *mode)
mbox_lock (int fd, const char *path, const mbox_mode_t mbox_mode)
{
#ifdef HAVE_FLOCK
int op;
if (mode[0] == 'r')
if (mbox_mode == r)
op = LOCK_SH;
else
op = LOCK_EX;

View File

@ -19,9 +19,7 @@
#ifndef MBOX_H
# define MBOX_H 1
# include <config.h>
# include "message.h"
typedef struct
@ -31,8 +29,14 @@ typedef struct
char *postmark_cache;
} mbox_t;
typedef enum
{
r,
w,
} mbox_mode_t;
mbox_t *mbox_open (const char *path, const char *mode);
mbox_t *mbox_open (const char *path, const mbox_mode_t mbox_mode);
void tmpmbox_create (const char *path);
void tmpfile_name (const char *path);
void tmpfile_mod_own (const int fd, const char *path);
@ -40,6 +44,6 @@ int tmpfile_create (void);
void mbox_close (mbox_t * mbp);
message_t *mbox_read_message (mbox_t * mp);
void mbox_write_message (message_t * m, mbox_t * mbox);
void mbox_lock (int fd, const char *path, const char *mode);
void mbox_lock (int fd, const char *path, const mbox_mode_t mbox_mode);
#endif /* MBOX_H */

View File

@ -1,6 +1,6 @@
/*
mboxgrep - scan mailbox for messages matching a regular expression
Copyright (C) 2000 - 2004, 2006, 2023 Daniel Spiljar
Copyright (C) 2000 - 2004, 2006, 2023 - 2024 Daniel Spiljar
Mboxgrep is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
@ -107,7 +107,7 @@ scan_mailbox (char path[])
if ((config.format == FORMAT_MBOX) || (config.format == FORMAT_ZMBOX)
|| (config.format == FORMAT_BZ2MBOX))
{
mbox = (mbox_t *) mbox_open (path, "r");
mbox = (mbox_t *) mbox_open (path, r);
if (mbox == NULL)
return;
}
@ -179,7 +179,7 @@ scan_mailbox (char path[])
else if ((config.format == FORMAT_MBOX) || (config.format == FORMAT_ZMBOX)
|| (config.format == FORMAT_BZ2MBOX))
{
out = mbox_open (config.outboxname, "w");
out = mbox_open (config.outboxname, w);
/* fprintf (out->fp, "%s\n%s", msg->headers, msg->body); */
mbox_write_message (msg, out);
mbox_close (out);