Files
mboxgrep/src/wrap.c
T

275 lines
4.8 KiB
C
Raw Normal View History

/* -*- C -*-
mboxgrep - scan mailbox for messages matching a regular expression
2023-01-30 21:51:37 +01:00
Copyright (C) 2000 - 2003, 2023 Daniel Spiljar
Mboxgrep is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
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
2023-01-30 21:51:37 +01:00
*/
#include <config.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_DIRENT_H
2023-03-03 22:49:48 +01:00
# include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name)
#else
2023-03-03 22:49:48 +01:00
# 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
2023-03-03 22:49:48 +01:00
# include <zlib.h>
#endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2
2023-03-03 22:49:48 +01:00
# include <bzlib.h>
#endif /* HAVE_LIBBZ2 */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "wrap.h"
#include "mboxgrep.h"
#ifndef APPNAME
2023-03-03 22:49:48 +01:00
# define APPNAME "mboxgrep"
#endif
#define BUFLEN 16384
2023-03-03 22:49:48 +01:00
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)
2023-01-30 21:51:37 +01:00
{
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))
2023-01-30 21:51:37 +01:00
exit (2);
}
return blah;
2023-03-03 22:49:48 +01:00
}
2023-03-03 22:49:48 +01:00
FILE *
m_fopen (const char *path, const char *mode)
{
FILE *blah;
blah = fopen (path, mode);
if (blah == NULL)
{
if (config.merr)
2023-01-30 21:51:37 +01:00
{
fprintf (stderr, "%s: %s: ", APPNAME, path);
perror (NULL);
}
if (mode[0] == 'w')
2023-01-30 21:51:37 +01:00
exit (2);
}
return blah;
}
2023-03-03 22:49:48 +01:00
FILE *
m_fdopen (int fildes, const char *mode)
{
FILE *blah;
blah = fdopen (fildes, mode);
if (blah == NULL)
{
if (config.merr)
2023-01-30 21:51:37 +01:00
perror (APPNAME);
}
return blah;
}
#ifdef HAVE_LIBZ
2023-03-03 22:49:48 +01:00
gzFile
m_gzdopen (int fildes, const char *mode)
{
gzFile blah;
blah = gzdopen (fildes, mode);
if (blah == NULL)
{
if (config.merr)
2023-01-30 21:51:37 +01:00
perror (APPNAME);
}
return blah;
}
#endif /* HAVE_LIBZ */
2023-03-03 22:49:48 +01:00
DIR *
m_opendir (const char *name)
{
DIR *blah;
blah = opendir (name);
if (blah == NULL)
{
if (config.merr)
2023-01-30 21:51:37 +01:00
{
fprintf (stderr, "%s: %s: ", APPNAME, name);
perror (NULL);
}
}
return blah;
}
#ifndef HAVE_LIBDMALLOC
2023-03-03 22:49:48 +01:00
void *
xmalloc (size_t size)
{
void *foo;
foo = malloc (size);
if (foo == NULL)
{
if (config.merr)
2023-01-30 21:51:37 +01:00
perror (APPNAME);
exit (2);
}
return foo;
}
2023-03-03 22:49:48 +01:00
void *
xrealloc (void *ptr, size_t size)
{
void *foo;
foo = realloc (ptr, size);
if (foo == NULL)
{
if (config.merr)
2023-01-30 21:51:37 +01:00
perror (APPNAME);
exit (2);
}
return foo;
}
2023-03-03 22:49:48 +01:00
void *
xcalloc (size_t nmemb, size_t size)
{
void *foo;
foo = calloc (nmemb, size);
if (foo == NULL)
{
if (config.merr)
2023-01-30 21:51:37 +01:00
perror (APPNAME);
exit (2);
}
return foo;
}
2023-03-03 22:49:48 +01:00
char *
xstrdup (const char *s)
{
char *foo;
foo = strdup (s);
if (foo == NULL)
{
if (config.merr)
2023-01-30 21:51:37 +01:00
perror (APPNAME);
exit (2);
}
return foo;
}
#endif /* HAVE_LIBDMALLOC */
2023-03-03 22:49:48 +01:00
int
m_unlink (const char *pathname)
{
int baz;
baz = unlink (pathname);
if (baz == -1)
{
if (config.merr)
2023-01-30 21:51:37 +01:00
{
fprintf (stderr, "%s: %s: ", APPNAME, pathname);
perror (NULL);
}
}
return baz;
}
#ifdef HAVE_LIBZ
2023-03-03 22:49:48 +01:00
void
gzwrite_loop (void *fp, char *str)
{
int quux, len, baz;
quux = 0;
baz = strlen (str);
for (;;)
{
2023-03-03 22:49:48 +01:00
len = gzwrite (fp, (str + quux),
(((quux + BUFLEN) < baz) ? BUFLEN : (baz - quux)));
quux += len;
if (quux == baz)
2023-01-30 21:51:37 +01:00
break;
}
}
#endif /* HAVE_LIBZ */
#ifdef HAVE_LIBBZ2
2023-03-03 22:49:48 +01:00
void
bzwrite_loop (void *fp, char *str)
{
int quux, len, baz;
quux = 0;
baz = strlen (str);
for (;;)
{
2023-03-03 22:49:48 +01:00
len = BZ2_bzwrite (fp, (str + quux),
(((quux + BUFLEN) < baz) ? BUFLEN : (baz - quux)));
quux += len;
if (quux == baz)
2023-01-30 21:51:37 +01:00
break;
}
}
#endif /* HAVE_LIBBZ2 */