Code indentation and other minor cleanups.

This commit is contained in:
Daniel Spiljar 2023-01-30 21:51:37 +01:00
parent 1d75932569
commit 8d4c7f1d4e
7 changed files with 274 additions and 303 deletions

View File

@ -28,7 +28,6 @@
void
print_wrap (char *str, int len, int *n)
/* {{{ */
{
*n += len;
fprintf (stdout, str);
@ -39,11 +38,9 @@ print_wrap (char *str, int len, int *n)
}
else fprintf (stdout, " ");
}
/* }}} */
void
version (void)
/* {{{ */
{
int n = 0;
@ -93,13 +90,12 @@ version (void)
print_wrap ("HAVE_SYS_NDIR_H", 15, &n);
#endif
fprintf (stdout, "\n");
exit(0);
}
/* }}} */
void
help (void)
/* {{{ */
{
fprintf(stdout, "%s %s - search MAILBOX for messages matching PATTERN\n\n",
APPNAME, VERSION);
@ -157,15 +153,12 @@ help (void)
exit(0);
}
/* }}} */
void
usage (void)
/* {{{ */
{
printf ("Usage: %s [OPTION] PATTERN MAILBOX ...\n\n"
"Try `%s --help' for more information.\n", APPNAME, APPNAME);
exit (2);
}
/* }}} */

View File

@ -1,6 +1,6 @@
/* -*- C -*-
/*
mboxgrep - scan mailbox for messages matching a regular expression
Copyright (C) 2000, 2001, 2002, 2003 Daniel Spiljar
Copyright (C) 2000 - 2003, 2023 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
@ -15,8 +15,7 @@
You should have received a copy of the GNU General Public License
along with mboxgrep; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id: info.h,v 1.5 2003-04-13 19:15:58 dspiljar Exp $ */
*/
#ifndef INFO_H
#define INFO_H

View File

@ -1,6 +1,6 @@
/* -*- C -*-
/*
mboxgrep - scan mailbox for messages matching a regular expression
Copyright (C) 2000, 2001, 2002, 2003, 2006 Daniel Spiljar
Copyright (C) 2000 - 2003, 2006, 2023 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
@ -15,8 +15,7 @@
You should have received a copy of the GNU General Public License
along with mboxgrep; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id: maildir.c,v 1.25 2006-10-19 01:53:43 dspiljar Exp $ */
*/
#include <config.h>
@ -84,10 +83,10 @@ maildir_open (const char *path)
if (mp->new == NULL)
{
if (config.merr)
{
fprintf (stderr, "%s: %s: ", APPNAME, dirname);
perror (NULL);
}
{
fprintf (stderr, "%s: %s: ", APPNAME, dirname);
perror (NULL);
}
free (dirname);
errno = 0;
return NULL;
@ -98,10 +97,10 @@ maildir_open (const char *path)
if (mp->cur == NULL)
{
if (config.merr)
{
fprintf (stderr, "%s: %s: ", APPNAME, dirname);
perror (NULL);
}
{
fprintf (stderr, "%s: %s: ", APPNAME, dirname);
perror (NULL);
}
free (dirname);
errno = 0;
return NULL;
@ -128,37 +127,37 @@ maildir_read_message (maildir_t *mdp)
for(;;)
{
if (mdp->new != NULL)
{
d_content = readdir (mdp->new);
if (d_content == NULL)
{
closedir (mdp->new);
mdp->new = NULL;
}
}
{
d_content = readdir (mdp->new);
if (d_content == NULL)
{
closedir (mdp->new);
mdp->new = NULL;
}
}
if (mdp->new == NULL && mdp->cur != NULL)
{
d_content = readdir (mdp->cur);
if (d_content == NULL)
{
closedir (mdp->cur);
mdp->cur = NULL;
}
}
{
d_content = readdir (mdp->cur);
if (d_content == NULL)
{
closedir (mdp->cur);
mdp->cur = NULL;
}
}
if (mdp->new == NULL && mdp->cur == NULL)
{
free (message->headers);
free (message->body);
free (message);
return NULL;
}
{
free (message->headers);
free (message->body);
free (message);
return NULL;
}
if (d_content->d_name[0] == '.')
continue;
continue;
filename =
(char *) xmalloc ((sizeof (char)*((strlen (d_content->d_name))
+ (strlen (config.boxname)) + 6)));
(char *) xmalloc ((sizeof (char)*((strlen (d_content->d_name))
+ (strlen (config.boxname)) + 6)));
/*
filename =
@ -166,9 +165,10 @@ maildir_read_message (maildir_t *mdp)
+ (strlen(config.boxname)) + 2)));
*/
if (mdp->new != NULL)
sprintf (filename, "%s/new/%s", config.boxname, d_content->d_name);
sprintf (filename, "%s/new/%s", config.boxname, d_content->d_name);
else
sprintf (filename, "%s/cur/%s", config.boxname, d_content->d_name);
sprintf (filename, "%s/cur/%s", config.boxname, d_content->d_name);
message->filename = (char *) xstrdup (filename);
free (filename);
@ -176,55 +176,58 @@ maildir_read_message (maildir_t *mdp)
fp = m_fopen(message->filename, "r");
if (fp == NULL)
continue;
continue;
while (fgets (buffer, BUFSIZ, fp) != NULL)
{
s = strlen (buffer);
if (0 == strncmp ("\n", buffer, 1) && isheaders == 1)
{
isheaders = 0;
continue;
} /* if */
if (isheaders)
{
if (0 == strncasecmp ("From: ", buffer, 6))
have_from = 1;
if (0 == strncasecmp ("Sender: ", buffer, 8))
have_sender = 1;
if (0 == strncasecmp ("Date: ", buffer, 6))
have_date = 1;
if (0 == strncasecmp ("To: ", buffer, 4))
have_to = 1;
if (0 == strncasecmp ("Message-ID: ", buffer, 12))
have_message_id = 1;
if (0 == strncasecmp ("Return-Path: ", buffer, 13))
message->from = parse_return_path(buffer);
{
s = strlen (buffer);
if (0 == strncmp ("\n", buffer, 1) && isheaders == 1)
{
isheaders = 0;
continue;
} /* if */
if (isheaders)
{
if (0 == strncasecmp ("From: ", buffer, 6))
have_from = 1;
if (0 == strncasecmp ("Sender: ", buffer, 8))
have_sender = 1;
if (0 == strncasecmp ("Date: ", buffer, 6))
have_date = 1;
if (0 == strncasecmp ("To: ", buffer, 4))
have_to = 1;
if (0 == strncasecmp ("Message-ID: ", buffer, 12))
have_message_id = 1;
if (0 == strncasecmp ("Return-Path: ", buffer, 13))
message->from = parse_return_path(buffer);
message->headers =
(char *) xrealloc (message->headers,
((1 + s + message->hbytes) * sizeof (char)));
strcpy (message->headers + message->hbytes, buffer);
message->hbytes += s;
} /* if */
else
{
message->body =
(char *) xrealloc (message->body,
((1 + s + message->bbytes) * sizeof (char)));
strcpy (message->body + message->bbytes, buffer);
message->bbytes += s;
} /* else */
} /* while */
message->headers =
(char *) xrealloc (message->headers,
((1 + s + message->hbytes) * sizeof (char)));
strcpy (message->headers + message->hbytes, buffer);
message->hbytes += s;
} /* if */
else
{
message->body =
(char *) xrealloc (message->body,
((1 + s + message->bbytes) * sizeof (char)));
strcpy (message->body + message->bbytes, buffer);
message->bbytes += s;
} /* else */
} /* while */
/* if (!have_from || !have_to || !have_message_id) */
if ((!have_from && !have_sender)|| !have_date)
{
if (config.merr)
fprintf(stderr, "%s: %s: Not a RFC 2822 message\n",
APPNAME, message->filename);
fclose(fp);
continue;
}
{
if (config.merr)
fprintf(stderr, "%s: %s: Not a RFC 2822 message\n",
APPNAME, message->filename);
fclose(fp);
continue;
}
fclose(fp);
return message;
@ -297,19 +300,19 @@ maildir_create (const char *path)
sprintf(s, "%s", path);
i = mkdir (s, S_IRWXU);
if (-1 == i)
break;
break;
sprintf(s, "%s/new", path);
i = mkdir (s, S_IRWXU);
if (-1 == i)
break;
break;
sprintf(s, "%s/cur", path);
i = mkdir (s, S_IRWXU);
if (-1 == i)
break;
break;
sprintf(s, "%s/tmp", path);
i = mkdir (s, S_IRWXU);
if (-1 == i)
break;
break;
break;
}
@ -317,9 +320,9 @@ maildir_create (const char *path)
if (errno != 0)
{
if (config.merr)
{
fprintf(stderr, "%s:%s: ", APPNAME, s);
perror (NULL);
}
{
fprintf(stderr, "%s:%s: ", APPNAME, s);
perror (NULL);
}
}
}

View File

@ -15,7 +15,6 @@
You should have received a copy of the GNU General Public License
along with mboxgrep; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <config.h>

View File

@ -1,4 +1,4 @@
/* -*- C -*-
/*
mboxgrep - scan mailbox for messages matching a regular expression
Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006 Daniel Spiljar
@ -15,8 +15,7 @@
You should have received a copy of the GNU General Public License
along with mboxgrep; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id: mbox.c,v 1.34 2006-10-22 23:34:49 dspiljar Exp $ */
*/
#include <config.h>
@ -52,7 +51,6 @@
mbox_t *
mbox_open (const char *path, const char *mode)
/* {{{ */
{
mbox_t *mp;
static int fd;
@ -69,155 +67,153 @@ mbox_open (const char *path, const char *mode)
else
{
if (mode[0] == 'r')
fd = m_open (path, O_RDONLY, 0);
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));
else
{
fprintf (stderr, "%s: mbox.c: Unknown mode %c. You shouldn't "
"get this error...", APPNAME, mode[0]);
exit (2);
}
{
fprintf (stderr, "%s: mbox.c: Unknown mode %c. You shouldn't "
"get this error...", APPNAME, mode[0]);
exit (2);
}
if (fd == -1)
{
if (config.merr)
{
fprintf (stderr, "%s: %s: ", APPNAME, path);
perror (NULL);
}
errno = 0;
return NULL;
}
{
if (config.merr)
{
fprintf (stderr, "%s: %s: ", APPNAME, path);
perror (NULL);
}
errno = 0;
return NULL;
}
if (config.lock)
{
{
#ifdef HAVE_FLOCK
int op;
int op;
if (mode[0] == 'r')
op = LOCK_SH;
else
op = LOCK_EX;
if (-1 == flock (fd, op))
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;
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))
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 (config.merr)
{
fprintf (stderr, "%s: %s: ", APPNAME, path);
perror (NULL);
}
errno = 0;
close (fd);
return NULL;
}
}
if (mode[0] == 'r')
{
if (config.format == MBOX)
mp->fp = (FILE *) m_fdopen (fd, "r");
{
if (config.format == MBOX)
mp->fp = (FILE *) m_fdopen (fd, "r");
#ifdef HAVE_LIBZ
else if (config.format == ZMBOX)
mp->fp = (gzFile *) m_gzdopen (fd, "rb");
else if (config.format == ZMBOX)
mp->fp = (gzFile *) m_gzdopen (fd, "rb");
#endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2
else if (config.format == BZ2MBOX)
mp->fp = (BZFILE *) BZ2_bzdopen (fd, "rb");
else if (config.format == BZ2MBOX)
mp->fp = (BZFILE *) BZ2_bzdopen (fd, "rb");
#endif /* HAVE_LIBBZ2 */
}
}
else if (mode[0] == 'w')
{
if (config.format == MBOX)
mp->fp = (FILE *) m_fdopen (fd, "w");
{
if (config.format == MBOX)
mp->fp = (FILE *) m_fdopen (fd, "w");
#ifdef HAVE_LIBZ
else if (config.format == ZMBOX)
mp->fp = (gzFile *) m_gzdopen (fd, "wb");
else if (config.format == ZMBOX)
mp->fp = (gzFile *) m_gzdopen (fd, "wb");
#endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2
else if (config.format == BZ2MBOX)
mp->fp = (BZFILE *) BZ2_bzdopen (fd, "wb");
else if (config.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 (config.merr)
{
fprintf (stderr, "%s: %s: ", APPNAME, path);
perror (NULL);
}
errno = 0;
close (fd);
return NULL;
}
}
if (mode[0] == 'r')
{
if (config.format == MBOX)
fgets (buffer, BUFSIZ, mp->fp);
fgets (buffer, BUFSIZ, mp->fp);
#ifdef HAVE_LIBZ
else if (config.format == ZMBOX)
gzgets (mp->fp, buffer, BUFSIZ);
gzgets (mp->fp, buffer, BUFSIZ);
#endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2
else if (config.format == BZ2MBOX)
{
char c[1] = "\0";
int n = 0;
{
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';
}
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)
{
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 == MBOX)
fclose (mp->fp);
{
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 == MBOX)
fclose (mp->fp);
#ifdef HAVE_LIBZ
else if (config.format == ZMBOX)
gzclose (mp->fp);
else if (config.format == ZMBOX)
gzclose (mp->fp);
#endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2
else if (config.format == BZ2MBOX)
BZ2_bzclose (mp->fp);
else if (config.format == BZ2MBOX)
BZ2_bzclose (mp->fp);
#endif /* HAVE_LIBBZ2 */
return NULL;
}
return NULL;
}
strcpy (mp->postmark_cache, buffer);
}
return mp;
}
/* }}} */
void
mbox_close (mbox_t * mp)
/* {{{ */
{
if (config.format == MBOX)
fclose (mp->fp);
@ -233,12 +229,9 @@ mbox_close (mbox_t * mp)
free (mp->postmark_cache);
free (mp);
}
/* }}} */
message_t *
mbox_read_message (mbox_t * mp)
/* {{{ */
{
int isheaders = 1, s;
char buffer[BUFSIZ];
@ -256,93 +249,89 @@ mbox_read_message (mbox_t * mp)
for (;;)
{
if (config.format == MBOX)
{
if (fgets (buffer, BUFSIZ, mp->fp) == NULL)
{
if (isheaders)
return NULL;
else
return message;
}
}
{
if (fgets (buffer, BUFSIZ, mp->fp) == NULL)
{
if (isheaders)
return NULL;
else
return message;
}
}
#ifdef HAVE_LIBZ
else if (config.format == ZMBOX)
{
if (gzgets (mp->fp, buffer, BUFSIZ) == NULL)
{
if (isheaders)
return NULL;
else
return message;
}
}
{
if (gzgets (mp->fp, buffer, BUFSIZ) == NULL)
{
if (isheaders)
return NULL;
else
return message;
}
}
#endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2
else if (config.format == BZ2MBOX)
{
char c[1] = "\0";
int n = 0;
{
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';
while (c[0] != '\n' && n < BUFSIZ)
{
BZ2_bzread (mp->fp, c, 1);
buffer[n] = c[0];
n++;
}
buffer[n] = '\0';
if (buffer[0] == '\0')
{
if (isheaders)
return NULL;
else
return message;
}
}
if (buffer[0] == '\0')
{
if (isheaders)
return NULL;
else
return message;
}
}
#endif /* HAVE_LIBBZ2 */
s = strlen (buffer);
if (buffer[0] == '\n' && isheaders == 1)
{
isheaders = 0;
continue;
}
{
isheaders = 0;
continue;
}
if (isheaders)
{
message->headers =
(char *) realloc (message->headers,
{
message->headers =
(char *) realloc (message->headers,
((1 + s + message->hbytes) * sizeof (char)));
strcpy (message->headers + message->hbytes, buffer);
message->hbytes += s;
} /* if */
strcpy (message->headers + message->hbytes, buffer);
message->hbytes += s;
}
else
{
if (0 == strncmp (buffer, "From ", 5))
{
strcpy (mp->postmark_cache, buffer);
return message;
}
message->body =
(char *) realloc (message->body,
{
if (0 == strncmp (buffer, "From ", 5))
{
strcpy (mp->postmark_cache, buffer);
return message;
}
message->body =
(char *) realloc (message->body,
((1 + s + message->bbytes) * sizeof (char)));
strcpy (message->body + message->bbytes, buffer);
message->bbytes += s;
}
} /* for */
strcpy (message->body + message->bbytes, buffer);
message->bbytes += s;
}
}
return NULL;
}
/* }}} */
void
tmpmbox_create (const char *path)
/* {{{ */
{
int foo;
@ -351,11 +340,9 @@ tmpmbox_create (const char *path)
tmpfile_mod_own (foo, path);
}
/* }}} */
void
tmpfile_name (const char *path)
/* {{{ */
{
char *fname, *tmpdir;
@ -363,7 +350,7 @@ tmpfile_name (const char *path)
{
tmpdir = getenv ("TMPDIR");
if (tmpdir == NULL)
tmpdir = xstrdup ("/tmp");
tmpdir = xstrdup ("/tmp");
fname = xstrdup ("/mboxgrepXXXXXX");
}
else
@ -377,13 +364,10 @@ tmpfile_name (const char *path)
* sizeof (char));
sprintf (config.tmpfilename, "%s%s", tmpdir, fname);
}
/* }}} */
void
mbox_write_message (message_t *msg, mbox_t *mbox)
/* {{{ */
{
if (config.format == MBOX)
fprintf (mbox->fp, "%s\n%s", msg->headers, msg->body);
@ -404,11 +388,9 @@ mbox_write_message (message_t *msg, mbox_t *mbox)
}
#endif /* HAVE_LIBBZ2 */
}
/* }}} */
void
tmpfile_mod_own (const int fd, const char *path)
/* {{{ */
{
/* If we're root, copy {owner, group, perms} of mailbox to the tmpfile
* so rename() will thus retain the original's ownership & permissions.
@ -418,20 +400,18 @@ tmpfile_mod_own (const int fd, const char *path)
struct stat s;
if (stat (path, &s) != -1)
{
if (fchown (fd, s.st_uid, s.st_gid) == -1)
if (config.merr) perror (config.tmpfilename);
if (fchmod (fd, s.st_mode) == -1)
if (config.merr) perror (config.tmpfilename);
}
{
if (fchown (fd, s.st_uid, s.st_gid) == -1)
if (config.merr) perror (config.tmpfilename);
if (fchmod (fd, s.st_mode) == -1)
if (config.merr) perror (config.tmpfilename);
}
else if (config.merr) perror (path);
}
}
/* }}} */
int
tmpfile_create (void)
/* {{{ */
{
int fd;
@ -439,12 +419,11 @@ tmpfile_create (void)
if (-1 == fd)
{
if (config.merr)
{
fprintf (stderr, "%s: %s: ", APPNAME, config.tmpfilename);
perror (NULL);
}
{
fprintf (stderr, "%s: %s: ", APPNAME, config.tmpfilename);
perror (NULL);
}
exit (2);
}
return fd;
}
/* }}} */

View File

@ -1,6 +1,6 @@
/* -*- C -*-
/*
mboxgrep - scan mailbox for messages matching a regular expression
Copyright (C) 2000, 2001, 2002, 2003, 2004 Daniel Spiljar
Copyright (C) 2000 - 2004, 2023 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
@ -15,8 +15,7 @@
You should have received a copy of the GNU General Public License
along with mboxgrep; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id: mbox.h,v 1.13 2006-10-22 23:34:49 dspiljar Exp $ */
*/
#ifndef MBOX_H
#define MBOX_H 1

View File

@ -1,6 +1,6 @@
/* -*- C -*-
/*
mboxgrep - scan mailbox for messages matching a regular expression
Copyright (C) 2020 Daniel Spiljar
Copyright (C) 2020, 2023 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
@ -15,8 +15,7 @@
You should have received a copy of the GNU General Public License
along with mboxgrep; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id$ */
*/
#ifndef MESSAGE_H
#define MESSAGE_H 1