Compare commits

..

No commits in common. "9515236244c96158f6b18f516bf37a40b896eecc" and "ec8062309d784aa62a506d1b19ccc8c6abba28a2" have entirely different histories.

4 changed files with 140 additions and 152 deletions

View File

@ -1,6 +1,6 @@
/* /*
mboxgrep - scan mailbox for messages matching a regular expression mboxgrep - scan mailbox for messages matching a regular expression
Copyright (C) 2000 - 2004, 2006, 2023 - 2024 Daniel Spiljar Copyright (C) 2000 - 2004, 2006, 2023 Daniel Spiljar
Mboxgrep is free software; you can redistribute it and/or modify it Mboxgrep is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by 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) if (config.action == ACTION_DELETE)
{ {
tmpmbox_create (argv[optind]); 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]); config.boxname = xstrdup (argv[optind]);

View File

@ -1,6 +1,6 @@
/* /*
mboxgrep - scan mailbox for messages matching a regular expression mboxgrep - scan mailbox for messages matching a regular expression
Copyright (C) 2000 - 2004, 2006, 2023 - 2024 Daniel Spiljar Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006, 2023 Daniel Spiljar
Mboxgrep is free software; you can redistribute it and/or modify it Mboxgrep is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by under the terms of the GNU General Public License as published by
@ -50,47 +50,166 @@
#endif /* HAVE_LIBDMALLOC */ #endif /* HAVE_LIBDMALLOC */
mbox_t * mbox_t *
mbox_open (const char *path, const mbox_mode_t mbox_mode) mbox_open (const char *path, const char *mode)
{ {
mbox_t *mp; mbox_t *mp;
static int fd; static int fd;
#ifndef HAVE_FLOCK #ifndef HAVE_FLOCK
struct flock lck; struct flock lck;
#endif /* HAVE_FLOCK */ #endif /* HAVE_FLOCK */
char buffer[BUFSIZ];
mp = (mbox_t *) xmalloc (sizeof (mbox_t)); mp = (mbox_t *) xmalloc (sizeof (mbox_t));
mp->postmark_cache = NULL; mp->postmark_cache = (char *) xmalloc (BUFSIZ * sizeof (char));
if (0 == strcmp ("-", path)) if (0 == strcmp ("-", path))
mp->fp = stdin; mp->fp = stdin;
else else
{ {
if (mbox_mode == w) if (mode[0] == 'r')
fd = m_open (path, O_RDONLY, 0);
else if (mode[0] == 'w')
fd = m_open (path, (O_WRONLY | O_CREAT | O_APPEND), fd = m_open (path, (O_WRONLY | O_CREAT | O_APPEND),
(S_IWUSR | S_IRUSR)); (S_IWUSR | S_IRUSR));
else else
fd = m_open (path, O_RDONLY, 0); {
fprintf (stderr, "%s: mbox.c: Unknown mode %c. You shouldn't "
"get this error...", APPNAME, mode[0]);
exit (2);
}
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;
} }
if (config.lock > LOCK_NONE) if (config.lock > LOCK_NONE)
mbox_lock (fd, path, mbox_mode); {
#ifdef HAVE_FLOCK
int op;
mp->fp = mbox_fdopen (fd, path, mbox_mode); if (mode[0] == 'r')
op = LOCK_SH;
else
op = LOCK_EX;
if (-1 == flock (fd, op))
#else
memset (&lck, 0, sizeof (struct flock));
lck.l_whence = SEEK_SET;
if (mode[0] == 'r')
lck.l_type = F_RDLCK;
else
lck.l_type = F_WRLCK;
if (-1 == fcntl (fd, F_SETLK, &lck))
#endif /* HAVE_FLOCK */
{
if (config.merr)
{
fprintf (stderr, "%s: %s: ", APPNAME, path);
perror (NULL);
}
errno = 0;
close (fd);
return NULL;
}
}
if (mode[0] == 'r')
{
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 (mode[0] == '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;
}
} }
if (mbox_mode == r) if (mode[0] == 'r')
{ {
mp->postmark_cache = mbox_check_postmark (mp, path); if (config.format == FORMAT_MBOX)
fgets (buffer, BUFSIZ, mp->fp);
#ifdef HAVE_LIBZ
else if (config.format == FORMAT_ZMBOX)
gzgets (mp->fp, buffer, BUFSIZ);
#endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2
else if (config.format == FORMAT_BZ2MBOX)
{
char c[1] = "\0";
int n = 0;
if (! mp->postmark_cache) while (c[0] != '\n' && n < BUFSIZ)
return NULL; {
BZ2_bzread (mp->fp, c, 1);
buffer[n] = c[0];
n++;
}
buffer[n] = '\0';
}
#endif /* HAVE_LIBBZ2 */
if (0 != strncmp ("From ", buffer, 5))
{
if (config.merr)
{
if (0 == strcmp ("-", path))
fprintf (stderr, "%s: (standard input): Not a mbox folder\n",
APPNAME);
else
fprintf (stderr, "%s: %s: Not a mbox folder\n", APPNAME,
path);
}
if (config.format == FORMAT_MBOX)
fclose (mp->fp);
#ifdef HAVE_LIBZ
else if (config.format == FORMAT_ZMBOX)
gzclose (mp->fp);
#endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2
else if (config.format == FORMAT_BZ2MBOX)
BZ2_bzclose (mp->fp);
#endif /* HAVE_LIBBZ2 */
return NULL;
}
strcpy (mp->postmark_cache, buffer);
} }
return mp; return mp;
} }
@ -312,127 +431,3 @@ tmpfile_create (void)
} }
return fd; return fd;
} }
void
mbox_lock (int fd, const char *path, const mbox_mode_t mbox_mode)
{
#ifdef HAVE_FLOCK
int op;
if (mbox_mode == r)
op = LOCK_SH;
else
op = LOCK_EX;
if (-1 == flock (fd, op))
#else
memset (&lck, 0, sizeof (struct flock));
lck.l_whence = SEEK_SET;
if (mode[0] == 'r')
lck.l_type = F_RDLCK;
else
lck.l_type = F_WRLCK;
if (-1 == fcntl (fd, F_SETLK, &lck))
#endif /* HAVE_FLOCK */
{
if (config.merr)
{
fprintf (stderr, "%s: %s: ", APPNAME, path);
perror (NULL);
exit (2);
}
}
}
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;
}
char *
mbox_check_postmark(mbox_t *mp, const char *path)
{
char *buffer;
buffer = (char *) xmalloc (BUFSIZ * sizeof (char));
memset (buffer, 0, BUFSIZ);
if (config.format == FORMAT_MBOX)
fgets (buffer, BUFSIZ, mp->fp);
#ifdef HAVE_LIBZ
else if (config.format == FORMAT_ZMBOX)
gzgets (mp->fp, buffer, BUFSIZ);
#endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2
else if (config.format == FORMAT_BZ2MBOX)
{
char c[1] = "\0";
int n = 0;
while (c[0] != '\n' && n < BUFSIZ)
{
BZ2_bzread (mp->fp, c, 1);
buffer[n] = c[0];
n++;
}
buffer[n] = '\0';
}
#endif /* HAVE_LIBBZ2 */
if (0 != strncmp ("From ", buffer, 5))
{
if ((config.merr) && (buffer[0] != '\0'))
{
if (0 == strcmp ("-", path))
fprintf (stderr, "%s: (standard input): Not an mbox folder\n",
APPNAME);
else
fprintf (stderr, "%s: %s: Not an mbox folder\n", APPNAME,
path);
}
if (config.format == FORMAT_MBOX)
fclose (mp->fp);
#ifdef HAVE_LIBZ
else if (config.format == FORMAT_ZMBOX)
gzclose (mp->fp);
#endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2
else if (config.format == FORMAT_BZ2MBOX)
BZ2_bzclose (mp->fp);
#endif /* HAVE_LIBBZ2 */
return NULL;
}
return buffer;
}

View File

@ -1,6 +1,6 @@
/* /*
mboxgrep - scan mailbox for messages matching a regular expression mboxgrep - scan mailbox for messages matching a regular expression
Copyright (C) 2000 - 2004, 2023 - 2024 Daniel Spiljar Copyright (C) 2000 - 2004, 2023 Daniel Spiljar
Mboxgrep is free software; you can redistribute it and/or modify it Mboxgrep is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by under the terms of the GNU General Public License as published by
@ -19,7 +19,9 @@
#ifndef MBOX_H #ifndef MBOX_H
# define MBOX_H 1 # define MBOX_H 1
# include <config.h> # include <config.h>
# include "message.h" # include "message.h"
typedef struct typedef struct
@ -29,14 +31,8 @@ typedef struct
char *postmark_cache; char *postmark_cache;
} mbox_t; } 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 tmpmbox_create (const char *path);
void tmpfile_name (const char *path); void tmpfile_name (const char *path);
void tmpfile_mod_own (const int fd, const char *path); void tmpfile_mod_own (const int fd, const char *path);
@ -44,8 +40,5 @@ int tmpfile_create (void);
void mbox_close (mbox_t * mbp); 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_fdopen (int fd, const char *path, const mbox_mode_t mbox_mode);
char *mbox_check_postmark(mbox_t *mp, const char *path);
#endif /* MBOX_H */ #endif /* MBOX_H */

View File

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