Code cleanup and indentation.

C source files (with the exception of third-party files, i.e. getopt and
md5) have been indented with GNU indent.

The indentation "standard" has been documented in the readme file.
This commit is contained in:
Daniel Spiljar 2023-03-03 22:49:48 +01:00
parent 882a38f908
commit ee2489483b
Signed by: dspiljar
GPG Key ID: A32CE9C59D8003B5
19 changed files with 740 additions and 663 deletions

View File

@ -13,3 +13,6 @@ GNU/Linux distrubution, for example), please drop me a line about it.
For build instructions, read the file "INSTALL", and run
`./configure --help`.
This project aims to follow the GNU coding style (https://www.gnu.org/prep/standards/html_node/Formatting.html),
at least loosely. The code should be indented with indent -gnu -nut -ppi2.

View File

@ -36,7 +36,8 @@ print_wrap (char *str, int len, int *n)
fprintf (stdout, "\n");
*n = 0;
}
else fprintf (stdout, " ");
else
fprintf (stdout, " ");
}
void
@ -45,12 +46,12 @@ version (void)
int n = 0;
fprintf (stdout, "%s %s\n\n"
"Copyright (C) 2000 - 2004, 2006, 2010, 2023 Daniel Spiljar\n"
"This program is free software; you can redistribute it and/or "
"modify\nit under the terms of the GNU General Public License "
"as published by\nthe Free Software Foundation; either version "
"2 of the License, or\n(at your option) any later version.\n\n",
APPNAME, VERSION);
"Copyright (C) 2000 - 2004, 2006, 2010, 2023 Daniel Spiljar\n"
"This program is free software; you can redistribute it and/or "
"modify\nit under the terms of the GNU General Public License "
"as published by\nthe Free Software Foundation; either version "
"2 of the License, or\n(at your option) any later version.\n\n",
APPNAME, VERSION);
fprintf (stdout, "Compilation options:\n");
#ifdef HAVE_DIRENT_H
print_wrap ("HAVE_DIRENT_H", 13, &n);
@ -64,9 +65,10 @@ version (void)
#ifdef HAVE_FTS_OPEN
print_wrap ("HAVE_FTS_OPEN", 13, &n);
#else
# ifdef HAVE_FTW
# ifdef HAVE_FTW
print_wrap ("HAVE_FTW", 8, &n);
# endif /* HAVE_FTW */
# endif
/* HAVE_FTW */
#endif /* HAVE_FTS_OPEN */
/*
fprintf (stdout, "HAVE_LIBLOCKFILE ");
@ -100,71 +102,71 @@ version (void)
#endif /* HAVE_LIBDMALLOC */
fprintf (stdout, "\n");
exit(0);
exit (0);
}
void
void
help (void)
{
fprintf(stdout, "%s %s - search MAILBOX for messages matching PATTERN\n\n",
APPNAME, VERSION);
fprintf(stdout,
"Miscellaneous:\n\n"
" -h, --help\t\t\tThis help screen\n"
" -V, --version\t\tDisplay version, copyright and\n"
"\t\t\t\tcompile-time options information\n"
" -r, --recursive\t\tDescend into directories recursively\n\n"
"Output control:\n\n"
" -c, --count\t\t\tPrint a count of matching messages\n"
" -d, --delete\t\t\tDelete matching messages\n"
" -nd, --no-duplicates\t\tIgnore duplicate messages\n"
" -o, --output=MAILBOX\t\tWrite messages to MAILBOX\n"
" -p, --pipe=COMMAND\t\tPipe each found message to COMMAND\n"
" -s, --no-messages\t\tSuppress most error messages\n"
" --debug\t\t\tPrint messages useful for debugging\n\n"
"Matching criteria:\n\n"
" -E, --extended-regexp\tPATTERN is an extended regular expression\n"
" -G, --basic-regexp\t\tPATTERN is a basic regular expression\n");
fprintf (stdout, "%s %s - search MAILBOX for messages matching PATTERN\n\n",
APPNAME, VERSION);
fprintf (stdout,
"Miscellaneous:\n\n"
" -h, --help\t\t\tThis help screen\n"
" -V, --version\t\tDisplay version, copyright and\n"
"\t\t\t\tcompile-time options information\n"
" -r, --recursive\t\tDescend into directories recursively\n\n"
"Output control:\n\n"
" -c, --count\t\t\tPrint a count of matching messages\n"
" -d, --delete\t\t\tDelete matching messages\n"
" -nd, --no-duplicates\t\tIgnore duplicate messages\n"
" -o, --output=MAILBOX\t\tWrite messages to MAILBOX\n"
" -p, --pipe=COMMAND\t\tPipe each found message to COMMAND\n"
" -s, --no-messages\t\tSuppress most error messages\n"
" --debug\t\t\tPrint messages useful for debugging\n\n"
"Matching criteria:\n\n"
" -E, --extended-regexp\tPATTERN is an extended regular expression\n"
" -G, --basic-regexp\t\tPATTERN is a basic regular expression\n");
#ifdef HAVE_LIBPCRE
fprintf(stdout,
" -P, --perl-regexp\t\tPATTERN is a Perl regular expression\n");
fprintf (stdout,
" -P, --perl-regexp\t\tPATTERN is a Perl regular expression\n");
#endif /* HAVE_LIBPCRE */
fprintf(stdout,
" -e, --regexp=PATTERN\t\tUse PATTERN as a regular expression\n"
" -i, --ignore-case\t\tIgnore case distinctions\n"
" -v, --invert-match\t\tSelect non-matching messages\n\n"
"Search scope selection:\n\n"
" -H, --headers\t\tMatch PATTERN against message headers\n"
" -B, --body\t\t\tMatch PATTERN against message body\n\n"
"File locking:\n\n"
" -nl, --no-file-lock\t\tDo not lock files\n"
" -l, --file-lock=METHOD\tSelect file locking METHOD\n"
"\t\t\t\tMETHOD is");
fprintf (stdout,
" -e, --regexp=PATTERN\t\tUse PATTERN as a regular expression\n"
" -i, --ignore-case\t\tIgnore case distinctions\n"
" -v, --invert-match\t\tSelect non-matching messages\n\n"
"Search scope selection:\n\n"
" -H, --headers\t\tMatch PATTERN against message headers\n"
" -B, --body\t\t\tMatch PATTERN against message body\n\n"
"File locking:\n\n"
" -nl, --no-file-lock\t\tDo not lock files\n"
" -l, --file-lock=METHOD\tSelect file locking METHOD\n"
"\t\t\t\tMETHOD is");
#ifdef HAVE_FCNTL
fprintf(stdout, " `fcntl',");
fprintf (stdout, " `fcntl',");
#endif /* HAVE_FCNTL */
#ifdef HAVE_FLOCK
fprintf(stdout, " `flock',");
fprintf (stdout, " `flock',");
#endif /* HAVE_FLOCK */
fprintf(stdout, " or `none'\n\n"
"Mailbox type selection:\n\n"
" -m, --mailbox-format=TYPE\tSelect mailbox TYPE\n"
"\t\t\t\tTYPE is `mbox', ");
fprintf (stdout, " or `none'\n\n"
"Mailbox type selection:\n\n"
" -m, --mailbox-format=TYPE\tSelect mailbox TYPE\n"
"\t\t\t\tTYPE is `mbox', ");
#ifdef HAVE_LIBZ
fprintf(stdout, "`zmbox', ");
fprintf (stdout, "`zmbox', ");
#endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2
fprintf(stdout, "`bz2mbox', ");
fprintf (stdout, "`bz2mbox', ");
#endif /* HAVE_LIBBZ2 */
fprintf(stdout,
"`mh',\n"
"\t\t\t\t`nnml', `nnmh', or `maildir'.\n\n"
"Mail bug reports and flames to <%s>.\n", BUGREPORT_ADDR);
fprintf (stdout,
"`mh',\n"
"\t\t\t\t`nnml', `nnmh', or `maildir'.\n\n"
"Mail bug reports and flames to <%s>.\n", BUGREPORT_ADDR);
exit(0);
exit (0);
}
void
void
usage (void)
{
printf ("Usage: %s [OPTION] PATTERN MAILBOX ...\n\n"

View File

@ -18,7 +18,7 @@
*/
#ifndef INFO_H
#define INFO_H
# define INFO_H
void print_wrap (char *str, int len, int *n);
void version (void);

View File

@ -23,20 +23,23 @@
#include <string.h>
#ifdef HAVE_DIRENT_H
# include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name)
# include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name)
#else
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
# ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif /* HAVE_SYS_NDIR_H */
# ifdef HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif /* HAVE_SYS_DIR_H */
# ifdef HAVE_NDIR_H
# include <ndir.h>
# endif /* HAVE_NDIR_H */
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
# ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif
/* HAVE_SYS_NDIR_H */
# ifdef HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif
/* HAVE_SYS_DIR_H */
# ifdef HAVE_NDIR_H
# include <ndir.h>
# endif
/* HAVE_NDIR_H */
#endif /* HAVE_DIRENT_H */
#include <stdlib.h>
@ -52,7 +55,7 @@
#include "wrap.h"
#ifdef HAVE_LIBDMALLOC
#include <dmalloc.h>
# include <dmalloc.h>
#endif /* HAVE_LIBDMALLOC */
maildir_t *
@ -64,20 +67,20 @@ maildir_open (const char *path)
foo = m_opendir (path);
if (foo == NULL)
return NULL;
return NULL;
closedir (foo);
if (-1 == maildir_check (path))
{
if (config.merr)
fprintf (stderr, "%s: %s: Not a maildir folder\n", APPNAME, path);
fprintf (stderr, "%s: %s: Not a maildir folder\n", APPNAME, path);
return NULL;
}
dirname = (char *) xmalloc((sizeof (char) * (strlen (path) + 5)));
dirname = (char *) xmalloc ((sizeof (char) * (strlen (path) + 5)));
mp = (maildir_t *) xmalloc(sizeof (maildir_t));
mp = (maildir_t *) xmalloc (sizeof (maildir_t));
sprintf (dirname, "%s/new", path);
mp->new = m_opendir (dirname);
if (mp->new == NULL)
@ -108,14 +111,14 @@ maildir_open (const char *path)
free (dirname);
return mp;
} /* maildir_open */
} /* maildir_open */
message_t *
maildir_read_message (maildir_t *mdp)
maildir_read_message (maildir_t * mdp)
{
int isheaders = 1;
int have_from = 0, have_to = 0, have_message_id = 0, have_sender = 0,
have_date = 0;
have_date = 0;
static message_t *message;
static struct dirent *d_content;
char *filename, buffer[BUFSIZ];
@ -124,9 +127,9 @@ maildir_read_message (maildir_t *mdp)
message = allocate_message ();
for(;;)
for (;;)
{
if (mdp->new != NULL)
if (mdp->new != NULL)
{
d_content = readdir (mdp->new);
if (d_content == NULL)
@ -155,9 +158,10 @@ maildir_read_message (maildir_t *mdp)
if (d_content->d_name[0] == '.')
continue;
filename =
(char *) xmalloc ((sizeof (char)*((strlen (d_content->d_name))
+ (strlen (config.boxname)) + 6)));
filename =
(char *) xmalloc ((sizeof (char) * ((strlen (d_content->d_name))
+ (strlen (config.boxname)) +
6)));
/*
filename =
@ -173,7 +177,7 @@ maildir_read_message (maildir_t *mdp)
free (filename);
isheaders = 1;
fp = m_fopen(message->filename, "r");
fp = m_fopen (message->filename, "r");
if (fp == NULL)
continue;
@ -185,7 +189,7 @@ maildir_read_message (maildir_t *mdp)
{
isheaders = 0;
continue;
} /* if */
} /* if */
if (isheaders)
{
if (0 == strncasecmp ("From: ", buffer, 6))
@ -199,43 +203,45 @@ maildir_read_message (maildir_t *mdp)
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->from = parse_return_path (buffer);
message->headers =
(char *) xrealloc (message->headers,
((1 + s + message->hbytes) * sizeof (char)));
((1 + s +
message->hbytes) * sizeof (char)));
strcpy (message->headers + message->hbytes, buffer);
message->hbytes += s;
} /* if */
} /* if */
else
{
message->body =
(char *) xrealloc (message->body,
((1 + s + message->bbytes) * sizeof (char)));
((1 + s +
message->bbytes) * sizeof (char)));
strcpy (message->body + message->bbytes, buffer);
message->bbytes += s;
} /* else */
} /* while */
} /* else */
} /* while */
/* if (!have_from || !have_to || !have_message_id) */
if ((!have_from && !have_sender)|| !have_date)
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);
fprintf (stderr, "%s: %s: Not a RFC 2822 message\n",
APPNAME, message->filename);
fclose (fp);
continue;
}
fclose(fp);
return message;
} /* for */
} /* maildir_read_message */
void
maildir_write_message (message_t *m, const char *path)
fclose (fp);
return message;
} /* for */
} /* maildir_read_message */
void
maildir_write_message (message_t * m, const char *path)
{
char bla[BUFSIZ], *s1, *s2;
int t;
@ -245,11 +251,11 @@ maildir_write_message (message_t *m, const char *path)
t = (int) time (NULL);
sprintf (bla, "%i.%i_%i.%s",
t, config.pid, runtime.maildir_count, config.hostname);
t, config.pid, runtime.maildir_count, config.hostname);
s1 = (char *) xmalloc ((strlen (path) + strlen (bla) + 6) * sizeof (char));
sprintf(s1, "%s/tmp/%s", path, bla);
sprintf (s1, "%s/tmp/%s", path, bla);
s2 = (char *) xmalloc ((strlen (path) + strlen (bla) + 6) * sizeof (char));
sprintf(s2, "%s/new/%s", path, bla);
sprintf (s2, "%s/new/%s", path, bla);
f1 = m_fopen (s1, "w");
fprintf (f1, "%s\n%s", m->headers, m->body);
@ -257,7 +263,7 @@ maildir_write_message (message_t *m, const char *path)
rename (s1, s2);
}
int
int
maildir_check (const char *path)
{
static struct stat fs;
@ -268,48 +274,54 @@ maildir_check (const char *path)
sprintf (s, "%s/cur", path);
i = stat (s, &fs);
if (-1 == i) return -1;
if (! S_ISDIR (fs.st_mode)) return -1;
if (-1 == i)
return -1;
if (!S_ISDIR (fs.st_mode))
return -1;
sprintf (s, "%s/new", path);
i = stat (s, &fs);
if (-1 == i) return -1;
if (! S_ISDIR (fs.st_mode)) return -1;
if (-1 == i)
return -1;
if (!S_ISDIR (fs.st_mode))
return -1;
sprintf(s, "%s/tmp", path);
sprintf (s, "%s/tmp", path);
i = stat (s, &fs);
if (-1 == i) return -1;
if (! S_ISDIR (fs.st_mode)) return -1;
if (-1 == i)
return -1;
if (!S_ISDIR (fs.st_mode))
return -1;
free (s);
return 0;
}
void
void
maildir_create (const char *path)
{
char *s;
int i;
s = (char *) xmalloc ((strlen (path) + 4) * sizeof(char));
s = (char *) xmalloc ((strlen (path) + 4) * sizeof (char));
errno = 0;
for (;;)
{
sprintf(s, "%s", path);
i = mkdir (s, S_IRWXU);
if (-1 == i)
break;
sprintf(s, "%s/new", path);
sprintf (s, "%s", path);
i = mkdir (s, S_IRWXU);
if (-1 == i)
break;
sprintf(s, "%s/cur", path);
sprintf (s, "%s/new", path);
i = mkdir (s, S_IRWXU);
if (-1 == i)
break;
sprintf(s, "%s/tmp", path);
sprintf (s, "%s/cur", path);
i = mkdir (s, S_IRWXU);
if (-1 == i)
break;
sprintf (s, "%s/tmp", path);
i = mkdir (s, S_IRWXU);
if (-1 == i)
break;
@ -321,7 +333,7 @@ maildir_create (const char *path)
{
if (config.merr)
{
fprintf(stderr, "%s:%s: ", APPNAME, s);
fprintf (stderr, "%s:%s: ", APPNAME, s);
perror (NULL);
}
}

View File

@ -18,28 +18,32 @@
*/
#ifndef MAILDIR_H
#define MAILDIR_H
# define MAILDIR_H
#include <config.h>
# include <config.h>
#ifdef HAVE_DIRENT_H
# include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name)
#else
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
# ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif /* HAVE_SYS_NDIR_H */
# ifdef HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif /* HAVE_SYS_DIR_H */
# ifdef HAVE_NDIR_H
# include <ndir.h>
# endif /* HAVE_NDIR_H */
#endif /* HAVE_DIRENT_H */
# ifdef HAVE_DIRENT_H
# include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name)
# else
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
# ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif
/* HAVE_SYS_NDIR_H */
# ifdef HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif
/* HAVE_SYS_DIR_H */
# ifdef HAVE_NDIR_H
# include <ndir.h>
# endif
/* HAVE_NDIR_H */
# endif
/* HAVE_DIRENT_H */
#include "mboxgrep.h"
# include "mboxgrep.h"
typedef struct
{
@ -50,8 +54,8 @@ typedef struct
maildir_t *maildir_open (const char *path);
int maildir_check (const char *path);
void maildir_create (const char *path);
void maildir_close (maildir_t *mdp);
message_t *maildir_read_message (maildir_t *mdp);
void maildir_write_message (message_t *m, const char *path);
void maildir_close (maildir_t * mdp);
message_t *maildir_read_message (maildir_t * mdp);
void maildir_write_message (message_t * m, const char *path);
#endif /* MAILDIR_H */

View File

@ -26,7 +26,7 @@
#include <stdlib.h>
#include <unistd.h>
#ifdef HAVE_LIBZ
#include <zlib.h>
# include <zlib.h>
#endif /* HAVE_LIBZ */
#include "getopt.h"
@ -36,11 +36,11 @@
#include "mbox.h"
#include "mh.h"
#include "scan.h"
#include "wrap.h" /* xcalloc() et cetera */
#include "wrap.h" /* xcalloc() et cetera */
#include "re.h"
#ifdef HAVE_LIBDMALLOC
#include <dmalloc.h>
# include <dmalloc.h>
#endif /* HAVE_LIBDMALLOC */
option_t config;
@ -54,33 +54,32 @@ main (int argc, char **argv)
runtime.count = 0;
runtime.maildir_count = 0;
static struct option long_options[] =
{
{"count", 0, 0, 'c'},
{"delete", 0, 0, 'd'},
/* {"date", 1, 0, 'D'}, */
{"extended-regexp", 0, 0, 'E'},
{"basic-regexp", 0, 0, 'G'},
{"perl-regexp", 0, 0, 'P'},
{"help", 0, 0, 'h'},
{"ignore-case", 0, 0, 'i'},
{"mailbox-format", 1, 0, 'm'},
{"no", 1, 0, 'n' },
{"pipe", 1, 0, 'p'},
{"regexp", 1, 0, 'e'},
{"invert-match", 0, 0, 'v'},
{"version", 0, 0, 'V'},
{"headers", 0, 0, 'H'},
{"body", 0, 0, 'B'},
{"no-messages", 0, 0, 's'},
{"output", 1, 0, 'o'},
{"no-duplicates", 0, 0, 200},
{"no-file-lock", 0, 0, 201},
{"debug", 0, 0, 202},
{"file-lock", 1, 0, 'l'},
{"recursive", 0, 0, 'r'},
{0, 0, 0, 0}
};
static struct option long_options[] = {
{"count", 0, 0, 'c'},
{"delete", 0, 0, 'd'},
/* {"date", 1, 0, 'D'}, */
{"extended-regexp", 0, 0, 'E'},
{"basic-regexp", 0, 0, 'G'},
{"perl-regexp", 0, 0, 'P'},
{"help", 0, 0, 'h'},
{"ignore-case", 0, 0, 'i'},
{"mailbox-format", 1, 0, 'm'},
{"no", 1, 0, 'n'},
{"pipe", 1, 0, 'p'},
{"regexp", 1, 0, 'e'},
{"invert-match", 0, 0, 'v'},
{"version", 0, 0, 'V'},
{"headers", 0, 0, 'H'},
{"body", 0, 0, 'B'},
{"no-messages", 0, 0, 's'},
{"output", 1, 0, 'o'},
{"no-duplicates", 0, 0, 200},
{"no-file-lock", 0, 0, 201},
{"debug", 0, 0, 202},
{"file-lock", 1, 0, 'l'},
{"recursive", 0, 0, 'r'},
{0, 0, 0, 0}
};
set_default_options ();
@ -102,14 +101,14 @@ main (int argc, char **argv)
runtime.cs->md5 = (char **) xcalloc (1, sizeof (char **));
runtime.cs->n = 0;
if (optind < argc && ! config.haveregex)
if (optind < argc && !config.haveregex)
{
config.regex_s = xstrdup (argv[optind]);
config.haveregex = 1;
++optind;
}
if (config.haveregex)
if (config.haveregex)
{
#ifdef HAVE_LIBPCRE
if (config.perl)
@ -161,7 +160,7 @@ main (int argc, char **argv)
++optind;
}
if (! havemailbox)
if (!havemailbox)
{
config.format = MBOX;
scan_mailbox ("-");

View File

@ -30,13 +30,13 @@
#include <errno.h>
#include <time.h>
#ifdef HAVE_FLOCK
#include <sys/file.h>
# include <sys/file.h>
#endif /* HAVE_FLOCK */
#ifdef HAVE_LIBZ
#include <zlib.h>
# include <zlib.h>
#endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2
#include <bzlib.h>
# include <bzlib.h>
#endif /* HAVE_LIBBZ2 */
#define BUFLEN 16384
@ -46,7 +46,7 @@
#include "wrap.h"
#ifdef HAVE_LIBDMALLOC
#include <dmalloc.h>
# include <dmalloc.h>
#endif /* HAVE_LIBDMALLOC */
mbox_t *
@ -70,11 +70,11 @@ mbox_open (const char *path, const char *mode)
fd = m_open (path, O_RDONLY, 0);
else if (mode[0] == 'w')
fd = m_open (path, (O_WRONLY | O_CREAT | O_APPEND),
(S_IWUSR | S_IRUSR));
(S_IWUSR | S_IRUSR));
else
{
fprintf (stderr, "%s: mbox.c: Unknown mode %c. You shouldn't "
"get this error...", APPNAME, mode[0]);
"get this error...", APPNAME, mode[0]);
exit (2);
}
@ -88,7 +88,7 @@ mbox_open (const char *path, const char *mode)
errno = 0;
return NULL;
}
if (config.lock)
{
#ifdef HAVE_FLOCK
@ -100,7 +100,7 @@ mbox_open (const char *path, const char *mode)
op = LOCK_EX;
if (-1 == flock (fd, op))
#else
memset (&lck, 0, sizeof (struct flock));
memset (&lck, 0, sizeof (struct flock));
lck.l_whence = SEEK_SET;
if (mode[0] == 'r')
lck.l_type = F_RDLCK;
@ -119,7 +119,7 @@ mbox_open (const char *path, const char *mode)
close (fd);
return NULL;
}
}
}
if (mode[0] == 'r')
{
@ -147,7 +147,7 @@ mbox_open (const char *path, const char *mode)
mp->fp = (BZFILE *) BZ2_bzdopen (fd, "wb");
#endif /* HAVE_LIBBZ2 */
}
if (mp->fp == NULL)
{
if (config.merr)
@ -191,9 +191,10 @@ mbox_open (const char *path, const char *mode)
{
if (0 == strcmp ("-", path))
fprintf (stderr, "%s: (standard input): Not a mbox folder\n",
APPNAME);
APPNAME);
else
fprintf (stderr, "%s: %s: Not a mbox folder\n", APPNAME, path);
fprintf (stderr, "%s: %s: Not a mbox folder\n", APPNAME,
path);
}
if (config.format == MBOX)
fclose (mp->fp);
@ -308,7 +309,7 @@ mbox_read_message (mbox_t * mp)
{
message->headers =
(char *) realloc (message->headers,
((1 + s + message->hbytes) * sizeof (char)));
((1 + s + message->hbytes) * sizeof (char)));
strcpy (message->headers + message->hbytes, buffer);
message->hbytes += s;
}
@ -321,7 +322,7 @@ mbox_read_message (mbox_t * mp)
}
message->body =
(char *) realloc (message->body,
((1 + s + message->bbytes) * sizeof (char)));
((1 + s + message->bbytes) * sizeof (char)));
strcpy (message->body + message->bbytes, buffer);
message->bbytes += s;
}
@ -346,7 +347,7 @@ tmpfile_name (const char *path)
{
char *fname, *tmpdir;
if (path == NULL) /* no path prefix given, use /tmp or TMPDIR */
if (path == NULL) /* no path prefix given, use /tmp or TMPDIR */
{
tmpdir = getenv ("TMPDIR");
if (tmpdir == NULL)
@ -361,13 +362,13 @@ tmpfile_name (const char *path)
config.tmpfilename =
(char *) xmalloc ((strlen (tmpdir) + (strlen (fname) + 1))
* sizeof (char));
* sizeof (char));
sprintf (config.tmpfilename, "%s%s", tmpdir, fname);
}
void
mbox_write_message (message_t *msg, mbox_t *mbox)
void
mbox_write_message (message_t * msg, mbox_t * mbox)
{
if (config.format == MBOX)
fprintf (mbox->fp, "%s\n%s", msg->headers, msg->body);
@ -375,7 +376,7 @@ mbox_write_message (message_t *msg, mbox_t *mbox)
else if (config.format == ZMBOX)
{
gzwrite_loop (mbox->fp, msg->headers);
gzwrite(mbox->fp, "\n", 1);
gzwrite (mbox->fp, "\n", 1);
gzwrite_loop (mbox->fp, msg->body);
}
#endif /* HAVE_LIBZ */
@ -402,11 +403,14 @@ tmpfile_mod_own (const int fd, const char *path)
if (stat (path, &s) != -1)
{
if (fchown (fd, s.st_uid, s.st_gid) == -1)
if (config.merr) perror (config.tmpfilename);
if (config.merr)
perror (config.tmpfilename);
if (fchmod (fd, s.st_mode) == -1)
if (config.merr) perror (config.tmpfilename);
if (config.merr)
perror (config.tmpfilename);
}
else if (config.merr) perror (path);
else if (config.merr)
perror (path);
}
}

View File

@ -18,11 +18,11 @@
*/
#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
{
@ -39,6 +39,6 @@ void tmpfile_mod_own (const int fd, const char *path);
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_write_message (message_t * m, mbox_t * mbox);
#endif /* MBOX_H */

View File

@ -18,14 +18,14 @@
*/
#ifndef MESSAGE_H
#define MESSAGE_H 1
# define MESSAGE_H 1
#include <config.h>
# include <config.h>
typedef struct
{
char *filename; /* used with directory formats, such as maildir or MH */
char *msgid;
char *filename; /* used with directory formats, such as maildir or MH */
char *msgid;
char *from;
char *headers;
int hbytes;

203
src/mh.c
View File

@ -22,20 +22,23 @@
#include <string.h>
#ifdef HAVE_DIRENT_H
# include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name)
# include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name)
#else
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
# ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif /* HAVE_SYS_NDIR_H */
# ifdef HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif /* HAVE_SYS_DIR_H */
# ifdef HAVE_NDIR_H
# include <ndir.h>
# endif /* HAVE_NDIR_H */
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
# ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif
/* HAVE_SYS_NDIR_H */
# ifdef HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif
/* HAVE_SYS_DIR_H */
# ifdef HAVE_NDIR_H
# include <ndir.h>
# endif
/* HAVE_NDIR_H */
#endif /* HAVE_DIRENT_H */
#include <sys/types.h>
@ -49,12 +52,13 @@
#include "wrap.h"
#ifdef HAVE_LIBDMALLOC
# include <dmalloc.h>
# include <dmalloc.h>
#endif /* HAVE_LIBDMALLOC */
extern option_t config;
DIR *mh_open (const char *path)
DIR *
mh_open (const char *path)
{
DIR *dp;
@ -62,22 +66,24 @@ DIR *mh_open (const char *path)
if (dp == NULL)
{
if (config.merr)
{
fprintf (stderr, "%s: %s: ", APPNAME, path);
perror (NULL);
}
{
fprintf (stderr, "%s: %s: ", APPNAME, path);
perror (NULL);
}
errno = 0;
return NULL;
}
return dp;
} /* mh_open */
} /* mh_open */
void mh_close (DIR *dp)
void
mh_close (DIR * dp)
{
closedir (dp);
} /* mh_close */
} /* mh_close */
message_t *mh_read_message (DIR *dp)
message_t *
mh_read_message (DIR * dp)
{
int isheaders = 1;
int have_from = 0, have_date = 0, have_sender = 0;
@ -91,16 +97,17 @@ message_t *mh_read_message (DIR *dp)
filename = NULL;
for(;;)
for (;;)
{
d_content = readdir(dp);
if (d_content == NULL) return NULL;
d_content = readdir (dp);
if (d_content == NULL)
return NULL;
if (d_content->d_name[0] == '.')
continue;
continue;
filename = (char *) xrealloc
(filename, ((strlen (d_content->d_name)) +
(strlen (config.boxname)) + 2));
filename = (char *) xrealloc
(filename, ((strlen (d_content->d_name)) +
(strlen (config.boxname)) + 2));
/* message->headers = (char *) xrealloc (message->headers, 0); */
/* message->hbytes = 0; */
@ -112,13 +119,13 @@ message_t *mh_read_message (DIR *dp)
fp = m_fopen (filename, "r");
isheaders = 1;
if (fp == NULL)
{
free (message->headers);
free (message->body);
message->hbytes = 0;
message->bbytes = 0;
continue;
}
{
free (message->headers);
free (message->body);
message->hbytes = 0;
message->bbytes = 0;
continue;
}
fgets (buffer, BUFSIZ, fp);
@ -137,76 +144,80 @@ message_t *mh_read_message (DIR *dp)
/* continue; */
/* } */
/* } */
fseek (fp, 0, SEEK_SET);
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 ("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 ("Return-Path: ", buffer, 13))
message->from = parse_return_path (buffer);
message->headers =
(char *) realloc (message->headers,
((1 + s + message->hbytes) * sizeof (char)));
strcpy (message->headers + message->hbytes, buffer);
message->hbytes += s;
} /* if */
else
{
message->body =
(char *) realloc (message->body,
((1 + s + message->bbytes) * sizeof (char)));
strcpy (message->body + message->bbytes, buffer);
message->bbytes += s;
} /* else */
} /* while */
message->headers =
(char *) realloc (message->headers,
((1 + s +
message->hbytes) * sizeof (char)));
strcpy (message->headers + message->hbytes, buffer);
message->hbytes += s;
} /* if */
else
{
message->body =
(char *) realloc (message->body,
((1 + s +
message->bbytes) * sizeof (char)));
strcpy (message->body + message->bbytes, buffer);
message->bbytes += s;
} /* else */
} /* while */
if ((!have_from && !have_sender)|| !have_date)
{
if (config.merr)
fprintf (stderr, "%s: %s: Not a RFC 2822 message\n",
APPNAME, filename);
fclose (fp);
free (message->headers);
message->headers = NULL;
free (message->body);
message->body = NULL;
message->hbytes = 0;
message->bbytes = 0;
continue;
}
if ((!have_from && !have_sender) || !have_date)
{
if (config.merr)
fprintf (stderr, "%s: %s: Not a RFC 2822 message\n",
APPNAME, filename);
fclose (fp);
free (message->headers);
message->headers = NULL;
free (message->body);
message->body = NULL;
message->hbytes = 0;
message->bbytes = 0;
continue;
}
else
{
message->filename = (char *) xstrdup (filename);
fclose (fp);
free (filename);
{
message->filename = (char *) xstrdup (filename);
fclose (fp);
free (filename);
return message;
}
} /* for */
} /* mh_read_message */
return message;
}
} /* for */
} /* mh_read_message */
void mh_write_message (message_t *m, const char *path)
void
mh_write_message (message_t * m, const char *path)
{
struct dirent *dc;
int x, y = 0;
char s1[BUFSIZ];
DIR *d; FILE *f;
DIR *d;
FILE *f;
d = m_opendir (path);
rewinddir (d);
@ -215,11 +226,11 @@ void mh_write_message (message_t *m, const char *path)
{
x = strtol (dc->d_name, NULL, 10);
if (x > y)
y = x;
y = x;
}
y++;
sprintf (s1, "%s/%i", path, y);
f = m_fopen (s1, "w");
fprintf (f, "%s\n%s", m->headers, m->body);
fclose (f);

View File

@ -18,29 +18,33 @@
*/
#ifndef MH_H
#define MH_H 1
# define MH_H 1
#include <config.h>
# include <config.h>
#ifdef HAVE_DIRENT_H
# include <dirent.h>
#else
# ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif /* HAVE_SYS_NDIR_H */
# ifdef HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif /* HAVE_SYS_DIR_H */
# ifdef HAVE_NDIR_H
# include <ndir.h>
# endif /* HAVE_NDIR_H */
#endif /* HAVE_DIRENT_H */
# ifdef HAVE_DIRENT_H
# include <dirent.h>
# else
# ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif
/* HAVE_SYS_NDIR_H */
# ifdef HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif
/* HAVE_SYS_DIR_H */
# ifdef HAVE_NDIR_H
# include <ndir.h>
# endif
/* HAVE_NDIR_H */
# endif
/* HAVE_DIRENT_H */
#include "mboxgrep.h"
# include "mboxgrep.h"
DIR *mh_open (const char *path);
void mh_close (DIR *dp);
message_t *mh_read_message (DIR *dp);
void mh_write_message (message_t *m, const char *path);
void mh_close (DIR * dp);
message_t *mh_read_message (DIR * dp);
void mh_write_message (message_t * m, const char *path);
#endif /* MH_H */

View File

@ -17,9 +17,9 @@
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define _XOPEN_SOURCE /* Pull in strptime(3) from time.h */
#define _BSD_SOURCE /* Compensate for _XOPEN_SOURCE to pull in strdup(3)
* from string.h. */
#define _XOPEN_SOURCE /* Pull in strptime(3) from time.h */
#define _BSD_SOURCE /* Compensate for _XOPEN_SOURCE to pull in strdup(3)
* from string.h. */
#include <config.h>
@ -68,7 +68,8 @@ folder_format (const char *name)
return f;
}
lockmethod_t lock_method (const char *name)
lockmethod_t
lock_method (const char *name)
{
lockmethod_t l;
@ -87,7 +88,7 @@ lockmethod_t lock_method (const char *name)
else
{
if (config.merr)
fprintf (stderr, "mboxgrep: %s: unknown file locking method\n", name);
fprintf (stderr, "mboxgrep: %s: unknown file locking method\n", name);
exit (2);
}
@ -122,17 +123,19 @@ time_t parse_date(char *datestr)
}
*/
char * parse_return_path(char *rpath)
char *
parse_return_path (char *rpath)
{
char *blah1, blah2[BUFSIZ];
sscanf(rpath, "Return-Path: <%[^\r\n>]>", blah2);
sscanf (rpath, "Return-Path: <%[^\r\n>]>", blah2);
blah1 = xstrdup (blah2);
return blah1;
}
void * allocate_message (void)
void *
allocate_message (void)
{
message_t *message;
@ -151,7 +154,8 @@ void * allocate_message (void)
return message;
}
void postmark_print (message_t *msg)
void
postmark_print (message_t * msg)
{
time_t tt;
struct tm *ct;
@ -178,9 +182,9 @@ set_default_options (void)
config.dedup = 0;
config.recursive = 0;
config.ignorecase = 0;
config.format = MBOX; /* default mailbox format */
config.lock = FCNTL; /* default file locking method */
config.merr = 1; /* report errors by default */
config.format = MBOX; /* default mailbox format */
config.lock = FCNTL; /* default file locking method */
config.merr = 1; /* report errors by default */
config.debug = 0;
}
@ -191,108 +195,108 @@ get_runtime_options (int *argc, char **argv, struct option *long_options)
while (1)
{
c = getopt_long (*argc, argv, "BcdEe:GHhil:m:n:o:Pp:rsVv", long_options,
&option_index);
c = getopt_long (*argc, argv, "BcdEe:GHhil:m:n:o:Pp:rsVv", long_options,
&option_index);
if (c == -1)
break;
switch (c)
{
case '?':
usage();
case 'c':
config.action = COUNT;
break;
case 'd':
config.action = DELETE;
break;
case 'e':
config.regex_s = xstrdup (optarg);
config.haveregex = 1;
break;
case 'o':
config.outboxname = xstrdup (optarg);
config.action = WRITE;
break;
case 'E':
config.extended = 1;
break;
case 'G':
config.extended = 0;
break;
case 'P':
case '?':
usage ();
case 'c':
config.action = COUNT;
break;
case 'd':
config.action = DELETE;
break;
case 'e':
config.regex_s = xstrdup (optarg);
config.haveregex = 1;
break;
case 'o':