diff --git a/src/main.c b/src/main.c index 81bdba3..26c4a0b 100644 --- a/src/main.c +++ b/src/main.c @@ -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]); diff --git a/src/mbox.c b/src/mbox.c index 32b2bfc..ba119ed 100644 --- a/src/mbox.c +++ b/src/mbox.c @@ -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; diff --git a/src/mbox.h b/src/mbox.h index e739180..4776383 100644 --- a/src/mbox.h +++ b/src/mbox.h @@ -19,9 +19,7 @@ #ifndef MBOX_H # define MBOX_H 1 - # include - # 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 */ diff --git a/src/scan.c b/src/scan.c index 85f19f9..69ff4fd 100644 --- a/src/scan.c +++ b/src/scan.c @@ -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);