Compare commits

..

1 Commits

Author SHA1 Message Date
fe1b5cdbd0
Code cleanup and indentation. 2023-03-03 22:49:48 +01:00
20 changed files with 594 additions and 676 deletions

2
.gitignore vendored
View File

@ -3,6 +3,4 @@ config.h
config.log config.log
config.status config.status
src/*.o src/*.o
src/*.c~
src/*.h~
src/mboxgrep src/mboxgrep

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

203
src/mh.c
View File

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

View File

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

View File

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

View File

@ -18,21 +18,20 @@
*/ */
#ifndef MISC_H #ifndef MISC_H
# define MISC_H 1 #define MISC_H 1
# include "mboxgrep.h" #include "mboxgrep.h"
# include "getopt.h" #include "getopt.h"
# include "message.h" #include "message.h"
/* #include <time.h> */ /* #include <time.h> */
format_t folder_format (const char *name); format_t folder_format (const char *name);
lockmethod_t lock_method (const char *name); lockmethod_t lock_method (const char *name);
/* time_t parse_date(char *datestr); */ /* time_t parse_date(char *datestr); */
char *parse_return_path (char *rpath); char * parse_return_path(char *rpath);
void *allocate_message (void); void * allocate_message (void);
void postmark_print (message_t * msg); void postmark_print (message_t *msg);
void set_default_options (void); void set_default_options (void);
void get_runtime_options (int *argc, char **argv, void get_runtime_options (int *argc, char **argv, struct option *long_options);
struct option *long_options);
#endif /* MISC_H */ #endif /* MISC_H */

View File

@ -22,11 +22,11 @@
#include <string.h> #include <string.h>
#include <regex.h> #include <regex.h>
#ifdef HAVE_LIBPCRE #ifdef HAVE_LIBPCRE
# include <pcre.h> #include <pcre.h>
#endif /* HAVE_LIBPCRE */ #endif /* HAVE_LIBPCRE */
#include "mboxgrep.h" #include "mboxgrep.h"
#include "message.h" #include "message.h"
#include "wrap.h" /* xcalloc() et cetera */ #include "wrap.h" /* xcalloc() et cetera */
#ifdef HAVE_LIBPCRE #ifdef HAVE_LIBPCRE
void void
@ -36,32 +36,35 @@ pcre_init (void)
const char *error; const char *error;
config.pcre_pattern = config.pcre_pattern =
(pcre *) pcre_compile (config.regex_s, (pcre *) pcre_compile (config.regex_s,
(config.ignorecase ? PCRE_CASELESS : 0), (config.ignorecase ? PCRE_CASELESS : 0),
&error, &errptr, NULL); &error, &errptr, NULL);
if (config.pcre_pattern == NULL) if (config.pcre_pattern == NULL)
{ {
if (config.merr) if (config.merr)
fprintf (stderr, "%s: %s: %s\n", APPNAME, config.regex_s, error); fprintf (stderr, "%s: %s: %s\n", APPNAME, config.regex_s,
exit (2); error);
exit(2);
} }
} }
void void
pcre_match (message_t * msg) pcre_match (message_t *msg)
{ {
int of[BUFSIZ]; int of[BUFSIZ];
if (config.headers) if (config.headers)
config.res1 = config.res1 =
pcre_exec ((pcre *) config.pcre_pattern, pcre_exec ((pcre *) config.pcre_pattern,
(pcre_extra *) config.pcre_hints, (pcre_extra *) config.pcre_hints,
msg->headers, (int) strlen (msg->headers), 0, 0, of, BUFSIZ); msg->headers,
(int) strlen (msg->headers), 0, 0, of, BUFSIZ);
if (config.body) if (config.body)
config.res2 = config.res2 =
pcre_exec ((pcre *) config.pcre_pattern, pcre_exec ((pcre *) config.pcre_pattern,
(pcre_extra *) config.pcre_hints, (pcre_extra *) config.pcre_hints,
msg->body, (int) strlen (msg->body), 0, 0, of, BUFSIZ); msg->body,
(int) strlen (msg->body), 0, 0, of, BUFSIZ);
config.res1 = config.res1 ^ 1; config.res1 = config.res1 ^ 1;
config.res2 = config.res2 ^ 1; config.res2 = config.res2 ^ 1;
@ -74,21 +77,20 @@ regex_init (void)
int flag1 = 0, flag2 = 0; int flag1 = 0, flag2 = 0;
int errcode = 0; int errcode = 0;
char errbuf[BUFSIZ]; char errbuf[BUFSIZ];
if (config.ignorecase) if (config.ignorecase)
flag1 = REG_ICASE; flag1 = REG_ICASE;
if (config.extended) if (config.extended)
flag2 = REG_EXTENDED; flag2 = REG_EXTENDED;
config.posix_pattern = (regex_t *) xmalloc (sizeof (regex_t)); config.posix_pattern = (regex_t *) xmalloc (sizeof (regex_t));
errcode = regcomp ((regex_t *) config.posix_pattern, config.regex_s, errcode = regcomp ((regex_t *) config.posix_pattern, config.regex_s,
(flag1 | flag2 | REG_NEWLINE)); (flag1 | flag2 | REG_NEWLINE ));
if (0 != errcode) if (0 != errcode)
{ {
if (config.merr) if (config.merr)
{ {
regerror (errcode, (regex_t *) config.posix_pattern, errbuf, regerror (errcode, (regex_t *) config.posix_pattern, errbuf, BUFSIZ);
BUFSIZ);
fprintf (stderr, "%s: %s: %s\n", APPNAME, config.regex_s, errbuf); fprintf (stderr, "%s: %s: %s\n", APPNAME, config.regex_s, errbuf);
} }
exit (2); exit (2);
@ -96,12 +98,12 @@ regex_init (void)
} }
void void
regex_match (message_t * msg) regex_match (message_t *msg)
{ {
if (config.headers) if (config.headers)
config.res1 = regexec ((regex_t *) config.posix_pattern, config.res1 = regexec ((regex_t *) config.posix_pattern,
msg->headers, 0, NULL, 0); msg->headers, 0, NULL, 0);
if (config.body) if (config.body)
config.res2 = regexec ((regex_t *) config.posix_pattern, config.res2 = regexec ((regex_t *) config.posix_pattern,
msg->body, 0, NULL, 0); msg->body, 0, NULL, 0);
} }

View File

@ -21,7 +21,7 @@
#ifdef HAVE_LIBPCRE #ifdef HAVE_LIBPCRE
void pcre_init (void); void pcre_init (void);
void pcre_match (message_t * msg); void pcre_match (message_t *msg);
#endif /* HAVE_LIBPCRE */ #endif /* HAVE_LIBPCRE */
void regex_init (void); void regex_init (void);
void regex_match (message_t * msg); void regex_match (message_t *msg);

View File

@ -25,23 +25,20 @@
#include <sys/types.h> #include <sys/types.h>
#ifdef HAVE_DIRENT_H #ifdef HAVE_DIRENT_H
# include <dirent.h> # include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name) # define NAMLEN(dirent) strlen((dirent)->d_name)
#else #else
# define dirent direct # define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen # define NAMLEN(dirent) (dirent)->d_namlen
# ifdef HAVE_SYS_NDIR_H # ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h> # include <sys/ndir.h>
# endif # endif /* HAVE_SYS_NDIR_H */
/* HAVE_SYS_NDIR_H */ # ifdef HAVE_SYS_DIR_H
# ifdef HAVE_SYS_DIR_H # include <sys/dir.h>
# include <sys/dir.h> # endif /* HAVE_SYS_DIR_H */
# endif # ifdef HAVE_NDIR_H
/* HAVE_SYS_DIR_H */ # include <ndir.h>
# ifdef HAVE_NDIR_H # endif /* HAVE_NDIR_H */
# include <ndir.h>
# endif
/* HAVE_NDIR_H */
#endif /* HAVE_DIRENT_H */ #endif /* HAVE_DIRENT_H */
#include <time.h> #include <time.h>
@ -59,21 +56,19 @@
#include "misc.h" #include "misc.h"
#include "re.h" #include "re.h"
#ifdef HAVE_FTS_OPEN #ifdef HAVE_FTS_OPEN
# include <sys/stat.h> # include <sys/stat.h>
# include <fts.h> # include <fts.h>
#else #else
# ifdef HAVE_FTW # ifdef HAVE_FTW
# include <ftw.h> # include <ftw.h>
# endif # endif /* HAVE_FTW */
/* HAVE_FTW */
#endif /* HAVE_FTS_OPEN */ #endif /* HAVE_FTS_OPEN */
#ifdef HAVE_LIBDMALLOC #ifdef HAVE_LIBDMALLOC
# include <dmalloc.h> #include <dmalloc.h>
#endif /* HAVE_LIBDMALLOC */ #endif /* HAVE_LIBDMALLOC */
void void scan_mailbox (char path[])
scan_mailbox (char path[])
{ {
static FILE *outf; static FILE *outf;
static mbox_t *mbox, *out; static mbox_t *mbox, *out;
@ -85,7 +80,7 @@ scan_mailbox (char path[])
if (config.format == MAILDIR && config.action == WRITE) if (config.format == MAILDIR && config.action == WRITE)
{ {
foo = opendir (config.outboxname); /* do NOT change this to m_opendir! */ foo = opendir (config.outboxname); /* do NOT change this to m_opendir! */
if (foo == NULL && errno == ENOENT) if (foo == NULL && errno == ENOENT)
maildir_create (config.outboxname); maildir_create (config.outboxname);
else else
@ -95,7 +90,7 @@ scan_mailbox (char path[])
{ {
if (config.merr) if (config.merr)
fprintf (stderr, "%s: %s: Not a maildir folder\n", APPNAME, fprintf (stderr, "%s: %s: Not a maildir folder\n", APPNAME,
config.outboxname); config.outboxname);
exit (2); exit (2);
} }
} }
@ -104,19 +99,17 @@ scan_mailbox (char path[])
if (config.action == DELETE) if (config.action == DELETE)
delete = 1; delete = 1;
if ((config.format == MBOX) || (config.format == ZMBOX) if ((config.format == MBOX) || (config.format == ZMBOX) ||
|| (config.format == BZ2MBOX)) (config.format == BZ2MBOX))
{ {
mbox = (mbox_t *) mbox_open (path, "r"); mbox = (mbox_t *) mbox_open (path, "r");
if (mbox == NULL) if (mbox == NULL) return;
return;
} }
else if ((config.format == MH) || (config.format == NNMH) else if ((config.format == MH) || (config.format == NNMH) ||
|| (config.format == NNML)) (config.format == NNML))
{ {
boxd = mh_open (path); boxd = mh_open (path);
if (boxd == NULL) if (boxd == NULL) return;
return;
} }
else if (config.format == MAILDIR) else if (config.format == MAILDIR)
{ {
@ -131,27 +124,25 @@ scan_mailbox (char path[])
config.res1 = 1; config.res1 = 1;
config.res2 = 1; config.res2 = 1;
if ((config.format == MBOX) || (config.format == ZMBOX) if ((config.format == MBOX) || (config.format == ZMBOX) ||
|| (config.format == BZ2MBOX)) (config.format == BZ2MBOX))
msg = (message_t *) mbox_read_message (mbox); msg = (message_t *) mbox_read_message (mbox);
else if ((config.format == MH) || (config.format == NNMH) else if ((config.format == MH) || (config.format == NNMH) ||
|| (config.format == NNML)) (config.format == NNML))
msg = (message_t *) mh_read_message (boxd); msg = (message_t *) mh_read_message (boxd);
else if (config.format == MAILDIR) else if (config.format == MAILDIR)
msg = (message_t *) maildir_read_message (maildird); msg = (message_t *) maildir_read_message (maildird);
if (msg == NULL) if (msg == NULL) break;
break;
if (msg->from == NULL) if (msg->from == NULL) msg->from = (char *) xstrdup ("nobody");
msg->from = (char *) xstrdup ("nobody");
#ifdef HAVE_LIBPCRE #ifdef HAVE_LIBPCRE
if (config.perl) if (config.perl)
pcre_match (msg); pcre_match (msg);
else else
#endif /* HAVE_LIBPCRE */ #endif /* HAVE_LIBPCRE */
regex_match (msg); regex_match (msg);
if (config.dedup) if (config.dedup)
isdup = md5_check_message (msg->body, runtime.cs); isdup = md5_check_message (msg->body, runtime.cs);
@ -173,11 +164,11 @@ scan_mailbox (char path[])
{ {
if (config.format == MAILDIR) if (config.format == MAILDIR)
maildir_write_message (msg, config.outboxname); maildir_write_message (msg, config.outboxname);
else if (config.format == MH || config.format == NNMH else if (config.format == MH || config.format == NNMH ||
|| config.format == NNML) config.format == NNML)
mh_write_message (msg, config.outboxname); mh_write_message (msg, config.outboxname);
else if ((config.format == MBOX) || (config.format == ZMBOX) else if ((config.format == MBOX) || (config.format == ZMBOX) ||
|| (config.format == BZ2MBOX)) (config.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); */
@ -196,40 +187,37 @@ scan_mailbox (char path[])
perror (NULL); perror (NULL);
} }
exit (2); exit (2);
} /* if */ } /* if */
fprintf (outf, "%s\n%s", msg->headers, msg->body); fprintf (outf, "%s\n%s", msg->headers, msg->body);
pclose (outf); pclose (outf);
} }
else if (config.action == COUNT) else if (config.action == COUNT)
runtime.count++; runtime.count++;
else if (config.action == DELETE && else if (config.action == DELETE &&
((config.format == MBOX) || (config.format == ZMBOX) ((config.format == MBOX) || (config.format == ZMBOX) ||
|| (config.format == BZ2MBOX))) (config.format == BZ2MBOX)))
mbox_write_message (msg, runtime.tmp_mbox); mbox_write_message (msg, runtime.tmp_mbox);
} }
else else if (((((config.res1 == 0) | (config.res2 == 0)) ^ config.invert) && delete) &&
if (((((config.res1 == 0) | (config.res2 == 0)) ^ config.invert) ((config.format == MH) || (config.format == NNMH) ||
&& delete) && ((config.format == MH) || (config.format == NNMH) (config.format == NNML) || (config.format == MAILDIR)))
|| (config.format == NNML) m_unlink(msg->filename);
|| (config.format == MAILDIR)))
m_unlink (msg->filename);
free (msg->body); free(msg->body);
free (msg->headers); free(msg->headers);
free (msg); free(msg);
} /* for */ } /* for */
if ((config.format == MBOX) || (config.format == ZMBOX) if ((config.format == MBOX) || (config.format == ZMBOX) ||
|| (config.format == BZ2MBOX)) (config.format == BZ2MBOX))
mbox_close (mbox); mbox_close (mbox);
else if ((config.format == MH) || (config.format == NNMH) else if ((config.format == MH) || (config.format == NNMH) ||
|| (config.format == NNML)) (config.format == NNML))
mh_close (boxd); mh_close(boxd);
} }
void void recursive_scan (char path[])
recursive_scan (char path[])
{ {
#ifdef HAVE_FTS_OPEN #ifdef HAVE_FTS_OPEN
FTS *ftsfoo; FTS *ftsfoo;
@ -247,9 +235,9 @@ recursive_scan (char path[])
if (ftsfoo == NULL) if (ftsfoo == NULL)
{ {
/* fixme (?) */ /* fixme (?) */
perror (APPNAME); perror(APPNAME);
exit (2); exit (2);
} }
while ((ftsbar = fts_read (ftsfoo))) while ((ftsbar = fts_read (ftsfoo)))
@ -262,8 +250,7 @@ recursive_scan (char path[])
#endif /* HAVE_FTS_OPEN */ #endif /* HAVE_FTS_OPEN */
} }
int int md5_check_message (char *body, checksum_t *chksum)
md5_check_message (char *body, checksum_t * chksum)
{ {
struct md5_ctx a; struct md5_ctx a;
char b[16]; char b[16];
@ -274,18 +261,17 @@ md5_check_message (char *body, checksum_t * chksum)
if (body == NULL) if (body == NULL)
md5_process_bytes ("", 0, &a); md5_process_bytes ("", 0, &a);
else else
md5_process_bytes (body, strlen (body), &a); md5_process_bytes (body, strlen(body), &a);
md5_finish_ctx (&a, b); md5_finish_ctx(&a, b);
for (i = 0; i < chksum->n; i++) for (i = 0; i < chksum->n; i++)
{ {
if (0 == strncmp ((char *) chksum->md5[i], b, 16)) if (0 == strncmp ((char *)chksum->md5[i], b, 16))
return 1; return 1;
} }
chksum->md5 = chksum->md5 = (char **) xrealloc (chksum->md5, (1 + chksum->n) * sizeof (char *));
(char **) xrealloc (chksum->md5, (1 + chksum->n) * sizeof (char *));
chksum->md5[chksum->n] = xstrdup (b); chksum->md5[chksum->n] = xstrdup (b);

View File

@ -18,12 +18,12 @@
*/ */
#ifndef SCAN_H #ifndef SCAN_H
# define SCAN_H 1 #define SCAN_H 1
# include "mboxgrep.h" #include "mboxgrep.h"
void scan_mailbox (char path[]); void scan_mailbox (char path[]);
void recursive_scan (char path[]); void recursive_scan (char path[]);
int md5_check_message (char *body, checksum_t * chksum); int md5_check_message (char *body, checksum_t *chksum);
#endif /* SCAN_H */ #endif /* SCAN_H */

View File

@ -24,29 +24,26 @@
#include <errno.h> #include <errno.h>
#ifdef HAVE_DIRENT_H #ifdef HAVE_DIRENT_H
# include <dirent.h> # include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name) # define NAMLEN(dirent) strlen((dirent)->d_name)
#else #else
# define dirent direct # define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen # define NAMLEN(dirent) (dirent)->d_namlen
# ifdef HAVE_SYS_NDIR_H # ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h> # include <sys/ndir.h>
# endif # endif /* HAVE_SYS_NDIR_H */
/* HAVE_SYS_NDIR_H */ # ifdef HAVE_SYS_DIR_H
# ifdef HAVE_SYS_DIR_H # include <sys/dir.h>
# include <sys/dir.h> # endif /* HAVE_SYS_DIR_H */
# endif # ifdef HAVE_NDIR_H
/* HAVE_SYS_DIR_H */ # include <ndir.h>
# ifdef HAVE_NDIR_H # endif /* HAVE_NDIR_H */
# include <ndir.h>
# endif
/* HAVE_NDIR_H */
#endif /* HAVE_DIRENT_H */ #endif /* HAVE_DIRENT_H */
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
# include <zlib.h> #include <zlib.h>
#endif /* HAVE_LIBZ */ #endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2 #ifdef HAVE_LIBBZ2
# include <bzlib.h> #include <bzlib.h>
#endif /* HAVE_LIBBZ2 */ #endif /* HAVE_LIBBZ2 */
#include <sys/types.h> #include <sys/types.h>
@ -57,13 +54,12 @@
#include "mboxgrep.h" #include "mboxgrep.h"
#ifndef APPNAME #ifndef APPNAME
# define APPNAME "mboxgrep" #define APPNAME "mboxgrep"
#endif #endif
#define BUFLEN 16384 #define BUFLEN 16384
int int m_open (const char *pathname, int flags, mode_t mode)
m_open (const char *pathname, int flags, mode_t mode)
{ {
int blah; int blah;
@ -80,10 +76,9 @@ m_open (const char *pathname, int flags, mode_t mode)
exit (2); exit (2);
} }
return blah; return blah;
} }
FILE * FILE *m_fopen (const char *path, const char *mode)
m_fopen (const char *path, const char *mode)
{ {
FILE *blah; FILE *blah;
@ -101,8 +96,7 @@ m_fopen (const char *path, const char *mode)
return blah; return blah;
} }
FILE * FILE *m_fdopen (int fildes, const char *mode)
m_fdopen (int fildes, const char *mode)
{ {
FILE *blah; FILE *blah;
@ -117,8 +111,7 @@ m_fdopen (int fildes, const char *mode)
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
gzFile gzFile m_gzdopen (int fildes, const char *mode)
m_gzdopen (int fildes, const char *mode)
{ {
gzFile blah; gzFile blah;
@ -133,8 +126,7 @@ m_gzdopen (int fildes, const char *mode)
#endif /* HAVE_LIBZ */ #endif /* HAVE_LIBZ */
DIR * DIR *m_opendir (const char *name)
m_opendir (const char *name)
{ {
DIR *blah; DIR *blah;
@ -152,8 +144,7 @@ m_opendir (const char *name)
#ifndef HAVE_LIBDMALLOC #ifndef HAVE_LIBDMALLOC
void * void *xmalloc (size_t size)
xmalloc (size_t size)
{ {
void *foo; void *foo;
@ -167,8 +158,7 @@ xmalloc (size_t size)
return foo; return foo;
} }
void * void *xrealloc (void *ptr, size_t size)
xrealloc (void *ptr, size_t size)
{ {
void *foo; void *foo;
@ -182,8 +172,7 @@ xrealloc (void *ptr, size_t size)
return foo; return foo;
} }
void * void *xcalloc (size_t nmemb, size_t size)
xcalloc (size_t nmemb, size_t size)
{ {
void *foo; void *foo;
@ -197,8 +186,7 @@ xcalloc (size_t nmemb, size_t size)
return foo; return foo;
} }
char * char *xstrdup (const char *s)
xstrdup (const char *s)
{ {
char *foo; char *foo;
@ -214,8 +202,7 @@ xstrdup (const char *s)
#endif /* HAVE_LIBDMALLOC */ #endif /* HAVE_LIBDMALLOC */
int int m_unlink (const char *pathname)
m_unlink (const char *pathname)
{ {
int baz; int baz;
@ -233,8 +220,7 @@ m_unlink (const char *pathname)
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
void void gzwrite_loop (void *fp, char *str)
gzwrite_loop (void *fp, char *str)
{ {
int quux, len, baz; int quux, len, baz;
@ -242,8 +228,8 @@ gzwrite_loop (void *fp, char *str)
baz = strlen (str); baz = strlen (str);
for (;;) for (;;)
{ {
len = gzwrite (fp, (str + quux), len = gzwrite (fp, (str+quux),
(((quux + BUFLEN) < baz) ? BUFLEN : (baz - quux))); (((quux + BUFLEN) < baz) ? BUFLEN : (baz - quux)));
quux += len; quux += len;
if (quux == baz) if (quux == baz)
break; break;
@ -254,8 +240,7 @@ gzwrite_loop (void *fp, char *str)
#ifdef HAVE_LIBBZ2 #ifdef HAVE_LIBBZ2
void void bzwrite_loop (void *fp, char *str)
bzwrite_loop (void *fp, char *str)
{ {
int quux, len, baz; int quux, len, baz;
@ -263,8 +248,8 @@ bzwrite_loop (void *fp, char *str)
baz = strlen (str); baz = strlen (str);
for (;;) for (;;)
{ {
len = BZ2_bzwrite (fp, (str + quux), len = BZ2_bzwrite (fp, (str+quux),
(((quux + BUFLEN) < baz) ? BUFLEN : (baz - quux))); (((quux + BUFLEN) < baz) ? BUFLEN : (baz - quux)));
quux += len; quux += len;
if (quux == baz) if (quux == baz)
break; break;

View File

@ -22,64 +22,55 @@
/* wrappers around certain std functions */ /* wrappers around certain std functions */
#ifndef WRAP_H #ifndef WRAP_H
# define WRAP_H #define WRAP_H
# include <config.h> #include <config.h>
# include <stdio.h> #include <stdio.h>
# include <sys/types.h> #include <sys/types.h>
# ifdef HAVE_DIRENT_H #ifdef HAVE_DIRENT_H
# include <dirent.h> # include <dirent.h>
# else #else
# ifdef HAVE_SYS_NDIR_H # ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h> # include <sys/ndir.h>
# endif # endif /* HAVE_SYS_NDIR_H */
/* HAVE_SYS_NDIR_H */ # ifdef HAVE_SYS_DIR_H
# ifdef HAVE_SYS_DIR_H # include <sys/dir.h>
# include <sys/dir.h> # endif /* HAVE_SYS_DIR_H */
# endif # ifdef HAVE_NDIR_H
/* HAVE_SYS_DIR_H */ # include <ndir.h>
# ifdef HAVE_NDIR_H # endif /* HAVE_NDIR_H */
# include <ndir.h> #endif /* HAVE_DIRENT_H */
# endif #ifdef HAVE_LIBZ
/* HAVE_NDIR_H */ #include <zlib.h>
# endif #endif /* HAVE_LIBZ */
/* HAVE_DIRENT_H */
# ifdef HAVE_LIBZ
# include <zlib.h>
# endif
/* HAVE_LIBZ */
# include <stdlib.h> #include <stdlib.h>
# ifdef HAVE_LIBDMALLOC #ifdef HAVE_LIBDMALLOC
# include <dmalloc.h> # include <dmalloc.h>
# endif #endif /* HAVE_LIBDMALLOC */
/* HAVE_LIBDMALLOC */
int m_open (const char *pathname, int flags, mode_t mode); int m_open (const char *pathname, int flags, mode_t mode);
FILE *m_fopen (const char *path, const char *mode); FILE *m_fopen (const char *path, const char *mode);
FILE *m_fdopen (int fildes, const char *mode); FILE *m_fdopen (int fildes, const char *mode);
# ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
gzFile m_gzdopen (int fildes, const char *mode); gzFile m_gzdopen (int fildes, const char *mode);
void gzwrite_loop (void *fp, char *str); void gzwrite_loop (void *fp, char *str);
# endif #endif /* HAVE_LIBZ */
/* HAVE_LIBZ */ #ifdef HAVE_LIBBZ2
# ifdef HAVE_LIBBZ2
void bzwrite_loop (void *fp, char *str); void bzwrite_loop (void *fp, char *str);
# endif #endif /* HAVE_LIBBZ2 */
/* HAVE_LIBBZ2 */
DIR *m_opendir (const char *name); DIR *m_opendir (const char *name);
# ifndef HAVE_LIBDMALLOC #ifndef HAVE_LIBDMALLOC
void *xmalloc (size_t size); void *xmalloc (size_t size);
void *xrealloc (void *ptr, size_t size); void *xrealloc (void *ptr, size_t size);
void *xcalloc (size_t nmemb, size_t size); void *xcalloc (size_t nmemb, size_t size);
char *xstrdup (const char *s); char *xstrdup (const char *s);
# endif #endif /* HAVE_LIBDMALLOC */
/* HAVE_LIBDMALLOC */
int m_unlink (const char *pathname); int m_unlink (const char *pathname);