Initial import into the new git repository. Back from the dead!
This commit is contained in:
		
							
								
								
									
										57
									
								
								src/Makefile.in
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								src/Makefile.in
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,57 @@
 | 
			
		||||
#  mboxgrep - scan mailbox for messages matching a regular expression
 | 
			
		||||
#  Copyright (C) 2000, 2001, 2002, 2003  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
 | 
			
		||||
#  the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
#  (at your option) any later version.
 | 
			
		||||
#
 | 
			
		||||
#  Mboxgrep is distributed in the hope that it will be useful, but 
 | 
			
		||||
#  WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
#  GNU General Public License for more details.
 | 
			
		||||
#
 | 
			
		||||
#  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: Makefile.in,v 1.9 2003/03/30 23:07:10 dspiljar Exp $
 | 
			
		||||
 | 
			
		||||
srcdir		=	@srcdir@
 | 
			
		||||
 | 
			
		||||
CC		= 	@CC@
 | 
			
		||||
OBJS		=	info.o main.o mh.o scan.o maildir.o mbox.o misc.o \
 | 
			
		||||
			wrap.o getopt.o getopt1.o md5.o
 | 
			
		||||
SRCS		= 	info.c main.c mh.c scan.c maildir.c mbox.c misc.c \
 | 
			
		||||
			wrap.c getopt.c getopt1.c md5.c
 | 
			
		||||
TARGET		=	mboxgrep
 | 
			
		||||
CFLAGS		=	@CFLAGS@
 | 
			
		||||
LIBS		=	@LIBS@
 | 
			
		||||
INSTALL		=	@INSTALL@
 | 
			
		||||
prefix		=	@prefix@
 | 
			
		||||
SHELL 		= 	/bin/sh
 | 
			
		||||
INCLUDES	=	-I. -I$(srcdir)
 | 
			
		||||
 | 
			
		||||
.SUFFIXES:
 | 
			
		||||
.SUFFIXES: .c .o
 | 
			
		||||
 | 
			
		||||
.c.o: 
 | 
			
		||||
	$(CC) $(CFLAGS) $(INCLUDES) -c $<
 | 
			
		||||
 | 
			
		||||
$(TARGET): $(OBJS)
 | 
			
		||||
	$(CC) $(CFLAGS) -o $(TARGET) $(OBJS) $(LIBS)
 | 
			
		||||
 | 
			
		||||
.PHONY: clean
 | 
			
		||||
clean:
 | 
			
		||||
	rm -f $(TARGET) $(OBJS) core
 | 
			
		||||
 | 
			
		||||
.PHONY: distclean
 | 
			
		||||
distclean:
 | 
			
		||||
	rm -f *~ Makefile config.h
 | 
			
		||||
 | 
			
		||||
.PHONY: install
 | 
			
		||||
install: mboxgrep
 | 
			
		||||
	$(INSTALL) -d $(prefix)/bin
 | 
			
		||||
	$(INSTALL) -s $(TARGET) $(prefix)/bin
 | 
			
		||||
 | 
			
		||||
.NOEXPORT:
 | 
			
		||||
							
								
								
									
										67
									
								
								src/config.h.in
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								src/config.h.in
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,67 @@
 | 
			
		||||
/* src/config.h.in.  Generated from configure.ac by autoheader.  */
 | 
			
		||||
 | 
			
		||||
/* Define to 1 if you have the <dirent.h> header file, and it defines `DIR'.
 | 
			
		||||
   */
 | 
			
		||||
#undef HAVE_DIRENT_H
 | 
			
		||||
 | 
			
		||||
/* Define to 1 if you have the `fcntl' function. */
 | 
			
		||||
#undef HAVE_FCNTL
 | 
			
		||||
 | 
			
		||||
/* Define to 1 if you have the `flock' function. */
 | 
			
		||||
#undef HAVE_FLOCK
 | 
			
		||||
 | 
			
		||||
/* Define to 1 if you have the `fts_open' function. */
 | 
			
		||||
#undef HAVE_FTS_OPEN
 | 
			
		||||
 | 
			
		||||
/* Define to 1 if you have the `ftw' function. */
 | 
			
		||||
#undef HAVE_FTW
 | 
			
		||||
 | 
			
		||||
/* Define to 1 if you have the `bz2' library (-lbz2). */
 | 
			
		||||
#undef HAVE_LIBBZ2
 | 
			
		||||
 | 
			
		||||
/* Define to 1 if you have the `dmalloc' library (-ldmalloc). */
 | 
			
		||||
#undef HAVE_LIBDMALLOC
 | 
			
		||||
 | 
			
		||||
/* Define to 1 if you have the `garfield' library (-lgarfield). */
 | 
			
		||||
#undef HAVE_LIBGARFIELD
 | 
			
		||||
 | 
			
		||||
/* Define to 1 if you have the `pcre' library (-lpcre). */
 | 
			
		||||
#undef HAVE_LIBPCRE
 | 
			
		||||
 | 
			
		||||
/* Define to 1 if you have the `z' library (-lz). */
 | 
			
		||||
#undef HAVE_LIBZ
 | 
			
		||||
 | 
			
		||||
/* Define to 1 if you have the <ndir.h> header file, and it defines `DIR'. */
 | 
			
		||||
#undef HAVE_NDIR_H
 | 
			
		||||
 | 
			
		||||
/* Define to 1 if you have the `regcomp' function. */
 | 
			
		||||
#undef HAVE_REGCOMP
 | 
			
		||||
 | 
			
		||||
/* Define to 1 if you have the `strptime' function. */
 | 
			
		||||
#undef HAVE_STRPTIME
 | 
			
		||||
 | 
			
		||||
/* Define to 1 if you have the <sys/dir.h> header file, and it defines `DIR'.
 | 
			
		||||
   */
 | 
			
		||||
#undef HAVE_SYS_DIR_H
 | 
			
		||||
 | 
			
		||||
/* Define to 1 if you have the <sys/ndir.h> header file, and it defines `DIR'.
 | 
			
		||||
   */
 | 
			
		||||
#undef HAVE_SYS_NDIR_H
 | 
			
		||||
 | 
			
		||||
/* Define to the address where bug reports for this package should be sent. */
 | 
			
		||||
#undef PACKAGE_BUGREPORT
 | 
			
		||||
 | 
			
		||||
/* Define to the full name of this package. */
 | 
			
		||||
#undef PACKAGE_NAME
 | 
			
		||||
 | 
			
		||||
/* Define to the full name and version of this package. */
 | 
			
		||||
#undef PACKAGE_STRING
 | 
			
		||||
 | 
			
		||||
/* Define to the one symbol short name of this package. */
 | 
			
		||||
#undef PACKAGE_TARNAME
 | 
			
		||||
 | 
			
		||||
/* Define to the version of this package. */
 | 
			
		||||
#undef PACKAGE_VERSION
 | 
			
		||||
 | 
			
		||||
/* Define to 1 if you have the ANSI C header files. */
 | 
			
		||||
#undef STDC_HEADERS
 | 
			
		||||
							
								
								
									
										731
									
								
								src/getopt.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										731
									
								
								src/getopt.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,731 @@
 | 
			
		||||
/* Getopt for GNU.
 | 
			
		||||
   NOTE: getopt is now part of the C library, so if you don't know what
 | 
			
		||||
   "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu
 | 
			
		||||
   before changing it!
 | 
			
		||||
 | 
			
		||||
   Copyright (C) 1987, 88, 89, 90, 91, 92, 1993
 | 
			
		||||
   	Free Software Foundation, Inc.
 | 
			
		||||
 | 
			
		||||
   This program is free software; you can redistribute it and/or modify it
 | 
			
		||||
   under the terms of the GNU General Public License as published by the
 | 
			
		||||
   Free Software Foundation; either version 2, or (at your option) any
 | 
			
		||||
   later version.
 | 
			
		||||
 | 
			
		||||
   This program is distributed in the hope that it will be useful,
 | 
			
		||||
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
   GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
   You should have received a copy of the GNU General Public License
 | 
			
		||||
   along with this program; if not, write to the Free Software
 | 
			
		||||
   Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 | 
			
		||||
 | 
			
		||||
/* NOTE!!!  AIX requires this to be the first thing in the file.
 | 
			
		||||
   Do not put ANYTHING before it!  */
 | 
			
		||||
#if !defined (__GNUC__) && defined (_AIX)
 | 
			
		||||
 #pragma alloca
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef HAVE_CONFIG_H
 | 
			
		||||
#include "config.h"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef __GNUC__
 | 
			
		||||
#define alloca __builtin_alloca
 | 
			
		||||
#else /* not __GNUC__ */
 | 
			
		||||
#if defined (HAVE_ALLOCA_H) || (defined(sparc) && (defined(sun) || (!defined(USG) && !defined(SVR4) && !defined(__svr4__))))
 | 
			
		||||
#include <alloca.h>
 | 
			
		||||
#else
 | 
			
		||||
#ifndef _AIX
 | 
			
		||||
char *alloca ();
 | 
			
		||||
#endif
 | 
			
		||||
#endif /* alloca.h */
 | 
			
		||||
#endif /* not __GNUC__ */
 | 
			
		||||
 | 
			
		||||
#if !__STDC__ && !defined(const) && IN_GCC
 | 
			
		||||
#define const
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.  */
 | 
			
		||||
#ifndef _NO_PROTO
 | 
			
		||||
#define _NO_PROTO
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
/* Comment out all this code if we are using the GNU C Library, and are not
 | 
			
		||||
   actually compiling the library itself.  This code is part of the GNU C
 | 
			
		||||
   Library, but also included in many other GNU distributions.  Compiling
 | 
			
		||||
   and linking in this code is a waste when using the GNU C library
 | 
			
		||||
   (especially if it is a shared library).  Rather than having every GNU
 | 
			
		||||
   program understand `configure --with-gnu-libc' and omit the object files,
 | 
			
		||||
   it is simpler to just do this in the source for each such file.  */
 | 
			
		||||
 | 
			
		||||
#if defined (_LIBC) || !defined (__GNU_LIBRARY__)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* This needs to come after some library #include
 | 
			
		||||
   to get __GNU_LIBRARY__ defined.  */
 | 
			
		||||
#ifdef	__GNU_LIBRARY__
 | 
			
		||||
#undef	alloca
 | 
			
		||||
/* Don't include stdlib.h for non-GNU C libraries because some of them
 | 
			
		||||
   contain conflicting prototypes for getopt.  */
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#else	/* Not GNU C library.  */
 | 
			
		||||
#define	__alloca	alloca
 | 
			
		||||
#endif	/* GNU C library.  */
 | 
			
		||||
 | 
			
		||||
/* If GETOPT_COMPAT is defined, `+' as well as `--' can introduce a
 | 
			
		||||
   long-named option.  Because this is not POSIX.2 compliant, it is
 | 
			
		||||
   being phased out.  */
 | 
			
		||||
/* #define GETOPT_COMPAT */
 | 
			
		||||
 | 
			
		||||
/* This version of `getopt' appears to the caller like standard Unix `getopt'
 | 
			
		||||
   but it behaves differently for the user, since it allows the user
 | 
			
		||||
   to intersperse the options with the other arguments.
 | 
			
		||||
 | 
			
		||||
   As `getopt' works, it permutes the elements of ARGV so that,
 | 
			
		||||
   when it is done, all the options precede everything else.  Thus
 | 
			
		||||
   all application programs are extended to handle flexible argument order.
 | 
			
		||||
 | 
			
		||||
   Setting the environment variable POSIXLY_CORRECT disables permutation.
 | 
			
		||||
   Then the behavior is completely standard.
 | 
			
		||||
 | 
			
		||||
   GNU application programs can use a third alternative mode in which
 | 
			
		||||
   they can distinguish the relative order of options and other arguments.  */
 | 
			
		||||
 | 
			
		||||
#include "getopt.h"
 | 
			
		||||
 | 
			
		||||
/* For communication from `getopt' to the caller.
 | 
			
		||||
   When `getopt' finds an option that takes an argument,
 | 
			
		||||
   the argument value is returned here.
 | 
			
		||||
   Also, when `ordering' is RETURN_IN_ORDER,
 | 
			
		||||
   each non-option ARGV-element is returned here.  */
 | 
			
		||||
 | 
			
		||||
char *optarg = 0;
 | 
			
		||||
 | 
			
		||||
/* Index in ARGV of the next element to be scanned.
 | 
			
		||||
   This is used for communication to and from the caller
 | 
			
		||||
   and for communication between successive calls to `getopt'.
 | 
			
		||||
 | 
			
		||||
   On entry to `getopt', zero means this is the first call; initialize.
 | 
			
		||||
 | 
			
		||||
   When `getopt' returns EOF, this is the index of the first of the
 | 
			
		||||
   non-option elements that the caller should itself scan.
 | 
			
		||||
 | 
			
		||||
   Otherwise, `optind' communicates from one call to the next
 | 
			
		||||
   how much of ARGV has been scanned so far.  */
 | 
			
		||||
 | 
			
		||||
/* XXX 1003.2 says this must be 1 before any call.  */
 | 
			
		||||
int optind = 0;
 | 
			
		||||
 | 
			
		||||
/* The next char to be scanned in the option-element
 | 
			
		||||
   in which the last option character we returned was found.
 | 
			
		||||
   This allows us to pick up the scan where we left off.
 | 
			
		||||
 | 
			
		||||
   If this is zero, or a null string, it means resume the scan
 | 
			
		||||
   by advancing to the next ARGV-element.  */
 | 
			
		||||
 | 
			
		||||
static char *nextchar;
 | 
			
		||||
 | 
			
		||||
/* Callers store zero here to inhibit the error message
 | 
			
		||||
   for unrecognized options.  */
 | 
			
		||||
 | 
			
		||||
int opterr = 1;
 | 
			
		||||
 | 
			
		||||
/* Set to an option character which was unrecognized.
 | 
			
		||||
   This must be initialized on some systems to avoid linking in the
 | 
			
		||||
   system's own getopt implementation.  */
 | 
			
		||||
 | 
			
		||||
int optopt = '?';
 | 
			
		||||
 | 
			
		||||
/* Describe how to deal with options that follow non-option ARGV-elements.
 | 
			
		||||
 | 
			
		||||
   If the caller did not specify anything,
 | 
			
		||||
   the default is REQUIRE_ORDER if the environment variable
 | 
			
		||||
   POSIXLY_CORRECT is defined, PERMUTE otherwise.
 | 
			
		||||
 | 
			
		||||
   REQUIRE_ORDER means don't recognize them as options;
 | 
			
		||||
   stop option processing when the first non-option is seen.
 | 
			
		||||
   This is what Unix does.
 | 
			
		||||
   This mode of operation is selected by either setting the environment
 | 
			
		||||
   variable POSIXLY_CORRECT, or using `+' as the first character
 | 
			
		||||
   of the list of option characters.
 | 
			
		||||
 | 
			
		||||
   PERMUTE is the default.  We permute the contents of ARGV as we scan,
 | 
			
		||||
   so that eventually all the non-options are at the end.  This allows options
 | 
			
		||||
   to be given in any order, even with programs that were not written to
 | 
			
		||||
   expect this.
 | 
			
		||||
 | 
			
		||||
   RETURN_IN_ORDER is an option available to programs that were written
 | 
			
		||||
   to expect options and other ARGV-elements in any order and that care about
 | 
			
		||||
   the ordering of the two.  We describe each non-option ARGV-element
 | 
			
		||||
   as if it were the argument of an option with character code 1.
 | 
			
		||||
   Using `-' as the first character of the list of option characters
 | 
			
		||||
   selects this mode of operation.
 | 
			
		||||
 | 
			
		||||
   The special argument `--' forces an end of option-scanning regardless
 | 
			
		||||
   of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
 | 
			
		||||
   `--' can cause `getopt' to return EOF with `optind' != ARGC.  */
 | 
			
		||||
 | 
			
		||||
static enum
 | 
			
		||||
{
 | 
			
		||||
  REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
 | 
			
		||||
} ordering;
 | 
			
		||||
 | 
			
		||||
#ifdef	__GNU_LIBRARY__
 | 
			
		||||
/* We want to avoid inclusion of string.h with non-GNU libraries
 | 
			
		||||
   because there are many ways it can cause trouble.
 | 
			
		||||
   On some systems, it contains special magic macros that don't work
 | 
			
		||||
   in GCC.  */
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#define	my_index	strchr
 | 
			
		||||
#define	my_bcopy(src, dst, n)	memcpy ((dst), (src), (n))
 | 
			
		||||
#else
 | 
			
		||||
 | 
			
		||||
/* Avoid depending on library functions or files
 | 
			
		||||
   whose names are inconsistent.  */
 | 
			
		||||
 | 
			
		||||
char *getenv ();
 | 
			
		||||
 | 
			
		||||
static char *
 | 
			
		||||
my_index (str, chr)
 | 
			
		||||
     const char *str;
 | 
			
		||||
     int chr;
 | 
			
		||||
{
 | 
			
		||||
  while (*str)
 | 
			
		||||
    {
 | 
			
		||||
      if (*str == chr)
 | 
			
		||||
	return (char *) str;
 | 
			
		||||
      str++;
 | 
			
		||||
    }
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
my_bcopy (from, to, size)
 | 
			
		||||
     const char *from;
 | 
			
		||||
     char *to;
 | 
			
		||||
     int size;
 | 
			
		||||
{
 | 
			
		||||
  int i;
 | 
			
		||||
  for (i = 0; i < size; i++)
 | 
			
		||||
    to[i] = from[i];
 | 
			
		||||
}
 | 
			
		||||
#endif				/* GNU C library.  */
 | 
			
		||||
 | 
			
		||||
/* Handle permutation of arguments.  */
 | 
			
		||||
 | 
			
		||||
/* Describe the part of ARGV that contains non-options that have
 | 
			
		||||
   been skipped.  `first_nonopt' is the index in ARGV of the first of them;
 | 
			
		||||
   `last_nonopt' is the index after the last of them.  */
 | 
			
		||||
 | 
			
		||||
static int first_nonopt;
 | 
			
		||||
static int last_nonopt;
 | 
			
		||||
 | 
			
		||||
/* Exchange two adjacent subsequences of ARGV.
 | 
			
		||||
   One subsequence is elements [first_nonopt,last_nonopt)
 | 
			
		||||
   which contains all the non-options that have been skipped so far.
 | 
			
		||||
   The other is elements [last_nonopt,optind), which contains all
 | 
			
		||||
   the options processed since those non-options were skipped.
 | 
			
		||||
 | 
			
		||||
   `first_nonopt' and `last_nonopt' are relocated so that they describe
 | 
			
		||||
   the new indices of the non-options in ARGV after they are moved.  */
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
exchange (argv)
 | 
			
		||||
     char **argv;
 | 
			
		||||
{
 | 
			
		||||
  int nonopts_size = (last_nonopt - first_nonopt) * sizeof (char *);
 | 
			
		||||
  char **temp = (char **) __alloca (nonopts_size);
 | 
			
		||||
 | 
			
		||||
  /* Interchange the two blocks of data in ARGV.  */
 | 
			
		||||
 | 
			
		||||
  my_bcopy ((char *) &argv[first_nonopt], (char *) temp, nonopts_size);
 | 
			
		||||
  my_bcopy ((char *) &argv[last_nonopt], (char *) &argv[first_nonopt],
 | 
			
		||||
	    (optind - last_nonopt) * sizeof (char *));
 | 
			
		||||
  my_bcopy ((char *) temp,
 | 
			
		||||
	    (char *) &argv[first_nonopt + optind - last_nonopt],
 | 
			
		||||
	    nonopts_size);
 | 
			
		||||
 | 
			
		||||
  /* Update records for the slots the non-options now occupy.  */
 | 
			
		||||
 | 
			
		||||
  first_nonopt += (optind - last_nonopt);
 | 
			
		||||
  last_nonopt = optind;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Scan elements of ARGV (whose length is ARGC) for option characters
 | 
			
		||||
   given in OPTSTRING.
 | 
			
		||||
 | 
			
		||||
   If an element of ARGV starts with '-', and is not exactly "-" or "--",
 | 
			
		||||
   then it is an option element.  The characters of this element
 | 
			
		||||
   (aside from the initial '-') are option characters.  If `getopt'
 | 
			
		||||
   is called repeatedly, it returns successively each of the option characters
 | 
			
		||||
   from each of the option elements.
 | 
			
		||||
 | 
			
		||||
   If `getopt' finds another option character, it returns that character,
 | 
			
		||||
   updating `optind' and `nextchar' so that the next call to `getopt' can
 | 
			
		||||
   resume the scan with the following option character or ARGV-element.
 | 
			
		||||
 | 
			
		||||
   If there are no more option characters, `getopt' returns `EOF'.
 | 
			
		||||
   Then `optind' is the index in ARGV of the first ARGV-element
 | 
			
		||||
   that is not an option.  (The ARGV-elements have been permuted
 | 
			
		||||
   so that those that are not options now come last.)
 | 
			
		||||
 | 
			
		||||
   OPTSTRING is a string containing the legitimate option characters.
 | 
			
		||||
   If an option character is seen that is not listed in OPTSTRING,
 | 
			
		||||
   return '?' after printing an error message.  If you set `opterr' to
 | 
			
		||||
   zero, the error message is suppressed but we still return '?'.
 | 
			
		||||
 | 
			
		||||
   If a char in OPTSTRING is followed by a colon, that means it wants an arg,
 | 
			
		||||
   so the following text in the same ARGV-element, or the text of the following
 | 
			
		||||
   ARGV-element, is returned in `optarg'.  Two colons mean an option that
 | 
			
		||||
   wants an optional arg; if there is text in the current ARGV-element,
 | 
			
		||||
   it is returned in `optarg', otherwise `optarg' is set to zero.
 | 
			
		||||
 | 
			
		||||
   If OPTSTRING starts with `-' or `+', it requests different methods of
 | 
			
		||||
   handling the non-option ARGV-elements.
 | 
			
		||||
   See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
 | 
			
		||||
 | 
			
		||||
   Long-named options begin with `--' instead of `-'.
 | 
			
		||||
   Their names may be abbreviated as long as the abbreviation is unique
 | 
			
		||||
   or is an exact match for some defined option.  If they have an
 | 
			
		||||
   argument, it follows the option name in the same ARGV-element, separated
 | 
			
		||||
   from the option name by a `=', or else the in next ARGV-element.
 | 
			
		||||
   When `getopt' finds a long-named option, it returns 0 if that option's
 | 
			
		||||
   `flag' field is nonzero, the value of the option's `val' field
 | 
			
		||||
   if the `flag' field is zero.
 | 
			
		||||
 | 
			
		||||
   The elements of ARGV aren't really const, because we permute them.
 | 
			
		||||
   But we pretend they're const in the prototype to be compatible
 | 
			
		||||
   with other systems.
 | 
			
		||||
 | 
			
		||||
   LONGOPTS is a vector of `struct option' terminated by an
 | 
			
		||||
   element containing a name which is zero.
 | 
			
		||||
 | 
			
		||||
   LONGIND returns the index in LONGOPT of the long-named option found.
 | 
			
		||||
   It is only valid when a long-named option has been found by the most
 | 
			
		||||
   recent call.
 | 
			
		||||
 | 
			
		||||
   If LONG_ONLY is nonzero, '-' as well as '--' can introduce
 | 
			
		||||
   long-named options.  */
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
_getopt_internal (argc, argv, optstring, longopts, longind, long_only)
 | 
			
		||||
     int argc;
 | 
			
		||||
     char *const *argv;
 | 
			
		||||
     const char *optstring;
 | 
			
		||||
     const struct option *longopts;
 | 
			
		||||
     int *longind;
 | 
			
		||||
     int long_only;
 | 
			
		||||
{
 | 
			
		||||
  int option_index;
 | 
			
		||||
 | 
			
		||||
  optarg = 0;
 | 
			
		||||
 | 
			
		||||
  /* Initialize the internal data when the first call is made.
 | 
			
		||||
     Start processing options with ARGV-element 1 (since ARGV-element 0
 | 
			
		||||
     is the program name); the sequence of previously skipped
 | 
			
		||||
     non-option ARGV-elements is empty.  */
 | 
			
		||||
 | 
			
		||||
  if (optind == 0)
 | 
			
		||||
    {
 | 
			
		||||
      first_nonopt = last_nonopt = optind = 1;
 | 
			
		||||
 | 
			
		||||
      nextchar = NULL;
 | 
			
		||||
 | 
			
		||||
      /* Determine how to handle the ordering of options and nonoptions.  */
 | 
			
		||||
 | 
			
		||||
      if (optstring[0] == '-')
 | 
			
		||||
	{
 | 
			
		||||
	  ordering = RETURN_IN_ORDER;
 | 
			
		||||
	  ++optstring;
 | 
			
		||||
	}
 | 
			
		||||
      else if (optstring[0] == '+')
 | 
			
		||||
	{
 | 
			
		||||
	  ordering = REQUIRE_ORDER;
 | 
			
		||||
	  ++optstring;
 | 
			
		||||
	}
 | 
			
		||||
      else if (getenv ("POSIXLY_CORRECT") != NULL)
 | 
			
		||||
	ordering = REQUIRE_ORDER;
 | 
			
		||||
      else
 | 
			
		||||
	ordering = PERMUTE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  if (nextchar == NULL || *nextchar == '\0')
 | 
			
		||||
    {
 | 
			
		||||
      if (ordering == PERMUTE)
 | 
			
		||||
	{
 | 
			
		||||
	  /* If we have just processed some options following some non-options,
 | 
			
		||||
	     exchange them so that the options come first.  */
 | 
			
		||||
 | 
			
		||||
	  if (first_nonopt != last_nonopt && last_nonopt != optind)
 | 
			
		||||
	    exchange ((char **) argv);
 | 
			
		||||
	  else if (last_nonopt != optind)
 | 
			
		||||
	    first_nonopt = optind;
 | 
			
		||||
 | 
			
		||||
	  /* Now skip any additional non-options
 | 
			
		||||
	     and extend the range of non-options previously skipped.  */
 | 
			
		||||
 | 
			
		||||
	  while (optind < argc
 | 
			
		||||
		 && (argv[optind][0] != '-' || argv[optind][1] == '\0')
 | 
			
		||||
#ifdef GETOPT_COMPAT
 | 
			
		||||
		 && (longopts == NULL
 | 
			
		||||
		     || argv[optind][0] != '+' || argv[optind][1] == '\0')
 | 
			
		||||
#endif				/* GETOPT_COMPAT */
 | 
			
		||||
		 )
 | 
			
		||||
	    optind++;
 | 
			
		||||
	  last_nonopt = optind;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
      /* Special ARGV-element `--' means premature end of options.
 | 
			
		||||
	 Skip it like a null option,
 | 
			
		||||
	 then exchange with previous non-options as if it were an option,
 | 
			
		||||
	 then skip everything else like a non-option.  */
 | 
			
		||||
 | 
			
		||||
      if (optind != argc && !strcmp (argv[optind], "--"))
 | 
			
		||||
	{
 | 
			
		||||
	  optind++;
 | 
			
		||||
 | 
			
		||||
	  if (first_nonopt != last_nonopt && last_nonopt != optind)
 | 
			
		||||
	    exchange ((char **) argv);
 | 
			
		||||
	  else if (first_nonopt == last_nonopt)
 | 
			
		||||
	    first_nonopt = optind;
 | 
			
		||||
	  last_nonopt = argc;
 | 
			
		||||
 | 
			
		||||
	  optind = argc;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
      /* If we have done all the ARGV-elements, stop the scan
 | 
			
		||||
	 and back over any non-options that we skipped and permuted.  */
 | 
			
		||||
 | 
			
		||||
      if (optind == argc)
 | 
			
		||||
	{
 | 
			
		||||
	  /* Set the next-arg-index to point at the non-options
 | 
			
		||||
	     that we previously skipped, so the caller will digest them.  */
 | 
			
		||||
	  if (first_nonopt != last_nonopt)
 | 
			
		||||
	    optind = first_nonopt;
 | 
			
		||||
	  return EOF;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
      /* If we have come to a non-option and did not permute it,
 | 
			
		||||
	 either stop the scan or describe it to the caller and pass it by.  */
 | 
			
		||||
 | 
			
		||||
      if ((argv[optind][0] != '-' || argv[optind][1] == '\0')
 | 
			
		||||
#ifdef GETOPT_COMPAT
 | 
			
		||||
	  && (longopts == NULL
 | 
			
		||||
	      || argv[optind][0] != '+' || argv[optind][1] == '\0')
 | 
			
		||||
#endif				/* GETOPT_COMPAT */
 | 
			
		||||
	  )
 | 
			
		||||
	{
 | 
			
		||||
	  if (ordering == REQUIRE_ORDER)
 | 
			
		||||
	    return EOF;
 | 
			
		||||
	  optarg = argv[optind++];
 | 
			
		||||
	  return 1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
      /* We have found another option-ARGV-element.
 | 
			
		||||
	 Start decoding its characters.  */
 | 
			
		||||
 | 
			
		||||
      nextchar = (argv[optind] + 1
 | 
			
		||||
		  + (longopts != NULL && argv[optind][1] == '-'));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  if (longopts != NULL
 | 
			
		||||
      && ((argv[optind][0] == '-'
 | 
			
		||||
	   && (argv[optind][1] == '-' || long_only))
 | 
			
		||||
#ifdef GETOPT_COMPAT
 | 
			
		||||
	  || argv[optind][0] == '+'
 | 
			
		||||
#endif				/* GETOPT_COMPAT */
 | 
			
		||||
	  ))
 | 
			
		||||
    {
 | 
			
		||||
      const struct option *p;
 | 
			
		||||
      char *s = nextchar;
 | 
			
		||||
      int exact = 0;
 | 
			
		||||
      int ambig = 0;
 | 
			
		||||
      const struct option *pfound = NULL;
 | 
			
		||||
      int indfound;
 | 
			
		||||
 | 
			
		||||
      while (*s && *s != '=')
 | 
			
		||||
	s++;
 | 
			
		||||
 | 
			
		||||
      /* Test all options for either exact match or abbreviated matches.  */
 | 
			
		||||
      for (p = longopts, option_index = 0; p->name;
 | 
			
		||||
	   p++, option_index++)
 | 
			
		||||
	if (!strncmp (p->name, nextchar, s - nextchar))
 | 
			
		||||
	  {
 | 
			
		||||
	    if (s - nextchar == strlen (p->name))
 | 
			
		||||
	      {
 | 
			
		||||
		/* Exact match found.  */
 | 
			
		||||
		pfound = p;
 | 
			
		||||
		indfound = option_index;
 | 
			
		||||
		exact = 1;
 | 
			
		||||
		break;
 | 
			
		||||
	      }
 | 
			
		||||
	    else if (pfound == NULL)
 | 
			
		||||
	      {
 | 
			
		||||
		/* First nonexact match found.  */
 | 
			
		||||
		pfound = p;
 | 
			
		||||
		indfound = option_index;
 | 
			
		||||
	      }
 | 
			
		||||
	    else
 | 
			
		||||
	      /* Second nonexact match found.  */
 | 
			
		||||
	      ambig = 1;
 | 
			
		||||
	  }
 | 
			
		||||
 | 
			
		||||
      if (ambig && !exact)
 | 
			
		||||
	{
 | 
			
		||||
	  if (opterr)
 | 
			
		||||
	    fprintf (stderr, "%s: option `%s' is ambiguous\n",
 | 
			
		||||
		     argv[0], argv[optind]);
 | 
			
		||||
	  nextchar += strlen (nextchar);
 | 
			
		||||
	  optind++;
 | 
			
		||||
	  return '?';
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
      if (pfound != NULL)
 | 
			
		||||
	{
 | 
			
		||||
	  option_index = indfound;
 | 
			
		||||
	  optind++;
 | 
			
		||||
	  if (*s)
 | 
			
		||||
	    {
 | 
			
		||||
	      /* Don't test has_arg with >, because some C compilers don't
 | 
			
		||||
		 allow it to be used on enums.  */
 | 
			
		||||
	      if (pfound->has_arg)
 | 
			
		||||
		optarg = s + 1;
 | 
			
		||||
	      else
 | 
			
		||||
		{
 | 
			
		||||
		  if (opterr)
 | 
			
		||||
		    {
 | 
			
		||||
		      if (argv[optind - 1][1] == '-')
 | 
			
		||||
			/* --option */
 | 
			
		||||
			fprintf (stderr,
 | 
			
		||||
				 "%s: option `--%s' doesn't allow an argument\n",
 | 
			
		||||
				 argv[0], pfound->name);
 | 
			
		||||
		      else
 | 
			
		||||
			/* +option or -option */
 | 
			
		||||
			fprintf (stderr,
 | 
			
		||||
			     "%s: option `%c%s' doesn't allow an argument\n",
 | 
			
		||||
			     argv[0], argv[optind - 1][0], pfound->name);
 | 
			
		||||
		    }
 | 
			
		||||
		  nextchar += strlen (nextchar);
 | 
			
		||||
		  return '?';
 | 
			
		||||
		}
 | 
			
		||||
	    }
 | 
			
		||||
	  else if (pfound->has_arg == 1)
 | 
			
		||||
	    {
 | 
			
		||||
	      if (optind < argc)
 | 
			
		||||
		optarg = argv[optind++];
 | 
			
		||||
	      else
 | 
			
		||||
		{
 | 
			
		||||
		  if (opterr)
 | 
			
		||||
		    fprintf (stderr, "%s: option `%s' requires an argument\n",
 | 
			
		||||
			     argv[0], argv[optind - 1]);
 | 
			
		||||
		  nextchar += strlen (nextchar);
 | 
			
		||||
		  return optstring[0] == ':' ? ':' : '?';
 | 
			
		||||
		}
 | 
			
		||||
	    }
 | 
			
		||||
	  nextchar += strlen (nextchar);
 | 
			
		||||
	  if (longind != NULL)
 | 
			
		||||
	    *longind = option_index;
 | 
			
		||||
	  if (pfound->flag)
 | 
			
		||||
	    {
 | 
			
		||||
	      *(pfound->flag) = pfound->val;
 | 
			
		||||
	      return 0;
 | 
			
		||||
	    }
 | 
			
		||||
	  return pfound->val;
 | 
			
		||||
	}
 | 
			
		||||
      /* Can't find it as a long option.  If this is not getopt_long_only,
 | 
			
		||||
	 or the option starts with '--' or is not a valid short
 | 
			
		||||
	 option, then it's an error.
 | 
			
		||||
	 Otherwise interpret it as a short option.  */
 | 
			
		||||
      if (!long_only || argv[optind][1] == '-'
 | 
			
		||||
#ifdef GETOPT_COMPAT
 | 
			
		||||
	  || argv[optind][0] == '+'
 | 
			
		||||
#endif				/* GETOPT_COMPAT */
 | 
			
		||||
	  || my_index (optstring, *nextchar) == NULL)
 | 
			
		||||
	{
 | 
			
		||||
	  if (opterr)
 | 
			
		||||
	    {
 | 
			
		||||
	      if (argv[optind][1] == '-')
 | 
			
		||||
		/* --option */
 | 
			
		||||
		fprintf (stderr, "%s: unrecognized option `--%s'\n",
 | 
			
		||||
			 argv[0], nextchar);
 | 
			
		||||
	      else
 | 
			
		||||
		/* +option or -option */
 | 
			
		||||
		fprintf (stderr, "%s: unrecognized option `%c%s'\n",
 | 
			
		||||
			 argv[0], argv[optind][0], nextchar);
 | 
			
		||||
	    }
 | 
			
		||||
	  nextchar = (char *) "";
 | 
			
		||||
	  optind++;
 | 
			
		||||
	  return '?';
 | 
			
		||||
	}
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  /* Look at and handle the next option-character.  */
 | 
			
		||||
 | 
			
		||||
  {
 | 
			
		||||
    char c = *nextchar++;
 | 
			
		||||
    char *temp = my_index (optstring, c);
 | 
			
		||||
 | 
			
		||||
    /* Increment `optind' when we start to process its last character.  */
 | 
			
		||||
    if (*nextchar == '\0')
 | 
			
		||||
      ++optind;
 | 
			
		||||
 | 
			
		||||
    if (temp == NULL || c == ':')
 | 
			
		||||
      {
 | 
			
		||||
	if (opterr)
 | 
			
		||||
	  {
 | 
			
		||||
#if 0
 | 
			
		||||
	    if (c < 040 || c >= 0177)
 | 
			
		||||
	      fprintf (stderr, "%s: unrecognized option, character code 0%o\n",
 | 
			
		||||
		       argv[0], c);
 | 
			
		||||
	    else
 | 
			
		||||
	      fprintf (stderr, "%s: unrecognized option `-%c'\n", argv[0], c);
 | 
			
		||||
#else
 | 
			
		||||
	    /* 1003.2 specifies the format of this message.  */
 | 
			
		||||
	    fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c);
 | 
			
		||||
#endif
 | 
			
		||||
	  }
 | 
			
		||||
	optopt = c;
 | 
			
		||||
	return '?';
 | 
			
		||||
      }
 | 
			
		||||
    if (temp[1] == ':')
 | 
			
		||||
      {
 | 
			
		||||
	if (temp[2] == ':')
 | 
			
		||||
	  {
 | 
			
		||||
	    /* This is an option that accepts an argument optionally.  */
 | 
			
		||||
	    if (*nextchar != '\0')
 | 
			
		||||
	      {
 | 
			
		||||
		optarg = nextchar;
 | 
			
		||||
		optind++;
 | 
			
		||||
	      }
 | 
			
		||||
	    else
 | 
			
		||||
	      optarg = 0;
 | 
			
		||||
	    nextchar = NULL;
 | 
			
		||||
	  }
 | 
			
		||||
	else
 | 
			
		||||
	  {
 | 
			
		||||
	    /* This is an option that requires an argument.  */
 | 
			
		||||
	    if (*nextchar != '\0')
 | 
			
		||||
	      {
 | 
			
		||||
		optarg = nextchar;
 | 
			
		||||
		/* If we end this ARGV-element by taking the rest as an arg,
 | 
			
		||||
		   we must advance to the next element now.  */
 | 
			
		||||
		optind++;
 | 
			
		||||
	      }
 | 
			
		||||
	    else if (optind == argc)
 | 
			
		||||
	      {
 | 
			
		||||
		if (opterr)
 | 
			
		||||
		  {
 | 
			
		||||
#if 0
 | 
			
		||||
		    fprintf (stderr, "%s: option `-%c' requires an argument\n",
 | 
			
		||||
			     argv[0], c);
 | 
			
		||||
#else
 | 
			
		||||
		    /* 1003.2 specifies the format of this message.  */
 | 
			
		||||
		    fprintf (stderr, "%s: option requires an argument -- %c\n",
 | 
			
		||||
			     argv[0], c);
 | 
			
		||||
#endif
 | 
			
		||||
		  }
 | 
			
		||||
		optopt = c;
 | 
			
		||||
		if (optstring[0] == ':')
 | 
			
		||||
		  c = ':';
 | 
			
		||||
		else
 | 
			
		||||
		  c = '?';
 | 
			
		||||
	      }
 | 
			
		||||
	    else
 | 
			
		||||
	      /* We already incremented `optind' once;
 | 
			
		||||
		 increment it again when taking next ARGV-elt as argument.  */
 | 
			
		||||
	      optarg = argv[optind++];
 | 
			
		||||
	    nextchar = NULL;
 | 
			
		||||
	  }
 | 
			
		||||
      }
 | 
			
		||||
    return c;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
getopt (argc, argv, optstring)
 | 
			
		||||
     int argc;
 | 
			
		||||
     char *const *argv;
 | 
			
		||||
     const char *optstring;
 | 
			
		||||
{
 | 
			
		||||
  return _getopt_internal (argc, argv, optstring,
 | 
			
		||||
			   (const struct option *) 0,
 | 
			
		||||
			   (int *) 0,
 | 
			
		||||
			   0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif	/* _LIBC or not __GNU_LIBRARY__.  */
 | 
			
		||||
 | 
			
		||||
#ifdef TEST
 | 
			
		||||
 | 
			
		||||
/* Compile with -DTEST to make an executable for use in testing
 | 
			
		||||
   the above definition of `getopt'.  */
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
main (argc, argv)
 | 
			
		||||
     int argc;
 | 
			
		||||
     char **argv;
 | 
			
		||||
{
 | 
			
		||||
  int c;
 | 
			
		||||
  int digit_optind = 0;
 | 
			
		||||
 | 
			
		||||
  while (1)
 | 
			
		||||
    {
 | 
			
		||||
      int this_option_optind = optind ? optind : 1;
 | 
			
		||||
 | 
			
		||||
      c = getopt (argc, argv, "abc:d:0123456789");
 | 
			
		||||
      if (c == EOF)
 | 
			
		||||
	break;
 | 
			
		||||
 | 
			
		||||
      switch (c)
 | 
			
		||||
	{
 | 
			
		||||
	case '0':
 | 
			
		||||
	case '1':
 | 
			
		||||
	case '2':
 | 
			
		||||
	case '3':
 | 
			
		||||
	case '4':
 | 
			
		||||
	case '5':
 | 
			
		||||
	case '6':
 | 
			
		||||
	case '7':
 | 
			
		||||
	case '8':
 | 
			
		||||
	case '9':
 | 
			
		||||
	  if (digit_optind != 0 && digit_optind != this_option_optind)
 | 
			
		||||
	    printf ("digits occur in two different argv-elements.\n");
 | 
			
		||||
	  digit_optind = this_option_optind;
 | 
			
		||||
	  printf ("option %c\n", c);
 | 
			
		||||
	  break;
 | 
			
		||||
 | 
			
		||||
	case 'a':
 | 
			
		||||
	  printf ("option a\n");
 | 
			
		||||
	  break;
 | 
			
		||||
 | 
			
		||||
	case 'b':
 | 
			
		||||
	  printf ("option b\n");
 | 
			
		||||
	  break;
 | 
			
		||||
 | 
			
		||||
	case 'c':
 | 
			
		||||
	  printf ("option c with value `%s'\n", optarg);
 | 
			
		||||
	  break;
 | 
			
		||||
 | 
			
		||||
	case '?':
 | 
			
		||||
	  break;
 | 
			
		||||
 | 
			
		||||
	default:
 | 
			
		||||
	  printf ("?? getopt returned character code 0%o ??\n", c);
 | 
			
		||||
	}
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  if (optind < argc)
 | 
			
		||||
    {
 | 
			
		||||
      printf ("non-option ARGV-elements: ");
 | 
			
		||||
      while (optind < argc)
 | 
			
		||||
	printf ("%s ", argv[optind++]);
 | 
			
		||||
      printf ("\n");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  exit (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif /* TEST */
 | 
			
		||||
							
								
								
									
										129
									
								
								src/getopt.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										129
									
								
								src/getopt.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,129 @@
 | 
			
		||||
/* Declarations for getopt.
 | 
			
		||||
   Copyright (C) 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
 | 
			
		||||
 | 
			
		||||
   This program is free software; you can redistribute it and/or modify it
 | 
			
		||||
   under the terms of the GNU General Public License as published by the
 | 
			
		||||
   Free Software Foundation; either version 2, or (at your option) any
 | 
			
		||||
   later version.
 | 
			
		||||
 | 
			
		||||
   This program is distributed in the hope that it will be useful,
 | 
			
		||||
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
   GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
   You should have received a copy of the GNU General Public License
 | 
			
		||||
   along with this program; if not, write to the Free Software
 | 
			
		||||
   Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 | 
			
		||||
 | 
			
		||||
#ifndef _GETOPT_H
 | 
			
		||||
#define _GETOPT_H 1
 | 
			
		||||
 | 
			
		||||
#ifdef	__cplusplus
 | 
			
		||||
extern "C" {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/* For communication from `getopt' to the caller.
 | 
			
		||||
   When `getopt' finds an option that takes an argument,
 | 
			
		||||
   the argument value is returned here.
 | 
			
		||||
   Also, when `ordering' is RETURN_IN_ORDER,
 | 
			
		||||
   each non-option ARGV-element is returned here.  */
 | 
			
		||||
 | 
			
		||||
extern char *optarg;
 | 
			
		||||
 | 
			
		||||
/* Index in ARGV of the next element to be scanned.
 | 
			
		||||
   This is used for communication to and from the caller
 | 
			
		||||
   and for communication between successive calls to `getopt'.
 | 
			
		||||
 | 
			
		||||
   On entry to `getopt', zero means this is the first call; initialize.
 | 
			
		||||
 | 
			
		||||
   When `getopt' returns EOF, this is the index of the first of the
 | 
			
		||||
   non-option elements that the caller should itself scan.
 | 
			
		||||
 | 
			
		||||
   Otherwise, `optind' communicates from one call to the next
 | 
			
		||||
   how much of ARGV has been scanned so far.  */
 | 
			
		||||
 | 
			
		||||
extern int optind;
 | 
			
		||||
 | 
			
		||||
/* Callers store zero here to inhibit the error message `getopt' prints
 | 
			
		||||
   for unrecognized options.  */
 | 
			
		||||
 | 
			
		||||
extern int opterr;
 | 
			
		||||
 | 
			
		||||
/* Set to an option character which was unrecognized.  */
 | 
			
		||||
 | 
			
		||||
extern int optopt;
 | 
			
		||||
 | 
			
		||||
/* Describe the long-named options requested by the application.
 | 
			
		||||
   The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
 | 
			
		||||
   of `struct option' terminated by an element containing a name which is
 | 
			
		||||
   zero.
 | 
			
		||||
 | 
			
		||||
   The field `has_arg' is:
 | 
			
		||||
   no_argument		(or 0) if the option does not take an argument,
 | 
			
		||||
   required_argument	(or 1) if the option requires an argument,
 | 
			
		||||
   optional_argument 	(or 2) if the option takes an optional argument.
 | 
			
		||||
 | 
			
		||||
   If the field `flag' is not NULL, it points to a variable that is set
 | 
			
		||||
   to the value given in the field `val' when the option is found, but
 | 
			
		||||
   left unchanged if the option is not found.
 | 
			
		||||
 | 
			
		||||
   To have a long-named option do something other than set an `int' to
 | 
			
		||||
   a compiled-in constant, such as set a value from `optarg', set the
 | 
			
		||||
   option's `flag' field to zero and its `val' field to a nonzero
 | 
			
		||||
   value (the equivalent single-letter option character, if there is
 | 
			
		||||
   one).  For long options that have a zero `flag' field, `getopt'
 | 
			
		||||
   returns the contents of the `val' field.  */
 | 
			
		||||
 | 
			
		||||
struct option
 | 
			
		||||
{
 | 
			
		||||
#if	__STDC__
 | 
			
		||||
  const char *name;
 | 
			
		||||
#else
 | 
			
		||||
  char *name;
 | 
			
		||||
#endif
 | 
			
		||||
  /* has_arg can't be an enum because some compilers complain about
 | 
			
		||||
     type mismatches in all the code that assumes it is an int.  */
 | 
			
		||||
  int has_arg;
 | 
			
		||||
  int *flag;
 | 
			
		||||
  int val;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* Names for the values of the `has_arg' field of `struct option'.  */
 | 
			
		||||
 | 
			
		||||
#define	no_argument		0
 | 
			
		||||
#define required_argument	1
 | 
			
		||||
#define optional_argument	2
 | 
			
		||||
 | 
			
		||||
#if __STDC__
 | 
			
		||||
#if defined(__GNU_LIBRARY__)
 | 
			
		||||
/* Many other libraries have conflicting prototypes for getopt, with
 | 
			
		||||
   differences in the consts, in stdlib.h.  To avoid compilation
 | 
			
		||||
   errors, only prototype getopt for the GNU C library.  */
 | 
			
		||||
extern int getopt (int argc, char *const *argv, const char *shortopts);
 | 
			
		||||
#else /* not __GNU_LIBRARY__ */
 | 
			
		||||
extern int getopt ();
 | 
			
		||||
#endif /* not __GNU_LIBRARY__ */
 | 
			
		||||
extern int getopt_long (int argc, char *const *argv, const char *shortopts,
 | 
			
		||||
		        const struct option *longopts, int *longind);
 | 
			
		||||
extern int getopt_long_only (int argc, char *const *argv,
 | 
			
		||||
			     const char *shortopts,
 | 
			
		||||
		             const struct option *longopts, int *longind);
 | 
			
		||||
 | 
			
		||||
/* Internal only.  Users should not call this directly.  */
 | 
			
		||||
extern int _getopt_internal (int argc, char *const *argv,
 | 
			
		||||
			     const char *shortopts,
 | 
			
		||||
		             const struct option *longopts, int *longind,
 | 
			
		||||
			     int long_only);
 | 
			
		||||
#else /* not __STDC__ */
 | 
			
		||||
extern int getopt ();
 | 
			
		||||
extern int getopt_long ();
 | 
			
		||||
extern int getopt_long_only ();
 | 
			
		||||
 | 
			
		||||
extern int _getopt_internal ();
 | 
			
		||||
#endif /* not __STDC__ */
 | 
			
		||||
 | 
			
		||||
#ifdef	__cplusplus
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif /* _GETOPT_H */
 | 
			
		||||
							
								
								
									
										176
									
								
								src/getopt1.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										176
									
								
								src/getopt1.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,176 @@
 | 
			
		||||
/* getopt_long and getopt_long_only entry points for GNU getopt.
 | 
			
		||||
   Copyright (C) 1987, 88, 89, 90, 91, 92, 1993
 | 
			
		||||
	Free Software Foundation, Inc.
 | 
			
		||||
 | 
			
		||||
   This program is free software; you can redistribute it and/or modify it
 | 
			
		||||
   under the terms of the GNU General Public License as published by the
 | 
			
		||||
   Free Software Foundation; either version 2, or (at your option) any
 | 
			
		||||
   later version.
 | 
			
		||||
 | 
			
		||||
   This program is distributed in the hope that it will be useful,
 | 
			
		||||
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
   GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
   You should have received a copy of the GNU General Public License
 | 
			
		||||
   along with this program; if not, write to the Free Software
 | 
			
		||||
   Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 | 
			
		||||
 | 
			
		||||
#ifdef HAVE_CONFIG_H
 | 
			
		||||
#include "config.h"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include "getopt.h"
 | 
			
		||||
 | 
			
		||||
#if !__STDC__ && !defined(const) && IN_GCC
 | 
			
		||||
#define const
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
/* Comment out all this code if we are using the GNU C Library, and are not
 | 
			
		||||
   actually compiling the library itself.  This code is part of the GNU C
 | 
			
		||||
   Library, but also included in many other GNU distributions.  Compiling
 | 
			
		||||
   and linking in this code is a waste when using the GNU C library
 | 
			
		||||
   (especially if it is a shared library).  Rather than having every GNU
 | 
			
		||||
   program understand `configure --with-gnu-libc' and omit the object files,
 | 
			
		||||
   it is simpler to just do this in the source for each such file.  */
 | 
			
		||||
 | 
			
		||||
#if defined (_LIBC) || !defined (__GNU_LIBRARY__)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* This needs to come after some library #include
 | 
			
		||||
   to get __GNU_LIBRARY__ defined.  */
 | 
			
		||||
#ifdef __GNU_LIBRARY__
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#else
 | 
			
		||||
char *getenv ();
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifndef	NULL
 | 
			
		||||
#define NULL 0
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
getopt_long (argc, argv, options, long_options, opt_index)
 | 
			
		||||
     int argc;
 | 
			
		||||
     char *const *argv;
 | 
			
		||||
     const char *options;
 | 
			
		||||
     const struct option *long_options;
 | 
			
		||||
     int *opt_index;
 | 
			
		||||
{
 | 
			
		||||
  return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Like getopt_long, but '-' as well as '--' can indicate a long option.
 | 
			
		||||
   If an option that starts with '-' (not '--') doesn't match a long option,
 | 
			
		||||
   but does match a short option, it is parsed as a short option
 | 
			
		||||
   instead.  */
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
getopt_long_only (argc, argv, options, long_options, opt_index)
 | 
			
		||||
     int argc;
 | 
			
		||||
     char *const *argv;
 | 
			
		||||
     const char *options;
 | 
			
		||||
     const struct option *long_options;
 | 
			
		||||
     int *opt_index;
 | 
			
		||||
{
 | 
			
		||||
  return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif	/* _LIBC or not __GNU_LIBRARY__.  */
 | 
			
		||||
 | 
			
		||||
#ifdef TEST
 | 
			
		||||
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
main (argc, argv)
 | 
			
		||||
     int argc;
 | 
			
		||||
     char **argv;
 | 
			
		||||
{
 | 
			
		||||
  int c;
 | 
			
		||||
  int digit_optind = 0;
 | 
			
		||||
 | 
			
		||||
  while (1)
 | 
			
		||||
    {
 | 
			
		||||
      int this_option_optind = optind ? optind : 1;
 | 
			
		||||
      int option_index = 0;
 | 
			
		||||
      static struct option long_options[] =
 | 
			
		||||
      {
 | 
			
		||||
	{"add", 1, 0, 0},
 | 
			
		||||
	{"append", 0, 0, 0},
 | 
			
		||||
	{"delete", 1, 0, 0},
 | 
			
		||||
	{"verbose", 0, 0, 0},
 | 
			
		||||
	{"create", 0, 0, 0},
 | 
			
		||||
	{"file", 1, 0, 0},
 | 
			
		||||
	{0, 0, 0, 0}
 | 
			
		||||
      };
 | 
			
		||||
 | 
			
		||||
      c = getopt_long (argc, argv, "abc:d:0123456789",
 | 
			
		||||
		       long_options, &option_index);
 | 
			
		||||
      if (c == EOF)
 | 
			
		||||
	break;
 | 
			
		||||
 | 
			
		||||
      switch (c)
 | 
			
		||||
	{
 | 
			
		||||
	case 0:
 | 
			
		||||
	  printf ("option %s", long_options[option_index].name);
 | 
			
		||||
	  if (optarg)
 | 
			
		||||
	    printf (" with arg %s", optarg);
 | 
			
		||||
	  printf ("\n");
 | 
			
		||||
	  break;
 | 
			
		||||
 | 
			
		||||
	case '0':
 | 
			
		||||
	case '1':
 | 
			
		||||
	case '2':
 | 
			
		||||
	case '3':
 | 
			
		||||
	case '4':
 | 
			
		||||
	case '5':
 | 
			
		||||
	case '6':
 | 
			
		||||
	case '7':
 | 
			
		||||
	case '8':
 | 
			
		||||
	case '9':
 | 
			
		||||
	  if (digit_optind != 0 && digit_optind != this_option_optind)
 | 
			
		||||
	    printf ("digits occur in two different argv-elements.\n");
 | 
			
		||||
	  digit_optind = this_option_optind;
 | 
			
		||||
	  printf ("option %c\n", c);
 | 
			
		||||
	  break;
 | 
			
		||||
 | 
			
		||||
	case 'a':
 | 
			
		||||
	  printf ("option a\n");
 | 
			
		||||
	  break;
 | 
			
		||||
 | 
			
		||||
	case 'b':
 | 
			
		||||
	  printf ("option b\n");
 | 
			
		||||
	  break;
 | 
			
		||||
 | 
			
		||||
	case 'c':
 | 
			
		||||
	  printf ("option c with value `%s'\n", optarg);
 | 
			
		||||
	  break;
 | 
			
		||||
 | 
			
		||||
	case 'd':
 | 
			
		||||
	  printf ("option d with value `%s'\n", optarg);
 | 
			
		||||
	  break;
 | 
			
		||||
 | 
			
		||||
	case '?':
 | 
			
		||||
	  break;
 | 
			
		||||
 | 
			
		||||
	default:
 | 
			
		||||
	  printf ("?? getopt returned character code 0%o ??\n", c);
 | 
			
		||||
	}
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  if (optind < argc)
 | 
			
		||||
    {
 | 
			
		||||
      printf ("non-option ARGV-elements: ");
 | 
			
		||||
      while (optind < argc)
 | 
			
		||||
	printf ("%s ", argv[optind++]);
 | 
			
		||||
      printf ("\n");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  exit (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif /* TEST */
 | 
			
		||||
							
								
								
									
										168
									
								
								src/info.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										168
									
								
								src/info.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,168 @@
 | 
			
		||||
/* -*- C -*- 
 | 
			
		||||
  mboxgrep - scan mailbox for messages matching a regular expression
 | 
			
		||||
  Copyright (C) 2000, 2001, 2002, 2003  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
 | 
			
		||||
  the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
  (at your option) any later version.
 | 
			
		||||
 | 
			
		||||
  Mboxgrep is distributed in the hope that it will be useful, but 
 | 
			
		||||
  WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
  GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
  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.c,v 1.19 2003/04/13 19:15:58 dspiljar Exp $ */
 | 
			
		||||
 | 
			
		||||
#include <config.h>
 | 
			
		||||
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
#include "mboxgrep.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
print_wrap (char *str, int len, int *n)
 | 
			
		||||
     /* {{{  */
 | 
			
		||||
{
 | 
			
		||||
  *n += len;
 | 
			
		||||
  fprintf (stdout, str);
 | 
			
		||||
  if (*n >= 50)
 | 
			
		||||
    {
 | 
			
		||||
      fprintf (stdout, "\n");
 | 
			
		||||
      *n = 0;
 | 
			
		||||
    }
 | 
			
		||||
  else fprintf (stdout, " ");
 | 
			
		||||
}
 | 
			
		||||
/* }}} */
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
version (void)
 | 
			
		||||
     /* {{{  */
 | 
			
		||||
{
 | 
			
		||||
  int n = 0;
 | 
			
		||||
 | 
			
		||||
  fprintf (stdout, "%s %s\n\n"
 | 
			
		||||
	   "Copyright (C) 2000, 2001, 2002, 2003  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);
 | 
			
		||||
#endif /* HAVE_DIRENT_H */
 | 
			
		||||
#ifdef HAVE_FCNTL
 | 
			
		||||
  print_wrap ("HAVE_FCNTL", 10, &n);
 | 
			
		||||
#endif /* HAVE_FCNTL */
 | 
			
		||||
#ifdef HAVE_FLOCK
 | 
			
		||||
  print_wrap ("HAVE_FLOCK", 10, &n);
 | 
			
		||||
#endif /* HAVE_FLOCK */
 | 
			
		||||
#ifdef HAVE_FTS_OPEN
 | 
			
		||||
  print_wrap ("HAVE_FTS_OPEN", 13, &n);
 | 
			
		||||
#else
 | 
			
		||||
# ifdef HAVE_FTW
 | 
			
		||||
  print_wrap ("HAVE_FTW", 8, &n);
 | 
			
		||||
# endif /* HAVE_FTW */
 | 
			
		||||
#endif /* HAVE_FTS_OPEN */
 | 
			
		||||
/*
 | 
			
		||||
  fprintf (stdout, "HAVE_LIBLOCKFILE ");
 | 
			
		||||
*/
 | 
			
		||||
#ifdef HAVE_LIBPCRE
 | 
			
		||||
  print_wrap ("HAVE_LIBPCRE", 12, &n);
 | 
			
		||||
#endif /* HAVE_LIBPCRE */
 | 
			
		||||
#ifdef HAVE_LIBZ
 | 
			
		||||
  print_wrap ("HAVE_LIBZ", 9, &n);
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
#ifdef HAVE_LIBBZ2
 | 
			
		||||
  print_wrap ("HAVE_LIBBZ2", 11, &n);
 | 
			
		||||
#endif /* HAVE_LIBBZ2 */
 | 
			
		||||
#ifdef HAVE_NDIR_H
 | 
			
		||||
  print_wrap ("HAVE_NDIR_H", 11, &n);
 | 
			
		||||
#endif /* HAVE_NDIR_H */
 | 
			
		||||
#ifdef HAVE_SYS_DIR_H
 | 
			
		||||
  print_wrap ("HAVE_SYS_DIR_H", 14, &n);
 | 
			
		||||
#endif /* HAVE_SYS_DIR_H */
 | 
			
		||||
#ifdef HAVE_SYS_NDIR_H
 | 
			
		||||
  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);
 | 
			
		||||
  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\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");
 | 
			
		||||
#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");
 | 
			
		||||
#ifdef HAVE_FCNTL
 | 
			
		||||
  fprintf(stdout, " `fcntl',");
 | 
			
		||||
#endif /* HAVE_FCNTL */
 | 
			
		||||
#ifdef HAVE_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', ");
 | 
			
		||||
#ifdef HAVE_LIBZ
 | 
			
		||||
  fprintf(stdout, "`zmbox', ");
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
  fprintf(stdout,
 | 
			
		||||
	  "`mh', `nnml',\n"
 | 
			
		||||
	  "\t\t\t\t`nnmh', or `maildir'.\n\n"
 | 
			
		||||
	  "Mail bug reports and flames to <%s>.\n", BUGREPORT_ADDR);
 | 
			
		||||
 | 
			
		||||
  exit(0);
 | 
			
		||||
}
 | 
			
		||||
/* }}} */
 | 
			
		||||
 | 
			
		||||
void 
 | 
			
		||||
usage (void)
 | 
			
		||||
     /* {{{  */
 | 
			
		||||
{
 | 
			
		||||
  printf ("Usage: %s [OPTION] PATTERN MAILBOX ...\n\n"
 | 
			
		||||
          "Try `%s --help' for more information.\n", APPNAME, APPNAME);
 | 
			
		||||
  exit (2);
 | 
			
		||||
}
 | 
			
		||||
/* }}} */
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										29
									
								
								src/info.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/info.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
/* -*- C -*- 
 | 
			
		||||
  mboxgrep - scan mailbox for messages matching a regular expression
 | 
			
		||||
  Copyright (C) 2000, 2001, 2002, 2003  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
 | 
			
		||||
  the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
  (at your option) any later version.
 | 
			
		||||
 | 
			
		||||
  Mboxgrep is distributed in the hope that it will be useful, but 
 | 
			
		||||
  WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
  GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
  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
 | 
			
		||||
 | 
			
		||||
void print_wrap (char *str, int len, int *n);
 | 
			
		||||
void version (void);
 | 
			
		||||
void help (void);
 | 
			
		||||
void usage (void);
 | 
			
		||||
 | 
			
		||||
#endif /* INFO_H */
 | 
			
		||||
							
								
								
									
										339
									
								
								src/maildir.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										339
									
								
								src/maildir.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,339 @@
 | 
			
		||||
/* -*- C -*- 
 | 
			
		||||
  mboxgrep - scan mailbox for messages matching a regular expression
 | 
			
		||||
  Copyright (C) 2000, 2001, 2002, 2003  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
 | 
			
		||||
  the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
  (at your option) any later version.
 | 
			
		||||
 | 
			
		||||
  Mboxgrep is distributed in the hope that it will be useful, but 
 | 
			
		||||
  WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
  GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
  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.21 2003/03/30 23:07:10 dspiljar Exp $ */
 | 
			
		||||
 | 
			
		||||
#include <config.h>
 | 
			
		||||
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <string.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 <stdlib.h>
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/stat.h>
 | 
			
		||||
 | 
			
		||||
#include "mboxgrep.h"
 | 
			
		||||
#include "misc.h"
 | 
			
		||||
#include "maildir.h"
 | 
			
		||||
#include "wrap.h"
 | 
			
		||||
 | 
			
		||||
#ifdef HAVE_LIBDMALLOC
 | 
			
		||||
#include <dmalloc.h>
 | 
			
		||||
#endif /* HAVE_LIBDMALLOC */
 | 
			
		||||
 | 
			
		||||
maildir_t *
 | 
			
		||||
maildir_open (const char *path)
 | 
			
		||||
{
 | 
			
		||||
  static maildir_t *mp;
 | 
			
		||||
  char *dirname;
 | 
			
		||||
  DIR *foo;
 | 
			
		||||
 | 
			
		||||
  foo = m_opendir (path);
 | 
			
		||||
  if (foo == NULL)
 | 
			
		||||
      return NULL;
 | 
			
		||||
 | 
			
		||||
  closedir (foo);
 | 
			
		||||
 | 
			
		||||
  if (-1 == maildir_check (path))
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	  fprintf (stderr, "%s: %s: Not a maildir folder\n", APPNAME, path);
 | 
			
		||||
      return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  dirname = (char *) xmalloc((sizeof (char) * (strlen (path) + 5)));
 | 
			
		||||
 | 
			
		||||
  mp = (maildir_t *) xmalloc(sizeof (maildir_t));
 | 
			
		||||
  sprintf (dirname, "%s/new", path);
 | 
			
		||||
  mp->new = m_opendir (dirname);
 | 
			
		||||
  if (mp->new == NULL)
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	{
 | 
			
		||||
	  fprintf (stderr, "%s: %s: ", APPNAME, dirname);
 | 
			
		||||
	  perror (NULL);
 | 
			
		||||
	}
 | 
			
		||||
      free (dirname);
 | 
			
		||||
      errno = 0;
 | 
			
		||||
      return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  sprintf (dirname, "%s/cur", path);
 | 
			
		||||
  mp->cur = m_opendir (dirname);
 | 
			
		||||
  if (mp->cur == NULL)
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	{
 | 
			
		||||
	  fprintf (stderr, "%s: %s: ", APPNAME, dirname);
 | 
			
		||||
	  perror (NULL);
 | 
			
		||||
	}
 | 
			
		||||
      free (dirname);
 | 
			
		||||
      errno = 0;
 | 
			
		||||
      return NULL;
 | 
			
		||||
    }  
 | 
			
		||||
 | 
			
		||||
  free (dirname);
 | 
			
		||||
  return mp;
 | 
			
		||||
} /* maildir_open */
 | 
			
		||||
 | 
			
		||||
message_t *
 | 
			
		||||
maildir_read_message (maildir_t *mdp)
 | 
			
		||||
{
 | 
			
		||||
  int isheaders = 1;
 | 
			
		||||
  int have_from = 0, have_to = 0, have_message_id = 0, have_return_path = 0,
 | 
			
		||||
    have_sender = 0, have_date = 0;
 | 
			
		||||
  static message_t *message;
 | 
			
		||||
  static struct dirent *d_content;
 | 
			
		||||
  char *filename, buffer[BUFSIZ];
 | 
			
		||||
  extern char *boxname;
 | 
			
		||||
  static FILE *fp;
 | 
			
		||||
  static int s;
 | 
			
		||||
 | 
			
		||||
  message = (message_t *) xmalloc (sizeof (message_t));
 | 
			
		||||
 | 
			
		||||
  message->headers = (char *) xmalloc (sizeof (char));
 | 
			
		||||
  message->headers[0] = '\0';
 | 
			
		||||
  message->hbytes = 0;
 | 
			
		||||
 | 
			
		||||
  message->body = (char *) xmalloc (sizeof (char));
 | 
			
		||||
  message->body[0] = '\0';
 | 
			
		||||
  message->bbytes = 0;
 | 
			
		||||
 | 
			
		||||
  message->from = NULL;
 | 
			
		||||
 | 
			
		||||
  for(;;)
 | 
			
		||||
    {
 | 
			
		||||
      if (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;
 | 
			
		||||
	    }
 | 
			
		||||
	}
 | 
			
		||||
      if (mdp->new == NULL && mdp->cur == NULL)
 | 
			
		||||
{
 | 
			
		||||
	free (message->headers);
 | 
			
		||||
	free (message->body);
 | 
			
		||||
	free (message);
 | 
			
		||||
	return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
      if (d_content->d_name[0] == '.')
 | 
			
		||||
	continue;
 | 
			
		||||
 | 
			
		||||
      filename = 
 | 
			
		||||
	(char *) xmalloc ((sizeof (char)*((strlen (d_content->d_name)) 
 | 
			
		||||
					  + (strlen (boxname)) + 6)));
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
      filename = 
 | 
			
		||||
	(char *) alloca((sizeof(char)*((strlen(d_content->d_name))
 | 
			
		||||
					 + (strlen(boxname)) + 2)));
 | 
			
		||||
*/
 | 
			
		||||
      if (mdp->new != NULL)
 | 
			
		||||
	sprintf (filename, "%s/new/%s", boxname, d_content->d_name);
 | 
			
		||||
      else
 | 
			
		||||
	sprintf (filename, "%s/cur/%s", boxname, d_content->d_name);
 | 
			
		||||
      message->filename = (char *) xstrdup (filename);
 | 
			
		||||
      free (filename);
 | 
			
		||||
 | 
			
		||||
      isheaders = 1;
 | 
			
		||||
      fp = m_fopen(message->filename, "r");
 | 
			
		||||
 | 
			
		||||
      if (fp == NULL)
 | 
			
		||||
	  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))
 | 
			
		||||
		{
 | 
			
		||||
		  have_return_path = 1;
 | 
			
		||||
		  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 */
 | 
			
		||||
/*       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;
 | 
			
		||||
	}
 | 
			
		||||
      
 | 
			
		||||
      fclose(fp);
 | 
			
		||||
      return message; 
 | 
			
		||||
    } /* for */
 | 
			
		||||
} /* maildir_read_message */
 | 
			
		||||
 | 
			
		||||
void 
 | 
			
		||||
maildir_write_message (message_t *m, const char *path)
 | 
			
		||||
{
 | 
			
		||||
  extern int maildir_count;
 | 
			
		||||
  char bla[BUFSIZ], *s1, *s2;
 | 
			
		||||
  int t;
 | 
			
		||||
  static FILE *f1;
 | 
			
		||||
 | 
			
		||||
  maildir_count++;
 | 
			
		||||
  t = (int) time (NULL);
 | 
			
		||||
 | 
			
		||||
  sprintf (bla, "%i.%i_%i.%s", t, config.pid, maildir_count, config.hostname);
 | 
			
		||||
  s1 = (char *) xmalloc ((strlen (path) + strlen (bla) + 6) * sizeof (char));
 | 
			
		||||
  sprintf(s1, "%s/tmp/%s", path, bla);
 | 
			
		||||
  s2 = (char *) xmalloc ((strlen (path) + strlen (bla) + 6) * sizeof (char));
 | 
			
		||||
  sprintf(s2, "%s/new/%s", path, bla);
 | 
			
		||||
 | 
			
		||||
  f1 = m_fopen (s1, "w");
 | 
			
		||||
  fprintf (f1, "%s\n%s", m->headers, m->body);
 | 
			
		||||
  fclose (f1);
 | 
			
		||||
  rename (s1, s2);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int 
 | 
			
		||||
maildir_check (const char *path)
 | 
			
		||||
{
 | 
			
		||||
  static struct stat fs;
 | 
			
		||||
  static int i;
 | 
			
		||||
  char *s;
 | 
			
		||||
 | 
			
		||||
  s = (char *) xmalloc ((strlen (path) + 5) * sizeof (char));
 | 
			
		||||
 | 
			
		||||
  sprintf (s, "%s/cur", path);
 | 
			
		||||
  i = stat (s, &fs);
 | 
			
		||||
  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;
 | 
			
		||||
 | 
			
		||||
  sprintf(s, "%s/tmp", path);
 | 
			
		||||
  i = stat (s, &fs);
 | 
			
		||||
  if (-1 == i) return -1;
 | 
			
		||||
  if (! S_ISDIR (fs.st_mode)) return -1;
 | 
			
		||||
 | 
			
		||||
  free (s);
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void 
 | 
			
		||||
maildir_create (const char *path)
 | 
			
		||||
{
 | 
			
		||||
  char *s;
 | 
			
		||||
  int i;
 | 
			
		||||
 | 
			
		||||
  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);
 | 
			
		||||
      i = mkdir (s, S_IRWXU);
 | 
			
		||||
      if (-1 == i)
 | 
			
		||||
	break;
 | 
			
		||||
      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;
 | 
			
		||||
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  if (errno != 0)
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	{
 | 
			
		||||
	  fprintf(stderr, "%s:%s: ", APPNAME, s);
 | 
			
		||||
	  perror (NULL);
 | 
			
		||||
	}
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										58
									
								
								src/maildir.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								src/maildir.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,58 @@
 | 
			
		||||
/* -*- C -*- 
 | 
			
		||||
  mboxgrep - scan mailbox for messages matching a regular expression
 | 
			
		||||
  Copyright (C) 2000, 2001, 2002, 2003  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
 | 
			
		||||
  the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
  (at your option) any later version.
 | 
			
		||||
 | 
			
		||||
  Mboxgrep is distributed in the hope that it will be useful, but 
 | 
			
		||||
  WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
  GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
  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.h,v 1.6 2003/03/30 23:07:10 dspiljar Exp $ */
 | 
			
		||||
 | 
			
		||||
#ifndef MAILDIR_H
 | 
			
		||||
#define MAILDIR_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 */
 | 
			
		||||
 | 
			
		||||
#include "mboxgrep.h"
 | 
			
		||||
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
  DIR *new;
 | 
			
		||||
  DIR *cur;
 | 
			
		||||
} maildir_t;
 | 
			
		||||
 | 
			
		||||
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);
 | 
			
		||||
 | 
			
		||||
#endif /* MAILDIR_H */
 | 
			
		||||
							
								
								
									
										375
									
								
								src/main.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										375
									
								
								src/main.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,375 @@
 | 
			
		||||
/* -*- C -*-
 | 
			
		||||
  mboxgrep - scan mailbox for messages matching a regular expression
 | 
			
		||||
  Copyright (C) 2000, 2001, 2002, 2003  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
 | 
			
		||||
  the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
  (at your option) any later version.
 | 
			
		||||
 | 
			
		||||
  Mboxgrep is distributed in the hope that it will be useful, but 
 | 
			
		||||
  WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
  GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
  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: main.c,v 1.32 2003/08/24 19:23:50 dspiljar Exp $ */
 | 
			
		||||
 | 
			
		||||
#include <config.h>
 | 
			
		||||
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/stat.h>
 | 
			
		||||
#include <regex.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#ifdef HAVE_LIBPCRE
 | 
			
		||||
#include <pcre.h>
 | 
			
		||||
#endif /* HAVE_LIBPCRE */
 | 
			
		||||
#ifdef HAVE_LIBZ
 | 
			
		||||
#include <zlib.h>
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
 | 
			
		||||
#include "getopt.h"
 | 
			
		||||
#include "mboxgrep.h"
 | 
			
		||||
#include "misc.h"
 | 
			
		||||
#include "info.h"
 | 
			
		||||
#include "mbox.h"
 | 
			
		||||
#include "mh.h"
 | 
			
		||||
#include "scan.h"
 | 
			
		||||
#include "wrap.h" /* xcalloc() et cetera */
 | 
			
		||||
 | 
			
		||||
#ifdef HAVE_LIBDMALLOC
 | 
			
		||||
#include <dmalloc.h>
 | 
			
		||||
#endif /* HAVE_LIBDMALLOC */
 | 
			
		||||
 | 
			
		||||
regex_t posix_pattern;
 | 
			
		||||
#ifdef HAVE_LIBPCRE
 | 
			
		||||
pcre *pcre_pattern;
 | 
			
		||||
pcre_extra *hints;
 | 
			
		||||
#endif /* HAVE_LIBPCRE */
 | 
			
		||||
char *boxname, *outboxname, *pipecmd, *tmpfilename;
 | 
			
		||||
int maildir_count = 0;
 | 
			
		||||
int count = 0;
 | 
			
		||||
void *tmpp;
 | 
			
		||||
checksum_t *cs;
 | 
			
		||||
 | 
			
		||||
int
 | 
			
		||||
main (int argc, char **argv)
 | 
			
		||||
     /* {{{  */
 | 
			
		||||
 | 
			
		||||
{
 | 
			
		||||
  int option_index = 0;
 | 
			
		||||
  int c;
 | 
			
		||||
#ifdef HAVE_LIBPCRE
 | 
			
		||||
  int errptr;
 | 
			
		||||
  const char *error;
 | 
			
		||||
#endif /* HAVE_LIBPCRE */
 | 
			
		||||
  int haveregex = 0, havemailbox = 0;
 | 
			
		||||
  static char *regex_s;
 | 
			
		||||
  int singlefile = 0;
 | 
			
		||||
 | 
			
		||||
  int errcode = 0;
 | 
			
		||||
  char errbuf[BUFSIZ];
 | 
			
		||||
 | 
			
		||||
  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},
 | 
			
		||||
      {"file-lock", 1, 0, 'l'},
 | 
			
		||||
      {"recursive", 0, 0, 'r'},
 | 
			
		||||
      {0, 0, 0, 0}
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
  /* }}} */
 | 
			
		||||
 | 
			
		||||
  config.perl = 0;
 | 
			
		||||
  config.extended = 1;
 | 
			
		||||
  config.invert = 0;
 | 
			
		||||
  config.headers = 0;
 | 
			
		||||
  config.body = 0;
 | 
			
		||||
  config.action = DISPLAY;
 | 
			
		||||
  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 */
 | 
			
		||||
 | 
			
		||||
  while (1)
 | 
			
		||||
    {
 | 
			
		||||
      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':
 | 
			
		||||
	  regex_s = xstrdup (optarg);
 | 
			
		||||
	  haveregex = 1;
 | 
			
		||||
	  break;
 | 
			
		||||
	case 'o':
 | 
			
		||||
	  outboxname = xstrdup (optarg);
 | 
			
		||||
	  config.action = WRITE;
 | 
			
		||||
	  break;
 | 
			
		||||
	case 'E':
 | 
			
		||||
	  config.extended = 1;
 | 
			
		||||
	  break;
 | 
			
		||||
	case 'G':
 | 
			
		||||
	  config.extended = 0;
 | 
			
		||||
	  break;
 | 
			
		||||
	case 'P':
 | 
			
		||||
#ifdef HAVE_LIBPCRE
 | 
			
		||||
	  config.extended = 0;
 | 
			
		||||
	  config.perl = 1;
 | 
			
		||||
#else
 | 
			
		||||
	  fprintf(stderr, 
 | 
			
		||||
		  "%s: Support for Perl regular expressions not "
 | 
			
		||||
		  "compiled in\n");
 | 
			
		||||
	  exit(2);
 | 
			
		||||
#endif /* HAVE_LIBPCRE */
 | 
			
		||||
	  break;
 | 
			
		||||
	case 'h':
 | 
			
		||||
	  help ();
 | 
			
		||||
	  break;
 | 
			
		||||
	case 'i':
 | 
			
		||||
	  config.ignorecase = 1;
 | 
			
		||||
	  break;
 | 
			
		||||
	case 'm':
 | 
			
		||||
	  config.format = folder_format (optarg);
 | 
			
		||||
	  break;
 | 
			
		||||
	case 'l':
 | 
			
		||||
	  config.lock = lock_method (optarg);
 | 
			
		||||
	  break;
 | 
			
		||||
	case 'p':
 | 
			
		||||
	  config.action = PIPE;
 | 
			
		||||
	  pipecmd = xstrdup (optarg);
 | 
			
		||||
	  break;
 | 
			
		||||
	case 'V':
 | 
			
		||||
	  version ();
 | 
			
		||||
	  break;
 | 
			
		||||
	case 'v':
 | 
			
		||||
	  config.invert = 1;
 | 
			
		||||
	  break;
 | 
			
		||||
	case 'H':
 | 
			
		||||
	  config.headers = 1;
 | 
			
		||||
	  break;
 | 
			
		||||
	case 'B':
 | 
			
		||||
	  config.body = 1;
 | 
			
		||||
	  break;
 | 
			
		||||
	case 's':
 | 
			
		||||
	  config.merr = 0;
 | 
			
		||||
	  break;
 | 
			
		||||
	case 201:
 | 
			
		||||
	  config.lock = 0;
 | 
			
		||||
	  break;
 | 
			
		||||
	case 'r':
 | 
			
		||||
	  config.recursive = 1;
 | 
			
		||||
	  break;
 | 
			
		||||
        case 200:
 | 
			
		||||
          config.dedup = 1;
 | 
			
		||||
          break;
 | 
			
		||||
        case 'n':
 | 
			
		||||
	  {
 | 
			
		||||
	    switch (optarg[0])
 | 
			
		||||
	      {
 | 
			
		||||
	        case 'd':
 | 
			
		||||
		  config.dedup = 1;
 | 
			
		||||
		  break;
 | 
			
		||||
		case 'l':
 | 
			
		||||
		  config.lock = 0;
 | 
			
		||||
		  break;
 | 
			
		||||
	        default:
 | 
			
		||||
		  fprintf(stderr, "%s: invalid option -- n%c\n", 
 | 
			
		||||
			  APPNAME, optarg[0]);
 | 
			
		||||
		  exit(2);
 | 
			
		||||
	      }
 | 
			
		||||
	  }
 | 
			
		||||
	} /* switch */
 | 
			
		||||
 | 
			
		||||
      /* }}} */
 | 
			
		||||
    } /* while */
 | 
			
		||||
 | 
			
		||||
  if ((config.body == 0) && (config.headers == 0))
 | 
			
		||||
    {
 | 
			
		||||
      config.body = 1;
 | 
			
		||||
      config.headers = 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  if (config.format == MAILDIR && config.action == WRITE)
 | 
			
		||||
    {
 | 
			
		||||
      gethostname (config.hostname, HOST_NAME_SIZE);
 | 
			
		||||
      config.pid = (int) getpid ();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  cs = (checksum_t *) xmalloc (sizeof (checksum_t));
 | 
			
		||||
  cs->md5 = (char **) xcalloc (1, sizeof (char **));
 | 
			
		||||
  cs->n = 0;
 | 
			
		||||
 | 
			
		||||
  if (optind < argc && ! haveregex)
 | 
			
		||||
    {
 | 
			
		||||
      regex_s = xstrdup (argv[optind]);
 | 
			
		||||
      haveregex = 1;
 | 
			
		||||
      ++optind;
 | 
			
		||||
    } /* if */
 | 
			
		||||
 | 
			
		||||
  if (haveregex) 
 | 
			
		||||
    {
 | 
			
		||||
#ifdef HAVE_LIBPCRE
 | 
			
		||||
      if (config.perl)
 | 
			
		||||
	/* {{{  */
 | 
			
		||||
 | 
			
		||||
	{
 | 
			
		||||
	  pcre_pattern = pcre_compile (regex_s, 
 | 
			
		||||
				       (config.ignorecase ? PCRE_CASELESS : 0),
 | 
			
		||||
				       &error, &errptr, NULL);
 | 
			
		||||
	  if (pcre_pattern == NULL)
 | 
			
		||||
	    {
 | 
			
		||||
	      if (config.merr)
 | 
			
		||||
		fprintf (stderr, "%s: %s: %s\n", APPNAME, regex_s, error);
 | 
			
		||||
	      exit(2);
 | 
			
		||||
	    }
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
      /* }}} */
 | 
			
		||||
      else
 | 
			
		||||
#endif /* HAVE_LIBPCRE */
 | 
			
		||||
	/* {{{  */
 | 
			
		||||
 | 
			
		||||
	{
 | 
			
		||||
	  int flag1 = 0, flag2 = 0;
 | 
			
		||||
	  
 | 
			
		||||
	  if (config.ignorecase)
 | 
			
		||||
	    flag1 = REG_ICASE;
 | 
			
		||||
	  if (config.extended)
 | 
			
		||||
	    flag2 = REG_EXTENDED;
 | 
			
		||||
	
 | 
			
		||||
	  errcode = regcomp (&posix_pattern, regex_s, 
 | 
			
		||||
			     (flag1 | flag2 | REG_NEWLINE ));
 | 
			
		||||
	  if (0 != errcode)
 | 
			
		||||
	    {
 | 
			
		||||
	      if (config.merr)
 | 
			
		||||
		{
 | 
			
		||||
		  regerror (errcode, &posix_pattern, errbuf, BUFSIZ);
 | 
			
		||||
		  fprintf (stderr, "%s: %s: %s\n", APPNAME, regex_s, errbuf);
 | 
			
		||||
		}
 | 
			
		||||
	      exit (2);
 | 
			
		||||
	    } /* if */
 | 
			
		||||
	} /* if */
 | 
			
		||||
 | 
			
		||||
      /* }}} */
 | 
			
		||||
    } /* if */
 | 
			
		||||
  else
 | 
			
		||||
    usage ();
 | 
			
		||||
 | 
			
		||||
  if (optind == (argc - 1))
 | 
			
		||||
    singlefile = 1;
 | 
			
		||||
 | 
			
		||||
  while (optind < argc)
 | 
			
		||||
    /* {{{  */
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
      if (config.action == DELETE) {
 | 
			
		||||
	  tmpp = tmpfile_open (argv[optind]);
 | 
			
		||||
 | 
			
		||||
	  /* If we're root, copy {owner, group, perms} of mailbox to the tmpfile
 | 
			
		||||
	   * so rename() will thus retain the original's ownership & permissions.
 | 
			
		||||
	   */
 | 
			
		||||
	  if (geteuid() == 0) {
 | 
			
		||||
	      struct stat s;
 | 
			
		||||
	      if (stat(argv[optind], &s) != -1) {
 | 
			
		||||
		  if (fchown(fileno(tmpp), s.st_uid, s.st_gid) == -1)
 | 
			
		||||
		      if (config.merr) perror(tmpfilename);
 | 
			
		||||
		  if (fchmod(fileno(tmpp), s.st_mode) == -1)
 | 
			
		||||
		      if (config.merr) perror(tmpfilename);
 | 
			
		||||
	      }
 | 
			
		||||
	      else if (config.merr) perror(argv[optind]);
 | 
			
		||||
	  }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      boxname = xstrdup (argv[optind]);
 | 
			
		||||
 | 
			
		||||
      if (config.recursive)
 | 
			
		||||
	recursive_scan (argv[optind]);
 | 
			
		||||
      else
 | 
			
		||||
	scan_mailbox (argv[optind]);
 | 
			
		||||
      havemailbox = 1;
 | 
			
		||||
      if (config.action == COUNT)
 | 
			
		||||
	{
 | 
			
		||||
	  if (singlefile)
 | 
			
		||||
	    fprintf (stdout, "%i\n", count);
 | 
			
		||||
	  else
 | 
			
		||||
	    {
 | 
			
		||||
	      if (0 == strcmp ("-", argv[optind]))
 | 
			
		||||
		fprintf (stdout, "(standard input):%i\n", count);
 | 
			
		||||
	      else
 | 
			
		||||
		fprintf (stdout, "%s:%i\n", argv[optind], count);
 | 
			
		||||
	    }
 | 
			
		||||
	}
 | 
			
		||||
      if (config.action == DELETE)
 | 
			
		||||
	{
 | 
			
		||||
#ifdef HAVE_LIBZ
 | 
			
		||||
	  if (config.format == ZMBOX)
 | 
			
		||||
	    gzclose (tmpp);
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
	  if (config.format == MBOX)
 | 
			
		||||
	    fclose (tmpp);
 | 
			
		||||
	  rename (tmpfilename, argv[optind]);
 | 
			
		||||
	}
 | 
			
		||||
      ++optind;
 | 
			
		||||
    } /* while */
 | 
			
		||||
 | 
			
		||||
  /* }}} */
 | 
			
		||||
 | 
			
		||||
  if (! havemailbox)
 | 
			
		||||
    /* {{{  */
 | 
			
		||||
 | 
			
		||||
    {
 | 
			
		||||
      config.format = MBOX;
 | 
			
		||||
      scan_mailbox ("-");
 | 
			
		||||
      if (config.action == COUNT)
 | 
			
		||||
	fprintf (stdout, "%i\n", count);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  /* }}} */
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
} /* main */
 | 
			
		||||
 | 
			
		||||
/* }}} */
 | 
			
		||||
							
								
								
									
										400
									
								
								src/mbox.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										400
									
								
								src/mbox.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,400 @@
 | 
			
		||||
/* -*- C -*- 
 | 
			
		||||
   mboxgrep - scan mailbox for messages matching a regular expression
 | 
			
		||||
   Copyright (C) 2000, 2001, 2002, 2003  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
 | 
			
		||||
   the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
   (at your option) any later version.
 | 
			
		||||
 | 
			
		||||
   Mboxgrep is distributed in the hope that it will be useful, but 
 | 
			
		||||
   WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
   GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
   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.26 2003/08/24 19:23:50 dspiljar Exp $ */
 | 
			
		||||
 | 
			
		||||
#include <config.h>
 | 
			
		||||
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/stat.h>
 | 
			
		||||
#include <fcntl.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
#ifdef HAVE_FLOCK
 | 
			
		||||
#include <sys/file.h>
 | 
			
		||||
#endif /* HAVE_FLOCK */
 | 
			
		||||
#ifdef HAVE_LIBZ
 | 
			
		||||
#include <zlib.h>
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
#ifdef HAVE_LIBBZ2
 | 
			
		||||
#include <bzlib.h>
 | 
			
		||||
#endif /* HAVE_LIBBZ2 */
 | 
			
		||||
 | 
			
		||||
#include "mboxgrep.h"
 | 
			
		||||
#include "mbox.h"
 | 
			
		||||
#include "misc.h"
 | 
			
		||||
#include "wrap.h"
 | 
			
		||||
 | 
			
		||||
#ifdef HAVE_LIBDMALLOC
 | 
			
		||||
#include <dmalloc.h>
 | 
			
		||||
#endif /* HAVE_LIBDMALLOC */
 | 
			
		||||
 | 
			
		||||
mbox_t *
 | 
			
		||||
mbox_open (const char *path, const char *mode)
 | 
			
		||||
     /* {{{  */
 | 
			
		||||
{
 | 
			
		||||
  mbox_t *mp;
 | 
			
		||||
  static int fd;
 | 
			
		||||
#ifndef HAVE_FLOCK
 | 
			
		||||
  struct flock lck;
 | 
			
		||||
#endif /* HAVE_FLOCK */
 | 
			
		||||
  char buffer[BUFSIZ];
 | 
			
		||||
 | 
			
		||||
  mp = (mbox_t *) xmalloc (sizeof (mbox_t));
 | 
			
		||||
  mp->postmark_cache = (char *) xmalloc (BUFSIZ * sizeof (char));
 | 
			
		||||
 | 
			
		||||
  if (0 == strcmp ("-", path))
 | 
			
		||||
    mp->fp = stdin;
 | 
			
		||||
  else
 | 
			
		||||
    {
 | 
			
		||||
      if (mode[0] == 'r')
 | 
			
		||||
	fd = m_open (path, O_RDONLY, 0);
 | 
			
		||||
      else if (mode[0] == 'w')
 | 
			
		||||
	fd = m_open (path, (O_WRONLY | O_CREAT | O_APPEND),
 | 
			
		||||
		     (S_IWUSR | S_IRUSR));
 | 
			
		||||
      else
 | 
			
		||||
	{
 | 
			
		||||
	  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.lock)
 | 
			
		||||
	{
 | 
			
		||||
#ifdef HAVE_FLOCK
 | 
			
		||||
	  int 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;
 | 
			
		||||
 | 
			
		||||
	  if (-1 == fcntl (fd, F_SETLK, &lck))
 | 
			
		||||
#endif /* HAVE_FLOCK */
 | 
			
		||||
	    {
 | 
			
		||||
	      if (config.merr)
 | 
			
		||||
		{
 | 
			
		||||
		  fprintf (stderr, "%s: %s: ", APPNAME, path);
 | 
			
		||||
		  perror (NULL);
 | 
			
		||||
		}
 | 
			
		||||
	      errno = 0;
 | 
			
		||||
	      close (fd);
 | 
			
		||||
	      return NULL;
 | 
			
		||||
	    }
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
      if (mode[0] == 'r')
 | 
			
		||||
	{
 | 
			
		||||
	  if (config.format == MBOX)
 | 
			
		||||
	    mp->fp = (FILE *) m_fdopen (fd, "r");
 | 
			
		||||
#ifdef HAVE_LIBZ
 | 
			
		||||
	  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");
 | 
			
		||||
#endif /* HAVE_LIBBZ2 */
 | 
			
		||||
	}
 | 
			
		||||
      else if (mode[0] == '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");
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
#ifdef HAVE_LIBBZ2
 | 
			
		||||
	  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 (mode[0] == 'r')
 | 
			
		||||
    {
 | 
			
		||||
      if (config.format == MBOX)
 | 
			
		||||
	fgets (buffer, BUFSIZ, mp->fp);
 | 
			
		||||
#ifdef HAVE_LIBZ
 | 
			
		||||
      else if (config.format == ZMBOX)
 | 
			
		||||
	gzgets (mp->fp, buffer, BUFSIZ);
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
#ifdef HAVE_LIBBZ2
 | 
			
		||||
      else if (config.format == BZ2MBOX)
 | 
			
		||||
	{
 | 
			
		||||
	  char c[1] = "\0";
 | 
			
		||||
	  int n = 0;
 | 
			
		||||
 | 
			
		||||
	  while (c[0] != '\n' && n < BUFSIZ)
 | 
			
		||||
	    {
 | 
			
		||||
	      BZ2_bzread (mp->fp, c, 1);
 | 
			
		||||
	      buffer[n] = c[0];
 | 
			
		||||
	      n++;
 | 
			
		||||
	    }
 | 
			
		||||
	  buffer[n] = '\0';
 | 
			
		||||
	}
 | 
			
		||||
#endif /* HAVE_LIBBZ2 */
 | 
			
		||||
 | 
			
		||||
      if (0 != strncmp ("From ", buffer, 5))
 | 
			
		||||
	{
 | 
			
		||||
	  if (config.merr)
 | 
			
		||||
	    {
 | 
			
		||||
	      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);
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
#ifdef HAVE_LIBBZ2
 | 
			
		||||
	  else if (config.format == BZ2MBOX)
 | 
			
		||||
	    BZ2_bzclose (mp->fp);
 | 
			
		||||
#endif /* HAVE_LIBBZ2 */
 | 
			
		||||
	  return NULL;
 | 
			
		||||
	}
 | 
			
		||||
      strcpy (mp->postmark_cache, buffer);
 | 
			
		||||
    }
 | 
			
		||||
  return mp;
 | 
			
		||||
}
 | 
			
		||||
/* }}} */
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
mbox_close (mbox_t * mp)
 | 
			
		||||
     /* {{{  */
 | 
			
		||||
{
 | 
			
		||||
  if (config.format == MBOX)
 | 
			
		||||
    fclose (mp->fp);
 | 
			
		||||
#ifdef HAVE_LIBZ
 | 
			
		||||
  else if (config.format == ZMBOX)
 | 
			
		||||
    gzclose (mp->fp);
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
#ifdef HAVE_LIBBZ2
 | 
			
		||||
  else if (config.format == BZ2MBOX)
 | 
			
		||||
    BZ2_bzclose (mp->fp);
 | 
			
		||||
#endif /* HAVE_LIBBZ2 */
 | 
			
		||||
 | 
			
		||||
  free (mp->postmark_cache);
 | 
			
		||||
  free (mp);
 | 
			
		||||
}
 | 
			
		||||
/* }}} */
 | 
			
		||||
 | 
			
		||||
message_t *
 | 
			
		||||
mbox_read_message (mbox_t * mp)
 | 
			
		||||
     /* {{{  */
 | 
			
		||||
 | 
			
		||||
{
 | 
			
		||||
  int isheaders = 1, s;
 | 
			
		||||
  char buffer[BUFSIZ];
 | 
			
		||||
  message_t *message;
 | 
			
		||||
 | 
			
		||||
  message = (message_t *) xmalloc (sizeof (message_t));
 | 
			
		||||
 | 
			
		||||
  message->headers = (char *) xmalloc (sizeof (char));
 | 
			
		||||
  message->headers[0] = '\0';
 | 
			
		||||
  message->hbytes = 0;
 | 
			
		||||
 | 
			
		||||
  message->body = (char *) xmalloc (sizeof (char));
 | 
			
		||||
  message->body[0] = '\0';
 | 
			
		||||
  message->bbytes = 0;
 | 
			
		||||
 | 
			
		||||
  message->from = NULL;
 | 
			
		||||
 | 
			
		||||
  s = strlen (mp->postmark_cache);
 | 
			
		||||
  message->headers =
 | 
			
		||||
    (char *) realloc (message->headers,
 | 
			
		||||
                      ((1 + s + message->hbytes) * sizeof (char)));
 | 
			
		||||
  strcpy (message->headers + message->hbytes, mp->postmark_cache);
 | 
			
		||||
  message->hbytes += s;
 | 
			
		||||
 | 
			
		||||
  for (;;)
 | 
			
		||||
    {
 | 
			
		||||
      if (config.format == MBOX)
 | 
			
		||||
	{
 | 
			
		||||
	  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;
 | 
			
		||||
	    }
 | 
			
		||||
	}
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
 | 
			
		||||
#ifdef HAVE_LIBBZ2
 | 
			
		||||
      else if (config.format == BZ2MBOX)
 | 
			
		||||
	{
 | 
			
		||||
	  char c[1] = "\0";
 | 
			
		||||
	  int n = 0;
 | 
			
		||||
 | 
			
		||||
	  while (c[0] != '\n' && n < BUFSIZ)
 | 
			
		||||
	    {
 | 
			
		||||
	      BZ2_bzread (mp->fp, c, 1);
 | 
			
		||||
	      buffer[n] = c[0];
 | 
			
		||||
	      n++;
 | 
			
		||||
	    }
 | 
			
		||||
	  buffer[n] = '\0';
 | 
			
		||||
 | 
			
		||||
	  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;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
      if (isheaders)
 | 
			
		||||
	{
 | 
			
		||||
	  message->headers =
 | 
			
		||||
	    (char *) realloc (message->headers,
 | 
			
		||||
			      ((1 + s + message->hbytes) * sizeof (char)));
 | 
			
		||||
	  strcpy (message->headers + message->hbytes, buffer);
 | 
			
		||||
	  message->hbytes += s;
 | 
			
		||||
	}			/* if */
 | 
			
		||||
      else
 | 
			
		||||
	{
 | 
			
		||||
	  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 */
 | 
			
		||||
  return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* }}} */
 | 
			
		||||
 | 
			
		||||
void *
 | 
			
		||||
tmpfile_open (const char *path)
 | 
			
		||||
     /* {{{  */
 | 
			
		||||
 | 
			
		||||
{
 | 
			
		||||
  extern char *tmpfilename;
 | 
			
		||||
  char *fname;
 | 
			
		||||
  char *tmpdir;
 | 
			
		||||
  int foo;
 | 
			
		||||
 | 
			
		||||
  if (path == NULL) { /* no path prefix given, use /tmp or TMPDIR */
 | 
			
		||||
    tmpdir = getenv ("TMPDIR");
 | 
			
		||||
    if (tmpdir == NULL)
 | 
			
		||||
      tmpdir = xstrdup ("/tmp");
 | 
			
		||||
    fname = xstrdup ("/mboxgrepXXXXXX");
 | 
			
		||||
  }
 | 
			
		||||
  else {
 | 
			
		||||
    tmpdir = (char *)path;
 | 
			
		||||
    fname = xstrdup (".XXXXXX");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  tmpfilename = (char *) xmalloc ((strlen (tmpdir) + (strlen (fname) + 1))
 | 
			
		||||
				    * sizeof (char));
 | 
			
		||||
  sprintf (tmpfilename, "%s%s", tmpdir, fname);
 | 
			
		||||
  foo = mkstemp (tmpfilename);
 | 
			
		||||
  if (-1 == foo)
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	{
 | 
			
		||||
	  fprintf (stderr, "%s: %s: ", APPNAME, tmpfilename);
 | 
			
		||||
	  perror (NULL);
 | 
			
		||||
	}
 | 
			
		||||
      exit (2);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  if (config.format == MBOX)
 | 
			
		||||
    return (m_fdopen (foo, "w"));
 | 
			
		||||
#ifdef HAVE_LIBZ
 | 
			
		||||
  else if (config.format == ZMBOX)
 | 
			
		||||
    return (m_gzdopen (foo, "wb"));
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
#ifdef HAVE_LIBBZ2
 | 
			
		||||
  else if (config.format == BZ2MBOX)
 | 
			
		||||
    return (BZ2_bzdopen (foo, "wb"));
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
 | 
			
		||||
  return NULL; /* not reached */
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* }}} */
 | 
			
		||||
							
								
								
									
										41
									
								
								src/mbox.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								src/mbox.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
			
		||||
/* -*- C -*- 
 | 
			
		||||
   mboxgrep - scan mailbox for messages matching a regular expression
 | 
			
		||||
   Copyright (C) 2000, 2001, 2002, 2003  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
 | 
			
		||||
   the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
   (at your option) any later version.
 | 
			
		||||
   
 | 
			
		||||
   Mboxgrep is distributed in the hope that it will be useful, but 
 | 
			
		||||
   WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
   GNU General Public License for more details.
 | 
			
		||||
   
 | 
			
		||||
   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.9 2003/08/24 19:23:50 dspiljar Exp $ */
 | 
			
		||||
 | 
			
		||||
#ifndef MBOX_H
 | 
			
		||||
#define MBOX_H 1
 | 
			
		||||
 | 
			
		||||
#include <config.h>
 | 
			
		||||
 | 
			
		||||
#include "mboxgrep.h"           /* for message_t structure */
 | 
			
		||||
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
  void *fp;
 | 
			
		||||
  void *fp_raw;
 | 
			
		||||
  char *postmark_cache;
 | 
			
		||||
} mbox_t;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
mbox_t *mbox_open (const char *path, const char *mode);
 | 
			
		||||
void *tmpfile_open (const char *path);
 | 
			
		||||
void mbox_close (mbox_t * mbp);
 | 
			
		||||
message_t *mbox_read_message (mbox_t * mbp);
 | 
			
		||||
 | 
			
		||||
#endif /* MBOX_H */
 | 
			
		||||
							
								
								
									
										134
									
								
								src/mboxgrep.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										134
									
								
								src/mboxgrep.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,134 @@
 | 
			
		||||
/* -*- C -*- 
 | 
			
		||||
  mboxgrep - scan mailbox for messages matching a regular expression
 | 
			
		||||
  Copyright (C) 2000, 2001, 2002, 2003  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
 | 
			
		||||
  the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
  (at your option) any later version.
 | 
			
		||||
 | 
			
		||||
  Mboxgrep is distributed in the hope that it will be useful, but 
 | 
			
		||||
  WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
  GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
  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: mboxgrep.h,v 1.21 2003/04/06 21:01:49 dspiljar Exp $ */
 | 
			
		||||
 | 
			
		||||
#ifndef MBOXGREP_H
 | 
			
		||||
#define MBOXGREP_H
 | 
			
		||||
 | 
			
		||||
#define APPNAME "mboxgrep"
 | 
			
		||||
#define VERSION "0.7.9"
 | 
			
		||||
#define BUGREPORT_ADDR "dspiljar@world.std.com"
 | 
			
		||||
 | 
			
		||||
#define HOST_NAME_SIZE 256
 | 
			
		||||
 | 
			
		||||
#include <config.h>
 | 
			
		||||
 | 
			
		||||
#include <time.h>  /* for tm structure */
 | 
			
		||||
 | 
			
		||||
#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 */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
typedef enum
 | 
			
		||||
  {
 | 
			
		||||
    MBOX,
 | 
			
		||||
    ZMBOX,
 | 
			
		||||
    MH,
 | 
			
		||||
    NNML,
 | 
			
		||||
    NNMH,
 | 
			
		||||
    MAILDIR,
 | 
			
		||||
    BZ2MBOX
 | 
			
		||||
  }
 | 
			
		||||
format_t;
 | 
			
		||||
 | 
			
		||||
typedef enum
 | 
			
		||||
  {
 | 
			
		||||
    NONE,
 | 
			
		||||
    FCNTL,
 | 
			
		||||
    FLOCK
 | 
			
		||||
  }
 | 
			
		||||
lockmethod_t;
 | 
			
		||||
 | 
			
		||||
typedef enum
 | 
			
		||||
  {
 | 
			
		||||
    DISPLAY,
 | 
			
		||||
    WRITE,
 | 
			
		||||
    COUNT,
 | 
			
		||||
    DELETE,
 | 
			
		||||
    PIPE
 | 
			
		||||
  }
 | 
			
		||||
action_t;
 | 
			
		||||
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
  char *filename; /* used with directory formats, such as maildir or MH */
 | 
			
		||||
  char *msgid; 
 | 
			
		||||
  char *from;
 | 
			
		||||
  char *headers;
 | 
			
		||||
  int hbytes;
 | 
			
		||||
  char *body;
 | 
			
		||||
  int bbytes;
 | 
			
		||||
  time_t date;
 | 
			
		||||
}
 | 
			
		||||
message_t;
 | 
			
		||||
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
  FILE *fp;
 | 
			
		||||
  DIR *dp;
 | 
			
		||||
  DIR *newp;
 | 
			
		||||
  DIR *curp;
 | 
			
		||||
  char *path;
 | 
			
		||||
}
 | 
			
		||||
folder_t;
 | 
			
		||||
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
  char **md5;
 | 
			
		||||
  int n;
 | 
			
		||||
}
 | 
			
		||||
checksum_t;
 | 
			
		||||
 | 
			
		||||
typedef struct
 | 
			
		||||
{
 | 
			
		||||
  int extended;
 | 
			
		||||
  int perl;
 | 
			
		||||
  
 | 
			
		||||
  int body;
 | 
			
		||||
  int headers;
 | 
			
		||||
  int dedup;
 | 
			
		||||
  int recursive;
 | 
			
		||||
  int invert;
 | 
			
		||||
  int count;
 | 
			
		||||
  int ignorecase;
 | 
			
		||||
  int merr;
 | 
			
		||||
  int pid;
 | 
			
		||||
 | 
			
		||||
  char hostname[HOST_NAME_SIZE];
 | 
			
		||||
 | 
			
		||||
  action_t action;
 | 
			
		||||
  format_t format;
 | 
			
		||||
  lockmethod_t lock;
 | 
			
		||||
}
 | 
			
		||||
option_t;
 | 
			
		||||
 | 
			
		||||
option_t config;
 | 
			
		||||
 | 
			
		||||
#endif /* MBOXGREP_H */
 | 
			
		||||
							
								
								
									
										443
									
								
								src/md5.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										443
									
								
								src/md5.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,443 @@
 | 
			
		||||
/* Functions to compute MD5 message digest of files or memory blocks.
 | 
			
		||||
   according to the definition of MD5 in RFC 1321 from April 1992.
 | 
			
		||||
   Copyright (C) 1995, 1996, 1997, 1999, 2000 Free Software Foundation, Inc.
 | 
			
		||||
   This file is part of the GNU C Library.
 | 
			
		||||
 | 
			
		||||
   The GNU C Library is free software; you can redistribute it and/or
 | 
			
		||||
   modify it under the terms of the GNU Lesser General Public
 | 
			
		||||
   License as published by the Free Software Foundation; either
 | 
			
		||||
   version 2.1 of the License, or (at your option) any later version.
 | 
			
		||||
 | 
			
		||||
   The GNU C Library is distributed in the hope that it will be useful,
 | 
			
		||||
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 | 
			
		||||
   Lesser General Public License for more details.
 | 
			
		||||
 | 
			
		||||
   You should have received a copy of the GNU Lesser General Public
 | 
			
		||||
   License along with the GNU C Library; if not, write to the Free
 | 
			
		||||
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 | 
			
		||||
   02111-1307 USA.  */
 | 
			
		||||
 | 
			
		||||
/* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.  */
 | 
			
		||||
 | 
			
		||||
#ifdef HAVE_CONFIG_H
 | 
			
		||||
# include <config.h>
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
 | 
			
		||||
#if STDC_HEADERS || defined _LIBC
 | 
			
		||||
# include <stdlib.h>
 | 
			
		||||
# include <string.h>
 | 
			
		||||
#else
 | 
			
		||||
# ifndef HAVE_MEMCPY
 | 
			
		||||
#  define memcpy(d, s, n) bcopy ((s), (d), (n))
 | 
			
		||||
# endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include "md5.h"
 | 
			
		||||
 | 
			
		||||
#ifdef _LIBC
 | 
			
		||||
# include <endian.h>
 | 
			
		||||
# if __BYTE_ORDER == __BIG_ENDIAN
 | 
			
		||||
#  define WORDS_BIGENDIAN 1
 | 
			
		||||
# endif
 | 
			
		||||
/* We need to keep the namespace clean so define the MD5 function
 | 
			
		||||
   protected using leading __ .  */
 | 
			
		||||
# define md5_init_ctx __md5_init_ctx
 | 
			
		||||
# define md5_process_block __md5_process_block
 | 
			
		||||
# define md5_process_bytes __md5_process_bytes
 | 
			
		||||
# define md5_finish_ctx __md5_finish_ctx
 | 
			
		||||
# define md5_read_ctx __md5_read_ctx
 | 
			
		||||
# define md5_stream __md5_stream
 | 
			
		||||
# define md5_buffer __md5_buffer
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef WORDS_BIGENDIAN
 | 
			
		||||
# define SWAP(n)							\
 | 
			
		||||
    (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
 | 
			
		||||
#else
 | 
			
		||||
# define SWAP(n) (n)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* This array contains the bytes used to pad the buffer to the next
 | 
			
		||||
   64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
 | 
			
		||||
static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* Initialize structure containing state of computation.
 | 
			
		||||
   (RFC 1321, 3.3: Step 3)  */
 | 
			
		||||
void
 | 
			
		||||
md5_init_ctx (ctx)
 | 
			
		||||
     struct md5_ctx *ctx;
 | 
			
		||||
{
 | 
			
		||||
  ctx->A = 0x67452301;
 | 
			
		||||
  ctx->B = 0xefcdab89;
 | 
			
		||||
  ctx->C = 0x98badcfe;
 | 
			
		||||
  ctx->D = 0x10325476;
 | 
			
		||||
 | 
			
		||||
  ctx->total[0] = ctx->total[1] = 0;
 | 
			
		||||
  ctx->buflen = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Put result from CTX in first 16 bytes following RESBUF.  The result
 | 
			
		||||
   must be in little endian byte order.
 | 
			
		||||
 | 
			
		||||
   IMPORTANT: On some systems it is required that RESBUF is correctly
 | 
			
		||||
   aligned for a 32 bits value.  */
 | 
			
		||||
void *
 | 
			
		||||
md5_read_ctx (ctx, resbuf)
 | 
			
		||||
     const struct md5_ctx *ctx;
 | 
			
		||||
     void *resbuf;
 | 
			
		||||
{
 | 
			
		||||
  ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
 | 
			
		||||
  ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
 | 
			
		||||
  ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
 | 
			
		||||
  ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
 | 
			
		||||
 | 
			
		||||
  return resbuf;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Process the remaining bytes in the internal buffer and the usual
 | 
			
		||||
   prolog according to the standard and write the result to RESBUF.
 | 
			
		||||
 | 
			
		||||
   IMPORTANT: On some systems it is required that RESBUF is correctly
 | 
			
		||||
   aligned for a 32 bits value.  */
 | 
			
		||||
void *
 | 
			
		||||
md5_finish_ctx (ctx, resbuf)
 | 
			
		||||
     struct md5_ctx *ctx;
 | 
			
		||||
     void *resbuf;
 | 
			
		||||
{
 | 
			
		||||
  /* Take yet unprocessed bytes into account.  */
 | 
			
		||||
  md5_uint32 bytes = ctx->buflen;
 | 
			
		||||
  size_t pad;
 | 
			
		||||
 | 
			
		||||
  /* Now count remaining bytes.  */
 | 
			
		||||
  ctx->total[0] += bytes;
 | 
			
		||||
  if (ctx->total[0] < bytes)
 | 
			
		||||
    ++ctx->total[1];
 | 
			
		||||
 | 
			
		||||
  pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
 | 
			
		||||
  memcpy (&ctx->buffer[bytes], fillbuf, pad);
 | 
			
		||||
 | 
			
		||||
  /* Put the 64-bit file length in *bits* at the end of the buffer.  */
 | 
			
		||||
  *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3);
 | 
			
		||||
  *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) |
 | 
			
		||||
							(ctx->total[0] >> 29));
 | 
			
		||||
 | 
			
		||||
  /* Process last bytes.  */
 | 
			
		||||
  md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
 | 
			
		||||
 | 
			
		||||
  return md5_read_ctx (ctx, resbuf);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Compute MD5 message digest for bytes read from STREAM.  The
 | 
			
		||||
   resulting message digest number will be written into the 16 bytes
 | 
			
		||||
   beginning at RESBLOCK.  */
 | 
			
		||||
int
 | 
			
		||||
md5_stream (stream, resblock)
 | 
			
		||||
     FILE *stream;
 | 
			
		||||
     void *resblock;
 | 
			
		||||
{
 | 
			
		||||
  /* Important: BLOCKSIZE must be a multiple of 64.  */
 | 
			
		||||
#define BLOCKSIZE 4096
 | 
			
		||||
  struct md5_ctx ctx;
 | 
			
		||||
  char buffer[BLOCKSIZE + 72];
 | 
			
		||||
  size_t sum;
 | 
			
		||||
 | 
			
		||||
  /* Initialize the computation context.  */
 | 
			
		||||
  md5_init_ctx (&ctx);
 | 
			
		||||
 | 
			
		||||
  /* Iterate over full file contents.  */
 | 
			
		||||
  while (1)
 | 
			
		||||
    {
 | 
			
		||||
      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
 | 
			
		||||
	 computation function processes the whole buffer so that with the
 | 
			
		||||
	 next round of the loop another block can be read.  */
 | 
			
		||||
      size_t n;
 | 
			
		||||
      sum = 0;
 | 
			
		||||
 | 
			
		||||
      /* Read block.  Take care for partial reads.  */
 | 
			
		||||
      do
 | 
			
		||||
	{
 | 
			
		||||
	  n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
 | 
			
		||||
 | 
			
		||||
	  sum += n;
 | 
			
		||||
	}
 | 
			
		||||
      while (sum < BLOCKSIZE && n != 0);
 | 
			
		||||
      if (n == 0 && ferror (stream))
 | 
			
		||||
        return 1;
 | 
			
		||||
 | 
			
		||||
      /* If end of file is reached, end the loop.  */
 | 
			
		||||
      if (n == 0)
 | 
			
		||||
	break;
 | 
			
		||||
 | 
			
		||||
      /* Process buffer with BLOCKSIZE bytes.  Note that
 | 
			
		||||
			BLOCKSIZE % 64 == 0
 | 
			
		||||
       */
 | 
			
		||||
      md5_process_block (buffer, BLOCKSIZE, &ctx);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  /* Add the last bytes if necessary.  */
 | 
			
		||||
  if (sum > 0)
 | 
			
		||||
    md5_process_bytes (buffer, sum, &ctx);
 | 
			
		||||
 | 
			
		||||
  /* Construct result in desired memory.  */
 | 
			
		||||
  md5_finish_ctx (&ctx, resblock);
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
 | 
			
		||||
   result is always in little endian byte order, so that a byte-wise
 | 
			
		||||
   output yields to the wanted ASCII representation of the message
 | 
			
		||||
   digest.  */
 | 
			
		||||
void *
 | 
			
		||||
md5_buffer (buffer, len, resblock)
 | 
			
		||||
     const char *buffer;
 | 
			
		||||
     size_t len;
 | 
			
		||||
     void *resblock;
 | 
			
		||||
{
 | 
			
		||||
  struct md5_ctx ctx;
 | 
			
		||||
 | 
			
		||||
  /* Initialize the computation context.  */
 | 
			
		||||
  md5_init_ctx (&ctx);
 | 
			
		||||
 | 
			
		||||
  /* Process whole buffer but last len % 64 bytes.  */
 | 
			
		||||
  md5_process_bytes (buffer, len, &ctx);
 | 
			
		||||
 | 
			
		||||
  /* Put result in desired memory area.  */
 | 
			
		||||
  return md5_finish_ctx (&ctx, resblock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
md5_process_bytes (buffer, len, ctx)
 | 
			
		||||
     const void *buffer;
 | 
			
		||||
     size_t len;
 | 
			
		||||
     struct md5_ctx *ctx;
 | 
			
		||||
{
 | 
			
		||||
  //const void aligned_buffer = buffer;
 | 
			
		||||
 | 
			
		||||
  /* When we already have some bits in our internal buffer concatenate
 | 
			
		||||
     both inputs first.  */
 | 
			
		||||
  if (ctx->buflen != 0)
 | 
			
		||||
    {
 | 
			
		||||
      size_t left_over = ctx->buflen;
 | 
			
		||||
      size_t add = 128 - left_over > len ? len : 128 - left_over;
 | 
			
		||||
 | 
			
		||||
      /* Only put full words in the buffer.  */
 | 
			
		||||
      add -= add % __alignof__ (md5_uint32);
 | 
			
		||||
 | 
			
		||||
      memcpy (&ctx->buffer[left_over], buffer, add);
 | 
			
		||||
      ctx->buflen += add;
 | 
			
		||||
 | 
			
		||||
      if (ctx->buflen > 64)
 | 
			
		||||
	{
 | 
			
		||||
	  md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
 | 
			
		||||
 | 
			
		||||
	  ctx->buflen &= 63;
 | 
			
		||||
	  /* The regions in the following copy operation cannot overlap.  */
 | 
			
		||||
	  memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
 | 
			
		||||
		  ctx->buflen);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
      buffer = (const char *) buffer + add;
 | 
			
		||||
      len -= add;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  /* Process available complete blocks.  */
 | 
			
		||||
  if (len > 64)
 | 
			
		||||
    {
 | 
			
		||||
      md5_process_block (buffer, len & ~63, ctx);
 | 
			
		||||
      buffer = (const char *) buffer + (len & ~63);
 | 
			
		||||
      len &= 63;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  /* Move remaining bytes in internal buffer.  */
 | 
			
		||||
  if (len > 0)
 | 
			
		||||
    {
 | 
			
		||||
      size_t left_over = ctx->buflen;
 | 
			
		||||
 | 
			
		||||
      memcpy (&ctx->buffer[left_over], buffer, len);
 | 
			
		||||
      left_over += len;
 | 
			
		||||
      if (left_over >= 64)
 | 
			
		||||
	{
 | 
			
		||||
	  md5_process_block (ctx->buffer, 64, ctx);
 | 
			
		||||
	  left_over -= 64;
 | 
			
		||||
	  memcpy (ctx->buffer, &ctx->buffer[64], left_over);
 | 
			
		||||
	}
 | 
			
		||||
      ctx->buflen = left_over;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* These are the four functions used in the four steps of the MD5 algorithm
 | 
			
		||||
   and defined in the RFC 1321.  The first function is a little bit optimized
 | 
			
		||||
   (as found in Colin Plumbs public domain implementation).  */
 | 
			
		||||
/* #define FF(b, c, d) ((b & c) | (~b & d)) */
 | 
			
		||||
#define FF(b, c, d) (d ^ (b & (c ^ d)))
 | 
			
		||||
#define FG(b, c, d) FF (d, b, c)
 | 
			
		||||
#define FH(b, c, d) (b ^ c ^ d)
 | 
			
		||||
#define FI(b, c, d) (c ^ (b | ~d))
 | 
			
		||||
 | 
			
		||||
/* Process LEN bytes of BUFFER, accumulating context into CTX.
 | 
			
		||||
   It is assumed that LEN % 64 == 0.  */
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
md5_process_block (buffer, len, ctx)
 | 
			
		||||
     const void *buffer;
 | 
			
		||||
     size_t len;
 | 
			
		||||
     struct md5_ctx *ctx;
 | 
			
		||||
{
 | 
			
		||||
  md5_uint32 correct_words[16];
 | 
			
		||||
  const md5_uint32 *words = buffer;
 | 
			
		||||
  size_t nwords = len / sizeof (md5_uint32);
 | 
			
		||||
  const md5_uint32 *endp = words + nwords;
 | 
			
		||||
  md5_uint32 A = ctx->A;
 | 
			
		||||
  md5_uint32 B = ctx->B;
 | 
			
		||||
  md5_uint32 C = ctx->C;
 | 
			
		||||
  md5_uint32 D = ctx->D;
 | 
			
		||||
 | 
			
		||||
  /* First increment the byte count.  RFC 1321 specifies the possible
 | 
			
		||||
     length of the file up to 2^64 bits.  Here we only compute the
 | 
			
		||||
     number of bytes.  Do a double word increment.  */
 | 
			
		||||
  ctx->total[0] += len;
 | 
			
		||||
  if (ctx->total[0] < len)
 | 
			
		||||
    ++ctx->total[1];
 | 
			
		||||
 | 
			
		||||
  /* Process all bytes in the buffer with 64 bytes in each round of
 | 
			
		||||
     the loop.  */
 | 
			
		||||
  while (words < endp)
 | 
			
		||||
    {
 | 
			
		||||
      md5_uint32 *cwp = correct_words;
 | 
			
		||||
      md5_uint32 A_save = A;
 | 
			
		||||
      md5_uint32 B_save = B;
 | 
			
		||||
      md5_uint32 C_save = C;
 | 
			
		||||
      md5_uint32 D_save = D;
 | 
			
		||||
 | 
			
		||||
      /* First round: using the given function, the context and a constant
 | 
			
		||||
	 the next context is computed.  Because the algorithms processing
 | 
			
		||||
	 unit is a 32-bit word and it is determined to work on words in
 | 
			
		||||
	 little endian byte order we perhaps have to change the byte order
 | 
			
		||||
	 before the computation.  To reduce the work for the next steps
 | 
			
		||||
	 we store the swapped words in the array CORRECT_WORDS.  */
 | 
			
		||||
 | 
			
		||||
#define OP(a, b, c, d, s, T)						\
 | 
			
		||||
      do								\
 | 
			
		||||
        {								\
 | 
			
		||||
	  a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T;		\
 | 
			
		||||
	  ++words;							\
 | 
			
		||||
	  CYCLIC (a, s);						\
 | 
			
		||||
	  a += b;							\
 | 
			
		||||
        }								\
 | 
			
		||||
      while (0)
 | 
			
		||||
 | 
			
		||||
      /* It is unfortunate that C does not provide an operator for
 | 
			
		||||
	 cyclic rotation.  Hope the C compiler is smart enough.  */
 | 
			
		||||
#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
 | 
			
		||||
 | 
			
		||||
      /* Before we start, one word to the strange constants.
 | 
			
		||||
	 They are defined in RFC 1321 as
 | 
			
		||||
 | 
			
		||||
	 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
 | 
			
		||||
       */
 | 
			
		||||
 | 
			
		||||
      /* Round 1.  */
 | 
			
		||||
      OP (A, B, C, D,  7, 0xd76aa478);
 | 
			
		||||
      OP (D, A, B, C, 12, 0xe8c7b756);
 | 
			
		||||
      OP (C, D, A, B, 17, 0x242070db);
 | 
			
		||||
      OP (B, C, D, A, 22, 0xc1bdceee);
 | 
			
		||||
      OP (A, B, C, D,  7, 0xf57c0faf);
 | 
			
		||||
      OP (D, A, B, C, 12, 0x4787c62a);
 | 
			
		||||
      OP (C, D, A, B, 17, 0xa8304613);
 | 
			
		||||
      OP (B, C, D, A, 22, 0xfd469501);
 | 
			
		||||
      OP (A, B, C, D,  7, 0x698098d8);
 | 
			
		||||
      OP (D, A, B, C, 12, 0x8b44f7af);
 | 
			
		||||
      OP (C, D, A, B, 17, 0xffff5bb1);
 | 
			
		||||
      OP (B, C, D, A, 22, 0x895cd7be);
 | 
			
		||||
      OP (A, B, C, D,  7, 0x6b901122);
 | 
			
		||||
      OP (D, A, B, C, 12, 0xfd987193);
 | 
			
		||||
      OP (C, D, A, B, 17, 0xa679438e);
 | 
			
		||||
      OP (B, C, D, A, 22, 0x49b40821);
 | 
			
		||||
 | 
			
		||||
      /* For the second to fourth round we have the possibly swapped words
 | 
			
		||||
	 in CORRECT_WORDS.  Redefine the macro to take an additional first
 | 
			
		||||
	 argument specifying the function to use.  */
 | 
			
		||||
#undef OP
 | 
			
		||||
#define OP(f, a, b, c, d, k, s, T)					\
 | 
			
		||||
      do 								\
 | 
			
		||||
	{								\
 | 
			
		||||
	  a += f (b, c, d) + correct_words[k] + T;			\
 | 
			
		||||
	  CYCLIC (a, s);						\
 | 
			
		||||
	  a += b;							\
 | 
			
		||||
	}								\
 | 
			
		||||
      while (0)
 | 
			
		||||
 | 
			
		||||
      /* Round 2.  */
 | 
			
		||||
      OP (FG, A, B, C, D,  1,  5, 0xf61e2562);
 | 
			
		||||
      OP (FG, D, A, B, C,  6,  9, 0xc040b340);
 | 
			
		||||
      OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
 | 
			
		||||
      OP (FG, B, C, D, A,  0, 20, 0xe9b6c7aa);
 | 
			
		||||
      OP (FG, A, B, C, D,  5,  5, 0xd62f105d);
 | 
			
		||||
      OP (FG, D, A, B, C, 10,  9, 0x02441453);
 | 
			
		||||
      OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
 | 
			
		||||
      OP (FG, B, C, D, A,  4, 20, 0xe7d3fbc8);
 | 
			
		||||
      OP (FG, A, B, C, D,  9,  5, 0x21e1cde6);
 | 
			
		||||
      OP (FG, D, A, B, C, 14,  9, 0xc33707d6);
 | 
			
		||||
      OP (FG, C, D, A, B,  3, 14, 0xf4d50d87);
 | 
			
		||||
      OP (FG, B, C, D, A,  8, 20, 0x455a14ed);
 | 
			
		||||
      OP (FG, A, B, C, D, 13,  5, 0xa9e3e905);
 | 
			
		||||
      OP (FG, D, A, B, C,  2,  9, 0xfcefa3f8);
 | 
			
		||||
      OP (FG, C, D, A, B,  7, 14, 0x676f02d9);
 | 
			
		||||
      OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
 | 
			
		||||
 | 
			
		||||
      /* Round 3.  */
 | 
			
		||||
      OP (FH, A, B, C, D,  5,  4, 0xfffa3942);
 | 
			
		||||
      OP (FH, D, A, B, C,  8, 11, 0x8771f681);
 | 
			
		||||
      OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
 | 
			
		||||
      OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
 | 
			
		||||
      OP (FH, A, B, C, D,  1,  4, 0xa4beea44);
 | 
			
		||||
      OP (FH, D, A, B, C,  4, 11, 0x4bdecfa9);
 | 
			
		||||
      OP (FH, C, D, A, B,  7, 16, 0xf6bb4b60);
 | 
			
		||||
      OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
 | 
			
		||||
      OP (FH, A, B, C, D, 13,  4, 0x289b7ec6);
 | 
			
		||||
      OP (FH, D, A, B, C,  0, 11, 0xeaa127fa);
 | 
			
		||||
      OP (FH, C, D, A, B,  3, 16, 0xd4ef3085);
 | 
			
		||||
      OP (FH, B, C, D, A,  6, 23, 0x04881d05);
 | 
			
		||||
      OP (FH, A, B, C, D,  9,  4, 0xd9d4d039);
 | 
			
		||||
      OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
 | 
			
		||||
      OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
 | 
			
		||||
      OP (FH, B, C, D, A,  2, 23, 0xc4ac5665);
 | 
			
		||||
 | 
			
		||||
      /* Round 4.  */
 | 
			
		||||
      OP (FI, A, B, C, D,  0,  6, 0xf4292244);
 | 
			
		||||
      OP (FI, D, A, B, C,  7, 10, 0x432aff97);
 | 
			
		||||
      OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
 | 
			
		||||
      OP (FI, B, C, D, A,  5, 21, 0xfc93a039);
 | 
			
		||||
      OP (FI, A, B, C, D, 12,  6, 0x655b59c3);
 | 
			
		||||
      OP (FI, D, A, B, C,  3, 10, 0x8f0ccc92);
 | 
			
		||||
      OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
 | 
			
		||||
      OP (FI, B, C, D, A,  1, 21, 0x85845dd1);
 | 
			
		||||
      OP (FI, A, B, C, D,  8,  6, 0x6fa87e4f);
 | 
			
		||||
      OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
 | 
			
		||||
      OP (FI, C, D, A, B,  6, 15, 0xa3014314);
 | 
			
		||||
      OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
 | 
			
		||||
      OP (FI, A, B, C, D,  4,  6, 0xf7537e82);
 | 
			
		||||
      OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
 | 
			
		||||
      OP (FI, C, D, A, B,  2, 15, 0x2ad7d2bb);
 | 
			
		||||
      OP (FI, B, C, D, A,  9, 21, 0xeb86d391);
 | 
			
		||||
 | 
			
		||||
      /* Add the starting values of the context.  */
 | 
			
		||||
      A += A_save;
 | 
			
		||||
      B += B_save;
 | 
			
		||||
      C += C_save;
 | 
			
		||||
      D += D_save;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  /* Put checksum in context given as argument.  */
 | 
			
		||||
  ctx->A = A;
 | 
			
		||||
  ctx->B = B;
 | 
			
		||||
  ctx->C = C;
 | 
			
		||||
  ctx->D = D;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										140
									
								
								src/md5.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										140
									
								
								src/md5.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,140 @@
 | 
			
		||||
/* md5.h - Declaration of functions and data types used for MD5 sum
 | 
			
		||||
   computing library functions.
 | 
			
		||||
   Copyright (C) 1995, 1996 Free Software Foundation, Inc.
 | 
			
		||||
   NOTE: The canonical source of this file is maintained with the GNU C
 | 
			
		||||
   Library.  Bugs can be reported to bug-glibc@prep.ai.mit.edu.
 | 
			
		||||
 | 
			
		||||
   This program is free software; you can redistribute it and/or modify it
 | 
			
		||||
   under the terms of the GNU General Public License as published by the
 | 
			
		||||
   Free Software Foundation; either version 2, or (at your option) any
 | 
			
		||||
   later version.
 | 
			
		||||
 | 
			
		||||
   This program is distributed in the hope that it will be useful,
 | 
			
		||||
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
   GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
   You should have received a copy of the GNU General Public License
 | 
			
		||||
   along with this program; if not, write to the Free Software Foundation,
 | 
			
		||||
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 | 
			
		||||
 | 
			
		||||
#ifndef _MD5_H
 | 
			
		||||
#define _MD5_H 1
 | 
			
		||||
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
#if defined HAVE_LIMITS_H || _LIBC
 | 
			
		||||
# include <limits.h>
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/* The following contortions are an attempt to use the C preprocessor
 | 
			
		||||
   to determine an unsigned integral type that is 32 bits wide.  An
 | 
			
		||||
   alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
 | 
			
		||||
   doing that would require that the configure script compile and *run*
 | 
			
		||||
   the resulting executable.  Locally running cross-compiled executables
 | 
			
		||||
   is usually not possible.  */
 | 
			
		||||
 | 
			
		||||
#ifdef _LIBC
 | 
			
		||||
# include <sys/types.h>
 | 
			
		||||
typedef u_int32_t md5_uint32;
 | 
			
		||||
#else
 | 
			
		||||
# if defined __STDC__ && __STDC__
 | 
			
		||||
#  define UINT_MAX_32_BITS 4294967295U
 | 
			
		||||
# else
 | 
			
		||||
#  define UINT_MAX_32_BITS 0xFFFFFFFF
 | 
			
		||||
# endif
 | 
			
		||||
 | 
			
		||||
/* If UINT_MAX isn't defined, assume it's a 32-bit type.
 | 
			
		||||
   This should be valid for all systems GNU cares about because
 | 
			
		||||
   that doesn't include 16-bit systems, and only modern systems
 | 
			
		||||
   (that certainly have <limits.h>) have 64+-bit integral types.  */
 | 
			
		||||
 | 
			
		||||
# ifndef UINT_MAX
 | 
			
		||||
#  define UINT_MAX UINT_MAX_32_BITS
 | 
			
		||||
# endif
 | 
			
		||||
 | 
			
		||||
# if UINT_MAX == UINT_MAX_32_BITS
 | 
			
		||||
   typedef unsigned int md5_uint32;
 | 
			
		||||
# else
 | 
			
		||||
#  if USHRT_MAX == UINT_MAX_32_BITS
 | 
			
		||||
    typedef unsigned short md5_uint32;
 | 
			
		||||
#  else
 | 
			
		||||
#   if ULONG_MAX == UINT_MAX_32_BITS
 | 
			
		||||
     typedef unsigned long md5_uint32;
 | 
			
		||||
#   else
 | 
			
		||||
     /* The following line is intended to evoke an error.
 | 
			
		||||
        Using #error is not portable enough.  */
 | 
			
		||||
     "Cannot determine unsigned 32-bit data type."
 | 
			
		||||
#   endif
 | 
			
		||||
#  endif
 | 
			
		||||
# endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/* Structure to save state of computation between the single steps.  */
 | 
			
		||||
struct md5_ctx
 | 
			
		||||
{
 | 
			
		||||
  md5_uint32 A;
 | 
			
		||||
  md5_uint32 B;
 | 
			
		||||
  md5_uint32 C;
 | 
			
		||||
  md5_uint32 D;
 | 
			
		||||
 | 
			
		||||
  md5_uint32 total[2];
 | 
			
		||||
  md5_uint32 buflen;
 | 
			
		||||
  char buffer[128];
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * The following three functions are build up the low level used in
 | 
			
		||||
 * the functions `md5_stream' and `md5_buffer'.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
/* Initialize structure containing state of computation.
 | 
			
		||||
   (RFC 1321, 3.3: Step 3)  */
 | 
			
		||||
extern void md5_init_ctx (struct md5_ctx *ctx);
 | 
			
		||||
 | 
			
		||||
/* Starting with the result of former calls of this function (or the
 | 
			
		||||
   initialization function update the context for the next LEN bytes
 | 
			
		||||
   starting at BUFFER.
 | 
			
		||||
   It is necessary that LEN is a multiple of 64!!! */
 | 
			
		||||
extern void md5_process_block (const void *buffer, size_t len,
 | 
			
		||||
				       struct md5_ctx *ctx);
 | 
			
		||||
 | 
			
		||||
/* Starting with the result of former calls of this function (or the
 | 
			
		||||
   initialization function update the context for the next LEN bytes
 | 
			
		||||
   starting at BUFFER.
 | 
			
		||||
   It is NOT required that LEN is a multiple of 64.  */
 | 
			
		||||
extern void md5_process_bytes (const void *buffer, size_t len,
 | 
			
		||||
				       struct md5_ctx *ctx);
 | 
			
		||||
 | 
			
		||||
/* Process the remaining bytes in the buffer and put result from CTX
 | 
			
		||||
   in first 16 bytes following RESBUF.  The result is always in little
 | 
			
		||||
   endian byte order, so that a byte-wise output yields to the wanted
 | 
			
		||||
   ASCII representation of the message digest.
 | 
			
		||||
 | 
			
		||||
   IMPORTANT: On some systems it is required that RESBUF is correctly
 | 
			
		||||
   aligned for a 32 bits value.  */
 | 
			
		||||
extern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* Put result from CTX in first 16 bytes following RESBUF.  The result is
 | 
			
		||||
   always in little endian byte order, so that a byte-wise output yields
 | 
			
		||||
   to the wanted ASCII representation of the message digest.
 | 
			
		||||
 | 
			
		||||
   IMPORTANT: On some systems it is required that RESBUF is correctly
 | 
			
		||||
   aligned for a 32 bits value.  */
 | 
			
		||||
extern void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* Compute MD5 message digest for bytes read from STREAM.  The
 | 
			
		||||
   resulting message digest number will be written into the 16 bytes
 | 
			
		||||
   beginning at RESBLOCK.  */
 | 
			
		||||
extern int md5_stream (FILE *stream, void *resblock);
 | 
			
		||||
 | 
			
		||||
/* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
 | 
			
		||||
   result is always in little endian byte order, so that a byte-wise
 | 
			
		||||
   output yields to the wanted ASCII representation of the message
 | 
			
		||||
   digest.  */
 | 
			
		||||
extern void *md5_buffer (const char *buffer, size_t len,
 | 
			
		||||
				 void *resblock);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										239
									
								
								src/mh.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										239
									
								
								src/mh.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,239 @@
 | 
			
		||||
/* -*- C -*- 
 | 
			
		||||
  mboxgrep - scan mailbox for messages matching a regular expression
 | 
			
		||||
  Copyright (C) 2000, 2001, 2002, 2003  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
 | 
			
		||||
  the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
  (at your option) any later version.
 | 
			
		||||
 | 
			
		||||
  Mboxgrep is distributed in the hope that it will be useful, but 
 | 
			
		||||
  WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
  GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
  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: mh.c,v 1.15 2003/03/30 23:07:10 dspiljar Exp $ */
 | 
			
		||||
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <string.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 <sys/types.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
 | 
			
		||||
#include "mboxgrep.h"
 | 
			
		||||
#include "misc.h"
 | 
			
		||||
#include "mh.h"
 | 
			
		||||
#include "wrap.h"
 | 
			
		||||
 | 
			
		||||
#ifdef HAVE_LIBDMALLOC
 | 
			
		||||
# include <dmalloc.h>
 | 
			
		||||
#endif /* HAVE_LIBDMALLOC */
 | 
			
		||||
 | 
			
		||||
extern option_t config;
 | 
			
		||||
 | 
			
		||||
DIR *mh_open (const char *path)
 | 
			
		||||
{
 | 
			
		||||
  DIR *dp;
 | 
			
		||||
 | 
			
		||||
  dp = opendir (path);
 | 
			
		||||
  if (dp == NULL)
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	{
 | 
			
		||||
	  fprintf (stderr, "%s: %s: ", APPNAME, path);
 | 
			
		||||
	  perror (NULL);
 | 
			
		||||
	}
 | 
			
		||||
      errno = 0;
 | 
			
		||||
      return NULL;
 | 
			
		||||
    }
 | 
			
		||||
  return dp;
 | 
			
		||||
} /* mh_open */
 | 
			
		||||
 | 
			
		||||
void mh_close (DIR *dp)
 | 
			
		||||
{
 | 
			
		||||
  closedir (dp);
 | 
			
		||||
} /* mh_close */
 | 
			
		||||
 | 
			
		||||
message_t *mh_read_message (DIR *dp)
 | 
			
		||||
{
 | 
			
		||||
  int isheaders = 1;
 | 
			
		||||
  int have_from = 0, have_return_path = 0,
 | 
			
		||||
    have_date = 0, have_sender = 0;
 | 
			
		||||
  static int s;
 | 
			
		||||
  message_t *message;
 | 
			
		||||
  struct dirent *d_content;
 | 
			
		||||
  char buffer[BUFSIZ], *filename;
 | 
			
		||||
  FILE *fp;
 | 
			
		||||
  extern char *boxname;
 | 
			
		||||
 | 
			
		||||
  message = (message_t *) xmalloc (sizeof (message_t));
 | 
			
		||||
 | 
			
		||||
  message->headers = NULL;
 | 
			
		||||
  message->hbytes = 0;
 | 
			
		||||
 | 
			
		||||
  message->body = NULL;
 | 
			
		||||
  message->bbytes = 0;
 | 
			
		||||
 | 
			
		||||
  message->from = NULL;
 | 
			
		||||
 | 
			
		||||
  filename = NULL;
 | 
			
		||||
 | 
			
		||||
  for(;;)
 | 
			
		||||
    {
 | 
			
		||||
      d_content = readdir(dp);
 | 
			
		||||
      if (d_content == NULL) return NULL;
 | 
			
		||||
      if (d_content->d_name[0] == '.')
 | 
			
		||||
	continue;
 | 
			
		||||
 | 
			
		||||
      filename = (char *) xrealloc 
 | 
			
		||||
	(filename, ((strlen (d_content->d_name)) + 
 | 
			
		||||
		    (strlen (boxname)) + 2));
 | 
			
		||||
 | 
			
		||||
/*       message->headers = (char *) xrealloc (message->headers, 0); */
 | 
			
		||||
/*       message->hbytes = 0; */
 | 
			
		||||
/*       message->body = (char *) xrealloc (message->body, 0); */
 | 
			
		||||
/*       message->bbytes = 0; */
 | 
			
		||||
 | 
			
		||||
      filename[0] = '\0';
 | 
			
		||||
      sprintf (filename, "%s/%s", boxname, d_content->d_name);
 | 
			
		||||
      fp = m_fopen (filename, "r");
 | 
			
		||||
      isheaders = 1;
 | 
			
		||||
      if (fp == NULL)
 | 
			
		||||
	{
 | 
			
		||||
	  free (message->headers);
 | 
			
		||||
	  free (message->body);
 | 
			
		||||
	  message->hbytes = 0;
 | 
			
		||||
	  message->bbytes = 0;
 | 
			
		||||
	  continue;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
      fgets (buffer, BUFSIZ, fp);
 | 
			
		||||
 | 
			
		||||
/*       if (config.format == NNML || config.format == NNMH) */
 | 
			
		||||
/* 	{ */
 | 
			
		||||
/* 	  if (0 != strncmp ("X-From-Line: ", buffer, 13)) */
 | 
			
		||||
/* 	    { */
 | 
			
		||||
/* 	      if (config.merr) */
 | 
			
		||||
/* 		fprintf (stderr, "%s: %s: Not a Gnus folder message\n",  */
 | 
			
		||||
/* 			 APPNAME, filename); */
 | 
			
		||||
/* 	      fclose (fp); */
 | 
			
		||||
/* 	      free (message->headers); */
 | 
			
		||||
/* 	      free (message->body); */
 | 
			
		||||
/* 	      message->hbytes = 0; */
 | 
			
		||||
/* 	      message->bbytes = 0; */
 | 
			
		||||
/* 	      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))
 | 
			
		||||
		{
 | 
			
		||||
		  have_return_path = 1;
 | 
			
		||||
		  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 */
 | 
			
		||||
 | 
			
		||||
      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);
 | 
			
		||||
 | 
			
		||||
	  return message;
 | 
			
		||||
	}
 | 
			
		||||
    } /* for */
 | 
			
		||||
} /* mh_read_message */
 | 
			
		||||
 | 
			
		||||
void mh_write_message (message_t *m, const char *path)
 | 
			
		||||
{
 | 
			
		||||
  struct dirent *dc;
 | 
			
		||||
  int x, y = 0;
 | 
			
		||||
  char s1[BUFSIZ];
 | 
			
		||||
  DIR *d; FILE *f;
 | 
			
		||||
 | 
			
		||||
  d = m_opendir (path);
 | 
			
		||||
  rewinddir (d);
 | 
			
		||||
 | 
			
		||||
  while ((dc = readdir (d)) != NULL)
 | 
			
		||||
    {
 | 
			
		||||
      x = strtol (dc->d_name, NULL, 10);
 | 
			
		||||
      if (x > y)
 | 
			
		||||
	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);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										47
									
								
								src/mh.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/mh.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
			
		||||
/* -*- C -*- 
 | 
			
		||||
  mboxgrep - scan mailbox for messages matching a regular expression
 | 
			
		||||
  Copyright (C) 2000, 2001, 2002, 2003  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
 | 
			
		||||
  the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
  (at your option) any later version.
 | 
			
		||||
 | 
			
		||||
  Mboxgrep is distributed in the hope that it will be useful, but 
 | 
			
		||||
  WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
  GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
  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: mh.h,v 1.6 2003/03/30 23:07:10 dspiljar Exp $ */
 | 
			
		||||
 | 
			
		||||
#ifndef MH_H
 | 
			
		||||
#define MH_H 1
 | 
			
		||||
 | 
			
		||||
#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 */
 | 
			
		||||
 | 
			
		||||
#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);
 | 
			
		||||
 | 
			
		||||
#endif /* MH_H */
 | 
			
		||||
							
								
								
									
										137
									
								
								src/misc.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										137
									
								
								src/misc.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,137 @@
 | 
			
		||||
/* -*- C -*- 
 | 
			
		||||
  mboxgrep - scan mailbox for messages matching a regular expression
 | 
			
		||||
  Copyright (C) 2000, 2001, 2002, 2003  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
 | 
			
		||||
  the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
  (at your option) any later version.
 | 
			
		||||
 | 
			
		||||
  Mboxgrep is distributed in the hope that it will be useful, but 
 | 
			
		||||
  WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
  GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
  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: misc.c,v 1.13 2003/04/06 21:01:49 dspiljar Exp $ */
 | 
			
		||||
 | 
			
		||||
#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>
 | 
			
		||||
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
 | 
			
		||||
#include "mboxgrep.h"
 | 
			
		||||
#include "wrap.h"
 | 
			
		||||
 | 
			
		||||
format_t
 | 
			
		||||
folder_format (const char *name)
 | 
			
		||||
     /* {{{  */
 | 
			
		||||
{
 | 
			
		||||
  format_t f;
 | 
			
		||||
 | 
			
		||||
  if (0 == strncasecmp (name, "mbox", 4))
 | 
			
		||||
    f = MBOX;
 | 
			
		||||
  else if (0 == strncasecmp (name, "zmbox", 5))
 | 
			
		||||
    f = ZMBOX;
 | 
			
		||||
  else if (0 == strncasecmp (name, "gzmbox", 6))
 | 
			
		||||
    f = ZMBOX;
 | 
			
		||||
  else if (0 == strncasecmp (name, "bzmbox", 5))
 | 
			
		||||
    f = BZ2MBOX;
 | 
			
		||||
  else if (0 == strncasecmp (name, "bz2mbox", 5))
 | 
			
		||||
    f = BZ2MBOX;
 | 
			
		||||
  else if (0 == strncasecmp (name, "mh", 2))
 | 
			
		||||
    f = MH;
 | 
			
		||||
  else if (0 == strncasecmp (name, "nnml", 4))
 | 
			
		||||
    f = NNML;
 | 
			
		||||
  else if (0 == strncasecmp (name, "nnmh", 4))
 | 
			
		||||
    f = NNMH;
 | 
			
		||||
  else if (0 == strncasecmp (name, "maildir", 7))
 | 
			
		||||
    f = MAILDIR;
 | 
			
		||||
  else
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	fprintf (stderr, "mboxgrep: %s: unknown folder type\n", name);
 | 
			
		||||
      exit (2);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  return f;
 | 
			
		||||
}
 | 
			
		||||
/* }}} */
 | 
			
		||||
 | 
			
		||||
lockmethod_t lock_method (const char *name)
 | 
			
		||||
     /* {{{  */
 | 
			
		||||
{
 | 
			
		||||
  lockmethod_t l;
 | 
			
		||||
 | 
			
		||||
  if (0 == strncasecmp (name, "none", 4))
 | 
			
		||||
    l = NONE;
 | 
			
		||||
  else if (0 == strncasecmp (name, "off", 3))
 | 
			
		||||
    l = NONE;
 | 
			
		||||
#ifdef HAVE_FCNTL
 | 
			
		||||
  else if (0 == strncasecmp (name, "fcntl", 5))
 | 
			
		||||
    l = FCNTL;
 | 
			
		||||
#endif /* HAVE_FCNTL */
 | 
			
		||||
#ifdef HAVE_FLOCK
 | 
			
		||||
  else if (0 == strncasecmp (name, "flock", 5))
 | 
			
		||||
    l = FLOCK;
 | 
			
		||||
#endif /* HAVE_FLOCK */
 | 
			
		||||
  else
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	fprintf (stderr, "mboxgrep: %s: unknown file locking method\n", name);
 | 
			
		||||
      exit (2);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  return l;
 | 
			
		||||
}
 | 
			
		||||
/* }}} */
 | 
			
		||||
 | 
			
		||||
/* time_t parse_date(char *datestr) */
 | 
			
		||||
     /* {{{  */
 | 
			
		||||
/* { */
 | 
			
		||||
/*   time_t t; */
 | 
			
		||||
/*   const char *fmt = "%d%n%b%n%Y%n%T"; */
 | 
			
		||||
/*   int h, m; */
 | 
			
		||||
/*   struct tm tm; */
 | 
			
		||||
/*   char *str2, str1[BUFSIZ]; */
 | 
			
		||||
 | 
			
		||||
/*   sscanf (datestr, "Date: %[^\r\n]", str1); */
 | 
			
		||||
 | 
			
		||||
/*   str2 = (char *) strptime (str1, "%d%n%b%n%Y%n%T", &tm); */
 | 
			
		||||
/*   if (str2 == NULL) */
 | 
			
		||||
/*     str2 = (char *) strptime (str1, "%a, %d%n%b%n%Y%n%T", &tm); */
 | 
			
		||||
/*   if (str2 == NULL) */
 | 
			
		||||
/*     return (time_t) 0; */
 | 
			
		||||
 
 | 
			
		||||
/*   if (sscanf (str2, "%3d%2d", &h, &m) == 2) */
 | 
			
		||||
/*     { */
 | 
			
		||||
/*       tm.tm_hour -= h; */
 | 
			
		||||
/*       tm.tm_min -= (h >= 0 ? m : -m); */
 | 
			
		||||
/*       t = (time_t) mktime (&tm); */
 | 
			
		||||
/*     } */
 | 
			
		||||
 | 
			
		||||
/*   return t; */
 | 
			
		||||
/* } */
 | 
			
		||||
/* }}} */
 | 
			
		||||
 | 
			
		||||
char * parse_return_path(char *rpath)
 | 
			
		||||
     /* {{{  */
 | 
			
		||||
{
 | 
			
		||||
  char *blah1, blah2[BUFSIZ];
 | 
			
		||||
 | 
			
		||||
  sscanf(rpath, "Return-Path: <%[^\r\n>]>", blah2);
 | 
			
		||||
  blah1 = xstrdup (blah2);
 | 
			
		||||
 | 
			
		||||
  return blah1;
 | 
			
		||||
}
 | 
			
		||||
/* }}} */
 | 
			
		||||
							
								
								
									
										32
									
								
								src/misc.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/misc.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
/* -*- C -*- 
 | 
			
		||||
  mboxgrep - scan mailbox for messages matching a regular expression
 | 
			
		||||
  Copyright (C) 2000, 2001, 2002, 2003  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
 | 
			
		||||
  the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
  (at your option) any later version.
 | 
			
		||||
 | 
			
		||||
  Mboxgrep is distributed in the hope that it will be useful, but 
 | 
			
		||||
  WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
  GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
  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: misc.h,v 1.5 2003/03/30 23:07:10 dspiljar Exp $ */
 | 
			
		||||
 | 
			
		||||
#ifndef MISC_H
 | 
			
		||||
#define MISC_H 1
 | 
			
		||||
 | 
			
		||||
#include "mboxgrep.h"
 | 
			
		||||
#include <time.h>
 | 
			
		||||
 | 
			
		||||
format_t folder_format (const char *name);
 | 
			
		||||
lockmethod_t lock_method (const char *name);
 | 
			
		||||
time_t parse_date(char *datestr);
 | 
			
		||||
char * parse_return_path(char *rpath);
 | 
			
		||||
 | 
			
		||||
#endif /* MISC_H */
 | 
			
		||||
							
								
								
									
										349
									
								
								src/scan.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										349
									
								
								src/scan.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,349 @@
 | 
			
		||||
/* -*- C -*- 
 | 
			
		||||
   mboxgrep - scan mailbox for messages matching a regular expression
 | 
			
		||||
   Copyright (C) 2000, 2001, 2002, 2003  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
 | 
			
		||||
   the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
   (at your option) any later version.
 | 
			
		||||
   
 | 
			
		||||
   Mboxgrep is distributed in the hope that it will be useful, but 
 | 
			
		||||
   WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
   GNU General Public License for more details.
 | 
			
		||||
   
 | 
			
		||||
   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: scan.c,v 1.21 2003/04/06 21:01:49 dspiljar Exp $ */
 | 
			
		||||
 | 
			
		||||
#include <config.h>
 | 
			
		||||
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <regex.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <sys/types.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 <time.h>
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#ifdef HAVE_LIBZ
 | 
			
		||||
# include <zlib.h>
 | 
			
		||||
#define BUFLEN 16384
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
#ifdef HAVE_LIBPCRE
 | 
			
		||||
# include <pcre.h>
 | 
			
		||||
#endif /* HAVE_LIBPCRE */
 | 
			
		||||
 | 
			
		||||
#include "scan.h"
 | 
			
		||||
#include "mbox.h"
 | 
			
		||||
#include "mh.h"
 | 
			
		||||
#include "maildir.h"
 | 
			
		||||
#include "wrap.h"
 | 
			
		||||
#include "md5.h"
 | 
			
		||||
#ifdef HAVE_FTS_OPEN
 | 
			
		||||
# include <sys/stat.h>
 | 
			
		||||
# include <fts.h>
 | 
			
		||||
#else
 | 
			
		||||
# ifdef HAVE_FTW
 | 
			
		||||
#  include <ftw.h>
 | 
			
		||||
# endif /* HAVE_FTW */
 | 
			
		||||
#endif /* HAVE_FTS_OPEN */
 | 
			
		||||
 | 
			
		||||
#ifdef HAVE_LIBDMALLOC
 | 
			
		||||
#include <dmalloc.h>
 | 
			
		||||
#endif /* HAVE_LIBDMALLOC */
 | 
			
		||||
 | 
			
		||||
void scan_mailbox (char path[])
 | 
			
		||||
     /* {{{  */
 | 
			
		||||
{
 | 
			
		||||
  static FILE *outf;
 | 
			
		||||
  extern FILE *tmpp;
 | 
			
		||||
  static mbox_t *mbox, *out;
 | 
			
		||||
#ifdef HAVE_LIBPCRE
 | 
			
		||||
  extern pcre *pcre_pattern;
 | 
			
		||||
  extern pcre_extra *hints;
 | 
			
		||||
  int of[BUFSIZ];
 | 
			
		||||
#endif /* HAVE_LIBPCRE */
 | 
			
		||||
  static DIR *boxd, *foo;
 | 
			
		||||
  static maildir_t *maildird;
 | 
			
		||||
  static message_t *msg;
 | 
			
		||||
  extern regex_t posix_pattern;
 | 
			
		||||
  extern char *pipecmd, *outboxname;
 | 
			
		||||
  extern int count;
 | 
			
		||||
  int delete = 0;
 | 
			
		||||
  char date_str[80];
 | 
			
		||||
  int isdup = 0;
 | 
			
		||||
  time_t tt;
 | 
			
		||||
  struct tm *ct;
 | 
			
		||||
  extern checksum_t *cs;
 | 
			
		||||
 | 
			
		||||
  extern option_t config;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  if (config.format == MAILDIR && config.action == WRITE)
 | 
			
		||||
    {
 | 
			
		||||
      foo = opendir (outboxname); /* do NOT change this to m_opendir! */
 | 
			
		||||
      if (foo == NULL && errno == ENOENT)
 | 
			
		||||
	maildir_create (outboxname);
 | 
			
		||||
      else closedir (foo);
 | 
			
		||||
 | 
			
		||||
      if (-1 == maildir_check (outboxname))
 | 
			
		||||
	{
 | 
			
		||||
	  if (config.merr)
 | 
			
		||||
	    fprintf (stderr, "%s: %s: Not a maildir folder\n", APPNAME, 
 | 
			
		||||
		     outboxname);
 | 
			
		||||
	  exit (2);
 | 
			
		||||
	}
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  count = 0;
 | 
			
		||||
  if (config.action == DELETE)
 | 
			
		||||
    delete = 1;
 | 
			
		||||
 | 
			
		||||
  if ((config.format == MBOX) || (config.format == ZMBOX) ||
 | 
			
		||||
      (config.format == BZ2MBOX))
 | 
			
		||||
    {
 | 
			
		||||
      mbox = (mbox_t *) mbox_open (path, "r");
 | 
			
		||||
      if (mbox == NULL) return;
 | 
			
		||||
    }
 | 
			
		||||
  else if ((config.format == MH) || (config.format == NNMH) ||
 | 
			
		||||
	   (config.format == NNML))
 | 
			
		||||
    {
 | 
			
		||||
      boxd = mh_open (path);
 | 
			
		||||
      if (boxd == NULL) return;
 | 
			
		||||
    }
 | 
			
		||||
  else if (config.format == MAILDIR)
 | 
			
		||||
    {
 | 
			
		||||
      maildird = maildir_open (path);
 | 
			
		||||
      if (maildird == NULL) return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  for (;;)
 | 
			
		||||
    {
 | 
			
		||||
      int res1 = 1, res2 = 1;
 | 
			
		||||
 | 
			
		||||
      if ((config.format == MBOX) || (config.format == ZMBOX) ||
 | 
			
		||||
	  (config.format == BZ2MBOX))
 | 
			
		||||
	msg = (message_t *) mbox_read_message (mbox);
 | 
			
		||||
      else if ((config.format == MH) || (config.format == NNMH) ||
 | 
			
		||||
	       (config.format == NNML))
 | 
			
		||||
	msg = (message_t *) mh_read_message (boxd);
 | 
			
		||||
      else if (config.format == MAILDIR)
 | 
			
		||||
	msg = (message_t *) maildir_read_message (maildird);
 | 
			
		||||
 | 
			
		||||
      if (msg == NULL) break;
 | 
			
		||||
 | 
			
		||||
      if (msg->from == NULL) msg->from = (char *) xstrdup ("nobody");
 | 
			
		||||
 | 
			
		||||
#ifdef HAVE_LIBPCRE
 | 
			
		||||
      if (config.perl)
 | 
			
		||||
	{
 | 
			
		||||
	  if (config.headers)
 | 
			
		||||
	    res1 = pcre_exec (pcre_pattern, hints, msg->headers,
 | 
			
		||||
			      (int) strlen (msg->headers), 0, 0, of, BUFSIZ);
 | 
			
		||||
	  if (config.body)
 | 
			
		||||
	    res2 = pcre_exec (pcre_pattern, hints, msg->body,
 | 
			
		||||
			      (int) strlen (msg->body), 0, 0, of, BUFSIZ);
 | 
			
		||||
 | 
			
		||||
	  res1 = res1 ^ 1;
 | 
			
		||||
	  res2 = res2 ^ 1;
 | 
			
		||||
	}
 | 
			
		||||
      else
 | 
			
		||||
#endif /* HAVE_LIBPCRE */
 | 
			
		||||
	{
 | 
			
		||||
	  if (config.headers)
 | 
			
		||||
	    res1 = regexec (&posix_pattern, msg->headers, 0, NULL, 0);
 | 
			
		||||
	  if (config.body)
 | 
			
		||||
	    res2 = regexec (&posix_pattern, msg->body, 0, NULL, 0);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
      if (config.dedup)
 | 
			
		||||
	isdup = md5_check_message (msg->body, cs);
 | 
			
		||||
 | 
			
		||||
      if (((res1 == 0) | (res2 == 0)) ^ ((config.invert ^ delete)) &&
 | 
			
		||||
	  ((config.dedup && !isdup) || !config.dedup))
 | 
			
		||||
	{
 | 
			
		||||
	  if (config.action == DISPLAY)
 | 
			
		||||
	    {
 | 
			
		||||
	      if (config.format != MBOX && config.format != ZMBOX
 | 
			
		||||
		  && config.format != BZ2MBOX
 | 
			
		||||
		  && 0 != strncmp ("From ", msg->headers, 5))
 | 
			
		||||
		{
 | 
			
		||||
		  tt = time (NULL);
 | 
			
		||||
		  ct = localtime (&tt);
 | 
			
		||||
		  strftime (date_str, 80, "%a %b %d %H:%M:%S %Y", ct);
 | 
			
		||||
		  if (msg->from)
 | 
			
		||||
		    fprintf (stdout, "From %s  %s\n", msg->from, date_str);
 | 
			
		||||
		  else
 | 
			
		||||
		    fprintf (stdout, "From nobody  %s\n", date_str);
 | 
			
		||||
		}
 | 
			
		||||
	      fprintf (stdout, "%s\n%s", msg->headers, msg->body);
 | 
			
		||||
	    }
 | 
			
		||||
	  else if (config.action == WRITE)
 | 
			
		||||
	    {
 | 
			
		||||
	      if (config.format == MAILDIR)
 | 
			
		||||
		maildir_write_message (msg, outboxname);
 | 
			
		||||
	      else if (config.format == MH || config.format == NNMH ||
 | 
			
		||||
		       config.format == NNML)
 | 
			
		||||
		mh_write_message (msg, outboxname);
 | 
			
		||||
	      else if (config.format == MBOX)
 | 
			
		||||
		{
 | 
			
		||||
		  out = mbox_open (outboxname, "w");
 | 
			
		||||
		  fprintf (out->fp, "%s\n%s", msg->headers, msg->body);
 | 
			
		||||
		  mbox_close (out);
 | 
			
		||||
		}
 | 
			
		||||
	    }
 | 
			
		||||
	  else if (config.action == PIPE)
 | 
			
		||||
	    {
 | 
			
		||||
	      outf = popen (pipecmd, "w");
 | 
			
		||||
	      if (outf == NULL)
 | 
			
		||||
		{
 | 
			
		||||
		  if (config.merr)
 | 
			
		||||
		    {
 | 
			
		||||
		      fprintf (stderr, "%s: %s: ", APPNAME, pipecmd);
 | 
			
		||||
		      perror (NULL);
 | 
			
		||||
		    }
 | 
			
		||||
		  exit (2);
 | 
			
		||||
		} /* if */
 | 
			
		||||
	      fprintf (outf, "%s\n%s", msg->headers, msg->body);
 | 
			
		||||
	      pclose (outf);
 | 
			
		||||
	    }
 | 
			
		||||
	  else if (config.action == COUNT)
 | 
			
		||||
	    ++count;
 | 
			
		||||
	  else if (config.action == DELETE && config.format == MBOX)
 | 
			
		||||
	    fprintf (tmpp, "%s\n%s", msg->headers, msg->body);
 | 
			
		||||
#ifdef HAVE_LIBZ
 | 
			
		||||
	  else if (config.action == DELETE && config.format == ZMBOX)
 | 
			
		||||
	    {
 | 
			
		||||
	      int quux, len, baz;
 | 
			
		||||
 | 
			
		||||
	      quux = 0;
 | 
			
		||||
	      baz = strlen (msg->headers);
 | 
			
		||||
	      for (;;)
 | 
			
		||||
		{
 | 
			
		||||
		  len = gzwrite (tmpp, (msg->headers+quux), 
 | 
			
		||||
			 	 (((quux + BUFLEN) < baz) ? BUFLEN : 
 | 
			
		||||
				  (baz - quux)));
 | 
			
		||||
		  quux += len;
 | 
			
		||||
		  if (quux == baz)
 | 
			
		||||
		    break;
 | 
			
		||||
		}
 | 
			
		||||
	      gzwrite(tmpp, "\n", 1);
 | 
			
		||||
	      quux = 0;
 | 
			
		||||
	      baz = strlen(msg->body);
 | 
			
		||||
	      for (;;)
 | 
			
		||||
		{
 | 
			
		||||
		  len = gzwrite(tmpp, (msg->body+quux), 
 | 
			
		||||
				(((quux + BUFLEN) < baz) ? BUFLEN : 
 | 
			
		||||
				 (baz - quux)));
 | 
			
		||||
		  quux += len;
 | 
			
		||||
		  if (quux == baz)
 | 
			
		||||
		    break;
 | 
			
		||||
		}
 | 
			
		||||
	    }
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
	} /* if */
 | 
			
		||||
      else if (((((res1 == 0) | (res2 == 0)) ^ config.invert) && delete) &&
 | 
			
		||||
	       ((config.format == MH) || (config.format == NNMH) || 
 | 
			
		||||
		(config.format == NNML) || (config.format == MAILDIR)))
 | 
			
		||||
	m_unlink(msg->filename);
 | 
			
		||||
 | 
			
		||||
      free(msg->body);
 | 
			
		||||
      free(msg->headers);
 | 
			
		||||
      free(msg);
 | 
			
		||||
    } /* for */
 | 
			
		||||
  if ((config.format == MBOX) || (config.format == ZMBOX) ||
 | 
			
		||||
      (config.format == BZ2MBOX))
 | 
			
		||||
    mbox_close (mbox);
 | 
			
		||||
  else if ((config.format == MH) || (config.format == NNMH) ||
 | 
			
		||||
	   (config.format == NNML))
 | 
			
		||||
    mh_close(boxd);
 | 
			
		||||
}
 | 
			
		||||
/* }}} */
 | 
			
		||||
 | 
			
		||||
void recursive_scan (char path[])
 | 
			
		||||
     /* {{{  */
 | 
			
		||||
 | 
			
		||||
{
 | 
			
		||||
#ifdef HAVE_FTS_OPEN
 | 
			
		||||
  FTS *ftsfoo;
 | 
			
		||||
  FTSENT *ftsbar;
 | 
			
		||||
#endif /* HAVE_FTS_OPEN */
 | 
			
		||||
 | 
			
		||||
#ifdef HAVE_FTS_OPEN
 | 
			
		||||
  {
 | 
			
		||||
    char *p[2];
 | 
			
		||||
 | 
			
		||||
    p[0] = strdup (path);
 | 
			
		||||
    p[1] = 0;
 | 
			
		||||
 | 
			
		||||
    ftsfoo = fts_open (p, FTS_NOCHDIR, NULL);
 | 
			
		||||
 | 
			
		||||
    if (ftsfoo == NULL)
 | 
			
		||||
      {
 | 
			
		||||
	/* fixme (?) */
 | 
			
		||||
	perror(APPNAME);
 | 
			
		||||
	exit (2);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
    while ((ftsbar = fts_read (ftsfoo)))
 | 
			
		||||
      scan_mailbox (ftsbar->fts_path);
 | 
			
		||||
 | 
			
		||||
    fts_close (ftsfoo);
 | 
			
		||||
  }
 | 
			
		||||
#else
 | 
			
		||||
  ftw (path, (void *) scan_mailbox, 1);
 | 
			
		||||
#endif /* HAVE_FTS_OPEN */
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* }}} */
 | 
			
		||||
 | 
			
		||||
int md5_check_message (char *body, checksum_t *chksum)
 | 
			
		||||
     /* {{{  */
 | 
			
		||||
{
 | 
			
		||||
  struct md5_ctx a;
 | 
			
		||||
  unsigned char b[16];
 | 
			
		||||
  int i;
 | 
			
		||||
 | 
			
		||||
  md5_init_ctx (&a);
 | 
			
		||||
  if (body == NULL)
 | 
			
		||||
    md5_process_bytes ("", 0, &a);
 | 
			
		||||
  else
 | 
			
		||||
    md5_process_bytes (body, strlen(body), &a);
 | 
			
		||||
  md5_finish_ctx(&a, b);
 | 
			
		||||
 | 
			
		||||
  for (i = 0; i < chksum->n; i++)
 | 
			
		||||
    {
 | 
			
		||||
      if (0 == strncmp (chksum->md5[i], b, 16)) 
 | 
			
		||||
	return 1; 
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  chksum->md5 = 
 | 
			
		||||
	(char **) xrealloc (chksum->md5, (1 + chksum->n) * sizeof (char *));
 | 
			
		||||
  chksum->md5[chksum->n] = xstrdup (b);
 | 
			
		||||
 | 
			
		||||
  (chksum->n)++;
 | 
			
		||||
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
/* }}} */
 | 
			
		||||
							
								
								
									
										30
									
								
								src/scan.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/scan.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
/* -*- C -*- 
 | 
			
		||||
  mboxgrep - scan mailbox for messages matching a regular expression
 | 
			
		||||
  Copyright (C) 2000, 2001, 2002, 2003  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
 | 
			
		||||
  the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
  (at your option) any later version.
 | 
			
		||||
 | 
			
		||||
  Mboxgrep is distributed in the hope that it will be useful, but 
 | 
			
		||||
  WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
  GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
  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: scan.h,v 1.7 2003/03/30 23:07:10 dspiljar Exp $ */
 | 
			
		||||
 | 
			
		||||
#ifndef SCAN_H
 | 
			
		||||
#define SCAN_H 1
 | 
			
		||||
 | 
			
		||||
#include "mboxgrep.h"
 | 
			
		||||
 | 
			
		||||
void scan_mailbox (char path[]);
 | 
			
		||||
void recursive_scan (char path[]);
 | 
			
		||||
int md5_check_message (char *body, checksum_t *chksum);
 | 
			
		||||
 | 
			
		||||
#endif /* SCAN_H */
 | 
			
		||||
							
								
								
									
										216
									
								
								src/wrap.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										216
									
								
								src/wrap.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,216 @@
 | 
			
		||||
/* -*- C -*- 
 | 
			
		||||
  mboxgrep - scan mailbox for messages matching a regular expression
 | 
			
		||||
  Copyright (C) 2000, 2001, 2002, 2003  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
 | 
			
		||||
  the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
  (at your option) any later version.
 | 
			
		||||
 | 
			
		||||
  Mboxgrep is distributed in the hope that it will be useful, but 
 | 
			
		||||
  WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
  GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
  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: wrap.c,v 1.12 2003/04/06 23:24:04 dspiljar Exp $ */
 | 
			
		||||
 | 
			
		||||
#include <config.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <errno.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_LIBZ
 | 
			
		||||
#include <zlib.h>
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/stat.h>
 | 
			
		||||
#include <fcntl.h>
 | 
			
		||||
 | 
			
		||||
#include "wrap.h"
 | 
			
		||||
#include "mboxgrep.h"
 | 
			
		||||
 | 
			
		||||
#ifndef APPNAME
 | 
			
		||||
#define APPNAME "mboxgrep"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
int m_open (const char *pathname, int flags, mode_t mode)
 | 
			
		||||
{
 | 
			
		||||
  int blah;
 | 
			
		||||
 | 
			
		||||
  blah = open (pathname, flags, mode);
 | 
			
		||||
  if (blah == -1 && config.merr)
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	{
 | 
			
		||||
	  fprintf (stderr, "%s: %s: ", APPNAME, pathname);
 | 
			
		||||
	  perror (NULL);
 | 
			
		||||
	}
 | 
			
		||||
/* failure to open a file for writing should be fatal */
 | 
			
		||||
      if (((flags & O_WRONLY) == O_WRONLY) || ((flags & O_RDWR) == O_RDWR))
 | 
			
		||||
	exit (2);
 | 
			
		||||
    }
 | 
			
		||||
  return blah;
 | 
			
		||||
}      
 | 
			
		||||
 | 
			
		||||
FILE *m_fopen (const char *path, const char *mode)
 | 
			
		||||
{
 | 
			
		||||
  FILE *blah;
 | 
			
		||||
 | 
			
		||||
  blah = fopen (path, mode);
 | 
			
		||||
  if (blah == NULL)
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	{
 | 
			
		||||
	  fprintf (stderr, "%s: %s: ", APPNAME, path);
 | 
			
		||||
	  perror (NULL);
 | 
			
		||||
	}
 | 
			
		||||
      if (mode[0] == 'w')
 | 
			
		||||
	exit (2);
 | 
			
		||||
    }
 | 
			
		||||
  return blah;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
FILE *m_fdopen (int fildes, const char *mode)
 | 
			
		||||
{
 | 
			
		||||
  FILE *blah;
 | 
			
		||||
 | 
			
		||||
  blah = fdopen (fildes, mode);
 | 
			
		||||
  if (blah == NULL)
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	perror (APPNAME);
 | 
			
		||||
    }
 | 
			
		||||
  return blah;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef HAVE_LIBZ
 | 
			
		||||
 | 
			
		||||
gzFile *m_gzdopen (int fildes, const char *mode)
 | 
			
		||||
{
 | 
			
		||||
  gzFile *blah;
 | 
			
		||||
 | 
			
		||||
  blah = gzdopen (fildes, mode);
 | 
			
		||||
  if (blah == NULL)
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	perror (APPNAME);
 | 
			
		||||
    }
 | 
			
		||||
  return blah;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
 | 
			
		||||
DIR *m_opendir (const char *name)
 | 
			
		||||
{
 | 
			
		||||
  DIR *blah;
 | 
			
		||||
 | 
			
		||||
  blah = opendir (name);
 | 
			
		||||
  if (blah == NULL)
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	{
 | 
			
		||||
	  fprintf (stderr, "%s: %s: ", APPNAME, name);
 | 
			
		||||
	  perror (NULL);
 | 
			
		||||
	}
 | 
			
		||||
    }
 | 
			
		||||
  return blah;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifndef HAVE_LIBDMALLOC
 | 
			
		||||
 | 
			
		||||
void *xmalloc (size_t size)
 | 
			
		||||
{
 | 
			
		||||
  void *foo;
 | 
			
		||||
 | 
			
		||||
  foo = malloc (size);
 | 
			
		||||
  if (foo == NULL)
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	perror (APPNAME);
 | 
			
		||||
      exit (2);
 | 
			
		||||
    }
 | 
			
		||||
  return foo;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void *xrealloc (void *ptr, size_t size)
 | 
			
		||||
{
 | 
			
		||||
  void *foo;
 | 
			
		||||
 | 
			
		||||
  foo = realloc (ptr, size);
 | 
			
		||||
  if (foo == NULL)
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	perror (APPNAME);
 | 
			
		||||
      exit (2);
 | 
			
		||||
    }
 | 
			
		||||
  return foo;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void *xcalloc (size_t nmemb, size_t size)
 | 
			
		||||
{
 | 
			
		||||
  void *foo;
 | 
			
		||||
 | 
			
		||||
  foo = calloc (nmemb, size);
 | 
			
		||||
  if (foo == NULL)
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	perror (APPNAME);
 | 
			
		||||
      exit (2);
 | 
			
		||||
    }
 | 
			
		||||
  return foo;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char *xstrdup (const char *s)
 | 
			
		||||
{
 | 
			
		||||
  char *foo;
 | 
			
		||||
 | 
			
		||||
  foo = strdup (s);
 | 
			
		||||
  if (foo == NULL)
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	perror (APPNAME);
 | 
			
		||||
      exit (2);
 | 
			
		||||
    }
 | 
			
		||||
  return foo;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif /* HAVE_LIBDMALLOC */
 | 
			
		||||
 | 
			
		||||
int m_unlink (const char *pathname)
 | 
			
		||||
{
 | 
			
		||||
  int baz;
 | 
			
		||||
 | 
			
		||||
  baz = unlink (pathname);
 | 
			
		||||
  if (baz == -1)
 | 
			
		||||
    {
 | 
			
		||||
      if (config.merr)
 | 
			
		||||
	{
 | 
			
		||||
	  fprintf (stderr, "%s: %s: ", APPNAME, pathname);
 | 
			
		||||
	  perror (NULL);
 | 
			
		||||
	}
 | 
			
		||||
    }
 | 
			
		||||
  return baz;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										74
									
								
								src/wrap.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								src/wrap.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,74 @@
 | 
			
		||||
/* -*- C -*- 
 | 
			
		||||
  mboxgrep - scan mailbox for messages matching a regular expression
 | 
			
		||||
  Copyright (C) 2000, 2001, 2002, 2003  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
 | 
			
		||||
  the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
  (at your option) any later version.
 | 
			
		||||
 | 
			
		||||
  Mboxgrep is distributed in the hope that it will be useful, but 
 | 
			
		||||
  WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
  GNU General Public License for more details.
 | 
			
		||||
 | 
			
		||||
  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: wrap.h,v 1.11 2003/04/06 23:24:04 dspiljar Exp $ */
 | 
			
		||||
 | 
			
		||||
/* This file is part of mboxgrep. */
 | 
			
		||||
 | 
			
		||||
/* wrappers around certain std functions */
 | 
			
		||||
 | 
			
		||||
#ifndef WRAP_H
 | 
			
		||||
#define WRAP_H
 | 
			
		||||
 | 
			
		||||
#include <config.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <sys/types.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_LIBZ
 | 
			
		||||
#include <zlib.h>
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
#ifdef HAVE_LIBDMALLOC
 | 
			
		||||
# include <dmalloc.h>
 | 
			
		||||
#endif /* HAVE_LIBDMALLOC */
 | 
			
		||||
 | 
			
		||||
int m_open (const char *pathname, int flags, mode_t mode);
 | 
			
		||||
 | 
			
		||||
FILE *m_fopen (const char *path, const char *mode);
 | 
			
		||||
FILE *m_fdopen (int fildes, const char *mode);
 | 
			
		||||
#ifdef HAVE_LIBZ
 | 
			
		||||
gzFile *m_gzdopen (int fildes, const char *mode);
 | 
			
		||||
#endif /* HAVE_LIBZ */
 | 
			
		||||
 | 
			
		||||
DIR *m_opendir (const char *name);
 | 
			
		||||
 | 
			
		||||
#ifndef HAVE_LIBDMALLOC
 | 
			
		||||
void *xmalloc (size_t size);
 | 
			
		||||
void *xrealloc (void *ptr, size_t size);
 | 
			
		||||
void *xcalloc (size_t nmemb, size_t size);
 | 
			
		||||
char *xstrdup (const char *s);
 | 
			
		||||
#endif /* HAVE_LIBDMALLOC */
 | 
			
		||||
 | 
			
		||||
int m_unlink (const char *pathname);
 | 
			
		||||
 | 
			
		||||
#endif /* WRAP_H */
 | 
			
		||||
		Reference in New Issue
	
	Block a user