dnl   Copyright (C) 2002 - 2006 Tomasz Kojm <tkojm@clamav.net>
dnl   gethostbyname_r and readdir_r checks (c) COPYRIGHT MIT 1995
dnl   socklen_t check (c) Alexander V. Lukyanov <lav@yars.free.net>
dnl
dnl   This program is free software; you can redistribute it and/or modify
dnl   it under the terms of the GNU General Public License as published by
dnl   the Free Software Foundation; either version 2 of the License, or
dnl   (at your option) any later version.
dnl
dnl   This program is distributed in the hope that it will be useful,
dnl   but WITHOUT ANY WARRANTY; without even the implied warranty of
dnl   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dnl   GNU General Public License for more details.
dnl
dnl   You should have received a copy of the GNU General Public License
dnl   along with this program; if not, write to the Free Software
dnl   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
dnl   MA 02110-1301, USA.

AC_PREREQ([2.59])
dnl For a release change [devel] to the real version [0.xy]
dnl also change VERSION below
AC_INIT([clamav], [devel], [http://bugs.clamav.net/])

dnl put configure auxiliary into config
AC_CONFIG_AUX_DIR([config])
AC_CONFIG_HEADER([clamav-config.h])

dnl safety check, this used to be a parameter to AC_INIT
AC_CONFIG_SRCDIR([clamscan/clamscan.c])
AC_CONFIG_MACRO_DIR([m4])
AC_CREATE_TARGET_H([target.h])

dnl -Wall and -Werror here are NOT CFLAGS, they refer to automake warnings
AM_INIT_AUTOMAKE([-Wall -Wportability -Wno-override -Werror std-options foreign dist-bzip2 no-define])

dnl we told automake to not define these, since we want to include
dnl the date in the version
AC_DEFINE([PACKAGE], PACKAGE_NAME, [Name of package])

dnl change this on a release
VERSION="devel-`date +%Y%m%d`"
dnl VERSION="0.94rc1"
AC_DEFINE_UNQUOTED([VERSION],"$VERSION",[Version number of package])

LC_CURRENT=5
LC_REVISION=0
LC_AGE=0
LIBCLAMAV_VERSION="$LC_CURRENT":"$LC_REVISION":"$LC_AGE"
AC_SUBST([LIBCLAMAV_VERSION])

AC_PROG_AWK
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET

dnl libtool by default brings in check for all the languages/compilers (CXX/F77/...)
dnl we don't need them, so avoid tagged configuration
dnl libtool-1.6 should already do it right, but thats not stable yet
AC_DEFUN([_LT_AC_TAGCONFIG])
AC_PROG_LIBTOOL

AC_DEFINE([SCANBUFF], 131072, [scan buffer size])
AC_DEFINE([FILEBUFF], 8192,   [file i/o buffer size])

AC_HEADER_STDC
AC_C_RESTRICT
AC_C_CONST
AC_C_INLINE
AC_C_FPU_BIGENDIAN
AC_C_BIGENDIAN
if test $ac_cv_c_bigendian = yes; then
  AC_DEFINE([WORDS_BIGENDIAN],1,endianess)
else
  AC_DEFINE([WORDS_BIGENDIAN],0,endianess)
fi

AC_ARG_ENABLE([gcc-vcheck],
[  --disable-gcc-vcheck	  do not check for buggy gcc version ],
gcc_check=$enableval, gcc_check="yes")

msg_gcc_check="use --disable-gcc-vcheck to disable this check. Before reporting any bugs check with a supported version of gcc"

dnl Check for gcc-4.1.0
if test "$gcc_check" = "yes"; then
	if test "x$ac_compiler_gnu" = "xyes"; then
		AC_MSG_CHECKING([for a supported version of gcc])
		gcc_version=`${CC} -dumpversion`
		case "${gcc_version}" in
			4.1.0*)
				AC_MSG_RESULT([no (${gcc_version})])
				AC_MSG_ERROR([gcc 4.1.0 is known to incorrectly compile upx.c. Upgrade your compiler to at least 4.1.1/4.1.2)])
				;;
			*)
				AC_MSG_RESULT([ok (${gcc_version})])
				;;
	esac
	fi
fi

dnl Checks if compiler produces valid code, regardless of compiler
dnl we do these checks here to avoid receiving endless bugreports about
dnl breakages due to compiler bugs.

dnl Check if compiler produces invalid code on gcc PR27603 (affects upx.c)
dnl testcase from gcc testsuite
AC_MSG_CHECKING([for gcc bug PR27603])
AC_TRY_RUN(
	   [
/* (C) Richard Guenther */	   
void exit (int);
void abort (void);
int a;
int main(void)
{
  int j;
  for (j = 0; j < 6; j++)
  {
    if ((unsigned)j - 3 <= 1)
      exit (0);
    a = 1000 * (6 - j);
  }
  abort ();
}
], [AC_MSG_RESULT([ok, bug not present])],
[AC_MSG_ERROR([your compiler has gcc PR27603 bug, use a different compiler, see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27603])], [AC_MSG_RESULT([cross-compiling, assumed ok])])

dnl Check if compiler produces invalid code on gcc PR26763-2 (affects upx.c)
dnl testcase from gcc testsuite
AC_MSG_CHECKING([for gcc bug PR26763-2])
AC_TRY_RUN(
	   [
/* (C) Richard Guenther */	   
extern void abort(void);

static int try (char *a, int d)
{
  return a + d > a;
}

int main(void)
{
  char bla[100];

  if (try (bla + 50, -1))
    abort ();

  return 0;
}
], [AC_MSG_RESULT([ok, bug not present])],
[AC_MSG_ERROR([your compiler has gcc PR26763-2 bug, use a different compiler, see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26763])],[AC_MSG_RESULT([cross-compiling, assumed ok])])

dnl Check if compiler produces invalid code on own testcase based on upx.c
AC_MSG_CHECKING([for valid code generation of CLI_ISCONTAINED])
AC_TRY_RUN(
	   [
#include <stdio.h>
static struct v{
	char* dst;
	unsigned int dsize;
	unsigned int dcur;
	unsigned int backsize;
	signed int unp_offset;
} values[] = {
	{(char*)0xf78ab008, 0x2e000, 1, 4, -1594},
	{(char*)0xb7af1008, 0x2e000, 1, 4, -1594}

};
extern void abort(void);

#define CLI_ISCONTAINED(bb, bb_size, sb, sb_size)	\
    (bb_size > 0 && sb_size > 0 && sb_size <= bb_size	\
     && sb >= bb && sb + sb_size <= bb + bb_size && sb + sb_size > bb)

int crashtest()
{
	unsigned int backsize, dcur;
	int dval=0x12000, unp_offset;
	int* dsize = &dval;
	char* dst = (char*)0x12000;
	while(1) {
		backsize=4;
		dcur=0;
		unp_offset=0x800002c7;

		if (!CLI_ISCONTAINED(dst, *dsize, dst+dcur+unp_offset, backsize) || !CLI_ISCONTAINED(dst, *dsize, dst+dcur, backsize) || unp_offset >=0)
			return -1;
		abort();
	}
	return 0;
}

int main(void)
{
	int i;
	for(i=0;i<sizeof(values)/sizeof(values[0]);i++) {
		struct v* v= &values[i];
		char* dst = v->dst;
		unsigned int* dsize = &v->dsize;
		unsigned int dcur = v->dcur;
		unsigned int backsize = v->backsize-1;
		signed int  unp_offset = v->unp_offset;

		if(!CLI_ISCONTAINED(dst, *dsize, dst+dcur+unp_offset, backsize) ||
				!CLI_ISCONTAINED(dst, *dsize,dst+dcur,backsize) || unp_offset >= 0)  {
			continue;
		}
		abort();
	}
	crashtest();
	return 0;
}
], [AC_MSG_RESULT([ok, bug not present])],
[AC_MSG_ERROR([your compiler has a bug that causes clamav bug no. 670, use a different compiler, see http://bugs.clamav.net/bugzilla/show_bug.cgi?id=670])], [AC_MSG_RESULT([cross-compiling, assumed ok])])

dnl Check if compiler produces invalid code on gcc PR28045 (affects upx.c)
dnl testcase from gcc testsuite
AC_MSG_CHECKING([for gcc bug PR28045])
AC_TRY_RUN(
	   [
/* (C) Andrew Pinski */
extern void abort(void);
struct a
{
   unsigned int bits : 1;
   signed long val : ((sizeof(long) * 8) - 1);
};
static int Fnegate (struct a b)
{
  if ((-((long)b.val)) <= ((long) ((1UL << ((sizeof(long) * 8) - 2)) -1UL))
      && (-((long)b.val)) >= (-(((long) ((1UL << ((sizeof(long) * 8) - 2)) -1UL))) - 1))
     return 0 ;
  abort ();
}
int main (void)
{
  struct a b = {1, 1};
  Fnegate (b);
  return 0;
}
], [AC_MSG_RESULT([ok, bug not present])],
[AC_MSG_ERROR([your compiler has gcc PR28045 bug, use a different compiler, see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28045])], [AC_MSG_RESULT([cross-compiling, assumed ok])])

dnl Linker feature checks
dnl check for version script support in the linker (GNU ld, or Solaris ld style)
AC_CACHE_CHECK([for ld --version-script], [ac_cv_ld_version_script], [dnl
  cat > conftest.c <<EOF
void cl_symbol1(void) {}
void cli_symbol2(void) {}
EOF
  cat > conftest.map <<EOF
RELEASE
{
	global:
		cl_*;
	local:
		*;
};
PRIVATE
{
	global:
		cli_*;
	local:
		*;
};
EOF
  dnl check for GNU ld style linker version script
  if AC_TRY_COMMAND([${CC-cc} $CFLAGS $pic_flag $LDFLAGS -shared
				-o conftest.so conftest.c
				-Wl,--version-script,conftest.map
		       1>&AS_MESSAGE_LOG_FD]);
  then
      VERSIONSCRIPTFLAG=--version-script
      ac_cv_ld_version_script=yes
  else
	dnl check for Solaris ld style linker version script
	if AC_TRY_COMMAND([${CC-cc} $CFLAGS $pic_flag $LDFLAGS -shared
				-o conftest.so conftest.c
				-Wl,-M,conftest.map
			1>&AS_MESSAGE_LOG_FD]);
	then
		VERSIONSCRIPTFLAG=-M
		ac_cv_ld_version_script=yes;
	else
		ac_cv_ld_version_script=no
	fi
  fi
 rm -f conftest*])
AC_SUBST([VERSIONSCRIPTFLAG])
AM_CONDITIONAL([VERSIONSCRIPT], test "x$ac_cv_ld_version_script" = "xyes")

# it is not fatal if gperf is missing
AM_MISSING_PROG(GPERF, gperf)
AC_SUBST(GPERF)

AC_CHECK_HEADERS([stdint.h unistd.h sys/int_types.h dlfcn.h inttypes.h sys/inttypes.h memory.h ndir.h stdlib.h strings.h string.h sys/mman.h sys/param.h sys/stat.h sys/types.h malloc.h poll.h limits.h sys/filio.h sys/uio.h termios.h stdbool.h pwd.h grp.h])
AC_CHECK_HEADER([syslog.h],AC_DEFINE([USE_SYSLOG],1,[use syslog]),)

AC_TYPE_OFF_T
AC_COMPILE_CHECK_SIZEOF([short])
AC_COMPILE_CHECK_SIZEOF([int])
AC_COMPILE_CHECK_SIZEOF([long])
AC_COMPILE_CHECK_SIZEOF([long long])

AC_ARG_ENABLE([experimental],
[  --enable-experimental	enable experimental code],
enable_experimental=$enableval, enable_experimental="no")

if test "$enable_experimental" = "yes"; then
  AC_DEFINE([CL_EXPERIMENTAL],1,[enable experimental code])
fi

AC_CHECK_LIB([socket], [bind], [LIBS="$LIBS -lsocket"; CLAMAV_MILTER_LIBS="$CLAMAV_MILTER_LIBS -lsocket"; FRESHCLAM_LIBS="$FRESHCLAM_LIBS -lsocket"; CLAMD_LIBS="$CLAMD_LIBS -lsocket"])
AC_SEARCH_LIBS([gethostent],[nsl], [(LIBS="$LIBS -lnsl"; CLAMAV_MILTER_LIBS="$CLAMAV_MILTER_LIBS -lnsl"; FRESHCLAM_LIBS="$FRESHCLAM_LIBS -lnsl"; CLAMD_LIBS="$CLAMD_LIBS -lnsl")])

AC_CHECK_FUNCS([poll setsid memcpy snprintf vsnprintf strerror_r strlcpy strlcat strcasestr inet_ntop setgroups initgroups ctime_r mkstemp])
AC_FUNC_MMAP
AC_FUNC_FSEEKO

enable_check_ut=auto
enable_ut_install=no
AC_ARG_ENABLE(check,
[  --enable-check           Enable 'check' unit tests (default=auto)], enable_check_ut=$enableval, enable_check_ut="auto" )

if test "$enable_check_ut" != "no" ; then
	PKG_CHECK_MODULES([CHECK],[check],[], [AC_MSG_WARN([unable to find 'check' using pkg-config:
$CHECK_PKG_ERRORS
_PKG_TEXT])])
	save_CPPFLAGS="$CPPFLAGS";
	CPPFLAGS="$CPPFLAGS $CHECK_CFLAGS"
	save_LIBS="$LIBS"
	if test -z "$CHECK_LIBS"; then
		# when pkg-config is not available, use defaults		
		CHECK_LIBS="-lcheck"
	fi
	LIBS="$LIBS $CHECK_LIBS"
	AC_CHECK_HEADER(check.h,
			[
				AC_MSG_CHECKING([linking with check])
				AC_LINK_IFELSE([AC_LANG_CALL([],[srunner_create])],[
					AC_MSG_RESULT([ok])
					AC_DEFINE([HAVE_CHECK], 1, [Enable 'check' unit tests])
					AC_ARG_ENABLE(tests-install,
					      AC_HELP_STRING([--enable-tests-install],
					      [install unit tests
					       @<:@default=no@:>@]),
					       [enable_ut_install=$enableval],[enable_ut_install="no"])
					],
					[AC_MSG_RESULT([failed]);CHECK_LIBS=]
				)
			])
	LIBS="$save_LIBS"
	CPPFLAGS="$save_CPPFLAGS"
fi

if test "x$CHECK_LIBS" = "x" -a "$enable_check_ut" = "yes"; then
    AC_MSG_ERROR([
$CHECK_PKG_ERRORS

_PKG_TEXT

ERROR!  Check was configured, but not found.  Get it from http://check.sf.net/
])
fi

AC_ARG_ENABLE(coverage,
	      AC_HELP_STRING([--enable-coverage],
			     [turn on test coverage
			      @<:@default=no@:>@]),
[enable_cov=$enableval],[enable_cov="no"])

if test "x$enable_coverage" = "xyes"; then
	if test "x$CHECK_LIBS" = "x"; then
		AC_MSG_ERROR([Coverage testing required, but unit tests not enabled!])
	fi
	if test "x$ac_compiler_gnu" != "xyes"; then
		AC_MSG_ERROR([coverage testing only works if gcc is used])
	fi

	CFLAGS="$CFLAGS -fprofile-arcs -ftest-coverage"
	LDFLAGS="$LDFLAGS -lgcov"
	AC_CHECK_PROGS(GCOV, gcov, false)
	AC_CHECK_PROGS(LCOV, lcov, false)
	AC_CHECK_PROGS(GENHTML, genhtml, false)
fi

AM_CONDITIONAL(ENABLE_COVERAGE, test "x$enable_coverage" = "xyes")
AM_CONDITIONAL(ENABLE_UT_INSTALL, test "x$enable_ut_install" = "xyes")

have_pthreads=no
AC_CHECK_HEADER([pthread.h],[have_pthreads=yes],)

AM_MAINTAINER_MODE
dnl Check for zlib
AC_MSG_CHECKING([for zlib installation])
AC_ARG_WITH([zlib],
[  --with-zlib=DIR	  path to directory containing zlib library (default=
			  /usr/local or /usr if not found in /usr/local)],
[
if test "$withval"; then
  ZLIB_HOME="$withval"
  AC_MSG_RESULT([using $ZLIB_HOME])
fi
], [
ZLIB_HOME=/usr/local
if test ! -f "$ZLIB_HOME/include/zlib.h"
then
  ZLIB_HOME=/usr
fi
AC_MSG_RESULT([$ZLIB_HOME])
])

AC_ARG_ENABLE([zlib-vcheck],
[  --disable-zlib-vcheck	  do not check for buggy zlib version ],
zlib_check=$enableval, zlib_check="yes")

if test ! -f "$ZLIB_HOME/include/zlib.h"
then
    AC_MSG_ERROR([Please install zlib and zlib-devel packages])
else

    vuln=`grep "ZLIB_VERSION \"1.2.0" $ZLIB_HOME/include/zlib.h`
    if test -z "$vuln"; then
	vuln=`grep "ZLIB_VERSION \"1.2.1" $ZLIB_HOME/include/zlib.h`
    fi

    if test -n "$vuln"; then
	if test "$zlib_check" = "yes"; then
	    AC_MSG_ERROR(The installed zlib version may contain a security bug. Please upgrade to 1.2.2 or later: http://www.zlib.net. You can omit this check with --disable-zlib-vcheck but DO NOT REPORT any stability issues then!)
	else
	    AC_MSG_WARN([****** This ClamAV installation may be linked against])
	    AC_MSG_WARN([****** a broken zlib version. Please DO NOT report any])
	    AC_MSG_WARN([****** stability problems to the ClamAV developers!])
	fi
    fi

    if test "$ZLIB_HOME" != "/usr"; then
	CPPFLAGS="$CPPFLAGS -I$ZLIB_HOME/include"
	save_LDFLAGS="$LDFLAGS"
	LDFLAGS="$LIBS -L$ZLIB_HOME/lib"
	AC_CHECK_LIB([z], [inflateEnd], [LIBCLAMAV_LIBS="$LIBCLAMAV_LIBS -L$ZLIB_HOME/lib -lz";FRESHCLAM_LIBS="$FRESHCLAM_LIBS -L$ZLIB_HOME/lib -lz"; AC_DEFINE([HAVE_ZLIB_H],1,zlib installed)], AC_MSG_ERROR([Please install zlib and zlib-devel packages]))
	AC_CHECK_LIB([z], [gzopen], [], AC_MSG_ERROR([Your zlib is missing gzopen()]))
	LDFLAGS="$save_LDFLAGS"
    else
	AC_CHECK_LIB([z], [inflateEnd], [LIBCLAMAV_LIBS="$LIBCLAMAV_LIBS -lz";FRESHCLAM_LIBS="$FRESHCLAM_LIBS -lz"; AC_DEFINE([HAVE_ZLIB_H],1,zlib installed)], AC_MSG_ERROR([Please install zlib and zlib-devel packages]))
	AC_CHECK_LIB([z], [gzopen],[], AC_MSG_ERROR([Your zlib is missing gzopen()]))
    fi
fi

AC_ARG_ENABLE([bzip2],
[  --disable-bzip2	  disable bzip2 support],
want_bzip2=$enableval, want_bzip2="yes")

if test "$want_bzip2" = "yes"
then
    AC_LIB_LINKFLAGS([bz2])
    save_LDFLAGS="$LDFLAGS"
    LDFLAGS="$LIBS -L$LIBBZ2_PREFIX/lib";
    AC_CHECK_LIB([bz2], [bzReadOpen],
		 [AC_DEFINE([NOBZ2PREFIX],1,[bzip funtions do not have bz2 prefix])]
		)
    LDFLAGS="$save_LDFLAGS"
    if test "$HAVE_LIBBZ2" = "yes"; then
	AC_CHECK_HEADER([bzlib.h],
			[AC_C_CVE_2008_1372],
			[ac_cv_c_cve_2008_1372="no"])
	if test "$ac_cv_c_cve_2008_1372" = "bugged"; then
		AC_MSG_WARN([****** bzip2 libraries are affected by the CVE-2008-1372 bug])
		AC_MSG_WARN([****** We strongly suggest you to update to bzip2 1.0.5.])
		AC_MSG_WARN([****** Please do not report stability problems to the ClamAV developers!])
	fi

	case "$ac_cv_c_cve_2008_1372" in
	ok|bugged)
		LIBCLAMAV_LIBS="$LIBCLAMAV_LIBS $LTLIBBZ2"
		AC_DEFINE([HAVE_BZLIB_H],1,[have bzip2])
		;;
	*)
		AC_MSG_WARN([****** bzip2 support disabled])
		;;
	esac

    else
	AC_MSG_WARN([****** bzip2 support disabled])
    fi
fi

AC_ARG_ENABLE([unrar],
[  --disable-unrar	  don't build libclamunrar and libclamunrar_iface ],
want_unrar=$enableval, want_unrar="yes")

AC_ARG_ENABLE([ipv6],
[  --disable-ipv6          disable IPv6 support],
want_ipv6=$enableval, want_ipv6="yes")

if test "$want_ipv6" = "yes"
then
    AC_MSG_CHECKING([for getaddrinfo])
    AC_CACHE_VAL([have_gai],[
		AC_TRY_RUN([
		    #include <sys/types.h>
		    #include <sys/socket.h>
		    #include <netdb.h>
		    int main(int argc, char **argv)
		    {
			    struct addrinfo *res;

			if(getaddrinfo("127.0.0.1", NULL, NULL, &res) < 0)
			    return 1;
			freeaddrinfo(res);
			return 0;
		    }
		],
		[have_gai=yes],
		[have_gai=no])
		])
    AC_MSG_RESULT([$have_gai])
    if test "$have_gai" = yes; then
	AC_DEFINE(SUPPORT_IPv6, 1, [Use IPv6 aware code])
    fi
fi

AC_C_DNS

AC_ARG_ENABLE([clamuko],
[  --disable-clamuko	  disable clamuko support (Linux, DragonFly and FreeBSD only)],
want_clamuko=$enableval, want_clamuko="yes")

dnl AC_FUNC_SETPGRP does not work when cross compiling
dnl Instead, assume we will have a prototype for setpgrp if cross compiling.
dnl testcase from gdb/configure.ac
if test "$cross_compiling" = no; then
 AC_FUNC_SETPGRP
else
 AC_CACHE_CHECK([whether setpgrp takes no argument], [ac_cv_func_setpgrp_void],
   [AC_TRY_COMPILE([
#include <unistd.h>
], [
 if (setpgrp(1,1) == -1)
   exit (0);
 else
   exit (1);
], ac_cv_func_setpgrp_void=no, ac_cv_func_setpgrp_void=yes)])
if test $ac_cv_func_setpgrp_void = yes; then
 AC_DEFINE([SETPGRP_VOID], 1)
fi
fi

AC_ARG_ENABLE([milter],
[  --enable-milter	  build clamav-milter],
have_milter=$enableval, have_milter="no")

if test "$have_milter" = "yes"; then
  sendmailprog=no
  AC_ARG_WITH([sendmail],
  [  --with-sendmail=PATH    specify location of Sendmail binary (default=auto find)],
  sendmailprog=$with_sendmail, sendmailprog=no)

  if test "$sendmailprog" = "no" ; then
    AC_PATH_PROG([sendmailprog], sendmail, no, $PATH:/sbin:/usr/sbin:/usr/lib:/usr/libexec)
  fi

  AC_DEFINE_UNQUOTED([SENDMAIL_BIN], "$sendmailprog", [location of Sendmail binary])

  AC_ARG_WITH(sendmail-version,
  [  --with-sendmail-version=VERSION    specify version of Sendmail (default=auto find)],
  sendmailvers=$with_sendmail_version, sendmailvers=no)

  if test "$sendmailvers" = "no" ; then
    sendmailver=`$sendmailprog -d0 < /dev/null | head -1 | awk '{print $2}'`
  fi

  if test -n "$sendmailver"; then
    sendmailver_a=`echo $sendmailver | awk -F. '{printf $1}'`
    sendmailver_b=`echo $sendmailver | awk -F. '{printf $2}'`
    sendmailver_c=`echo $sendmailver | awk -F. '{printf $3}'`

    AC_DEFINE_UNQUOTED([SENDMAIL_VERSION_A], $sendmailver_a, [major version of Sendmail])
    AC_DEFINE_UNQUOTED([SENDMAIL_VERSION_B], $sendmailver_b, [minor version of Sendmail])
    AC_DEFINE_UNQUOTED([SENDMAIL_VERSION_C], $sendmailver_c, [subversion of Sendmail])
  fi
  if test "x$ac_cv_have_lresolv_r" != "xyes"; then
    AC_MSG_WARN([****** Your libresolv is not thread safe, SPF queries in clamav-milter will block])
  fi
fi

AC_ARG_ENABLE([dsig],
[  --disable-dsig	  disable support for digital signatures],
want_dsig=$enableval, want_dsig="yes")

if test "$want_dsig" = "yes"
then
    dnl defines HAVE_LIBGMP, LIBGMP, and LTLIBGMP
    dnl also adds option --with-libgmp-prefix to configure
    AC_LIB_HAVE_LINKFLAGS([gmp],,
	[#include <gmp.h>
	],
	[mpz_init(0);])
    dnl we don't need to check for __gmpz_init and mpz_init,
    dnl since we are not checking directly for symbols
    if test "$HAVE_LIBGMP" = "yes"; then
	    LIBCLAMAV_LIBS="$LIBCLAMAV_LIBS $LTLIBGMP";
    else
	    AC_MSG_WARN([****** GNU MP 2 or newer NOT FOUND - digital signature support will be disabled !]);
	    want_dsig="no";
    fi
fi

dnl we need to try to link with iconv, otherwise there could be a 
dnl mismatch between a 32-bit and 64-bit lib. Detect this at configure time.
dnl we need to check after zlib/bzip2, because they can change the include path
AC_ARG_WITH([iconv], [  --with-iconv supports iconv() (default=auto)],
[
 case "$withval" in
	 yes|no) wiconv="$withval";;
	 *) AC_MSG_ERROR([--with-iconv does not take an argument]);;
 esac],
[ wiconv=auto ])
if test "X$wiconv" != "Xno"; then
	AC_CHECK_LIB([iconv], [libiconv_open], LIBCLAMAV_LIBS="$LIBCLAMAV_LIBS -liconv")
	AC_MSG_CHECKING([for iconv])
	save_LIBS="$LIBS"
	LIBS="$LIBCLAMAV_LIBS"
	AC_TRY_LINK([
		     #include <iconv.h>
	],[
	char** xin,**xout;
	unsigned il,ol;
	int rc;
	iconv_t iconv_struct = iconv_open("UTF-16BE","UTF-8");
	rc = iconv(iconv_struct,xin,&il,xout,&ol);
	iconv_close(iconv_struct);
],[
   AC_MSG_RESULT(yes)
   AC_DEFINE([HAVE_ICONV], 1, [iconv() available])
],[
	AC_MSG_RESULT(no)
])
	LIBS="$save_LIBS"
fi

AC_ARG_ENABLE([pthreads],
[  --disable-pthreads      disable POSIX threads support],
have_pthreads=$enableval,)

AC_ARG_ENABLE([cr],
[  --disable-cr		  don't link with C reentrant library (BSD) ],
use_cr=$enableval,)

AC_ARG_ENABLE([id-check],
[  --enable-id-check	  use id utility instead of /etc/passwd parsing],
use_id=$enableval, use_id="no")

AC_ARG_ENABLE([yp-check],
[  --enable-yp-check	  use ypmatch utility instead of /etc/passwd parsing],
use_yp=$enableval, use_yp="no")

AC_ARG_WITH([user], 
[  --with-user=uid	  name of the clamav user (default=clamav)],
clamav_user="$withval", clamav_user="clamav")

AC_ARG_WITH([group], 
[  --with-group=gid	  name of the clamav group (default=clamav)],
clamav_group="$withval", clamav_group="clamav")

AC_DEFINE_UNQUOTED([CLAMAVUSER],"$clamav_user",[name of the clamav user])
AC_DEFINE_UNQUOTED([CLAMAVGROUP],"$clamav_group",[name of the clamav group])

AC_ARG_ENABLE([clamav],
[  --disable-clamav	  disable test for clamav user/group],
test_clamav=$enableval, test_clamav=yes)

AC_ARG_ENABLE([debug],
[  --enable-debug	  enable debug code],
enable_debug=$enableval, enable_debug="no")

if test "$enable_debug" = "yes"; then
  AC_DEFINE([CL_DEBUG],1,[enable debugging])
fi

AC_ARG_ENABLE([no-cache],
[  --enable-no-cache	  use "Cache-Control: no-cache" in freshclam],
enable_nocache=$enableval, enable_nocache="no")

if test "$enable_nocache" = "yes"; then
  AC_DEFINE([FRESHCLAM_NO_CACHE],1,[use "Cache-Control: no-cache" in freshclam])
fi

AC_ARG_ENABLE([dns-fix],
[  --enable-dns-fix	  enable workaround for broken DNS servers (as in SpeedTouch 510)],
enable_dnsfix=$enableval, enable_dnsfix="no")

if test "$enable_dnsfix" = "yes"; then
  AC_DEFINE([FRESHCLAM_DNS_FIX],1,[enable workaround for broken DNS servers])
fi

AC_ARG_ENABLE([bigstack],
[  --enable-bigstack	  increase thread stack size],
enable_bigstack=$enableval, enable_bigstack="no")

if test "$enable_bigstack" = "yes"; then
  AC_DEFINE([C_BIGSTACK],1,[Increase thread stack size.])
fi

AC_ARG_WITH([dbdir], 
[  --with-dbdir=path	  path to virus database directory],
db_dir="$withval", db_dir="_default_")

dnl I had problems with $pkgdatadir thus these funny checks
if test "$db_dir" = "_default_"
then
    if test "$prefix" = "NONE"
    then
	db_dir="$ac_default_prefix/share/clamav"
    else
	db_dir="$prefix/share/clamav"
    fi
fi

AC_DEFINE_UNQUOTED([DATADIR],"$db_dir", [Path to virus database directory.])
DBDIR="$db_dir"
AC_SUBST([DBDIR])

dnl configure config directory
cfg_dir=`echo $sysconfdir | grep prefix`

if test -n "$cfg_dir"; then
    if test "$prefix" = "NONE"
    then
	cfg_dir="$ac_default_prefix/etc"
    else
	cfg_dir="$prefix/etc"
    fi
else
    cfg_dir="$sysconfdir"
fi

CFGDIR=$cfg_dir
AC_SUBST([CFGDIR])
AC_DEFINE_UNQUOTED([CONFDIR],"$cfg_dir",[where to look for the config file])

dnl check for in_port_t definition
AC_MSG_CHECKING([whether in_port_t is defined])
AC_TRY_COMPILE([
#include <sys/types.h>
#include <netinet/in.h>
],
[in_port_t pt; pt = 0; return pt;],
[
    AC_MSG_RESULT(yes)
    AC_DEFINE([HAVE_IN_PORT_T],1,[in_port_t is defined])
],
AC_MSG_RESULT(no))

dnl check for in_addr_t definition
AC_MSG_CHECKING([for in_addr_t definition])
AC_TRY_COMPILE([
#include <sys/types.h>
#include <netinet/in.h>
],
[ in_addr_t pt; pt = 0; return pt; ],
[
    AC_MSG_RESULT(yes)
    AC_DEFINE([HAVE_IN_ADDR_T],1,[in_addr_t is defined])
],
AC_MSG_RESULT(no))

case "$target_os" in
linux*)
    AC_DEFINE([C_LINUX],1,[target is linux])
    if test "$have_pthreads" = "yes"; then
	THREAD_LIBS="-lpthread"
	TH_SAFE="-thread-safe"
	AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
	AC_DEFINE([_REENTRANT],1,[thread safe])
	if test "$want_clamuko" = "yes"; then
	    AC_DEFINE([CLAMUKO],1,[enable clamuko])
	fi
    fi
    ;;
kfreebsd*-gnu)
    AC_DEFINE([C_KFREEBSD_GNU],1,[target is kfreebsd-gnu])
    if test "$have_pthreads" = "yes"; then
       THREAD_LIBS="-lpthread"
       TH_SAFE="-thread-safe"
       AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
       AC_DEFINE([_REENTRANT],1,[thread safe])
       if test "$want_clamuko" = "yes"; then
           AC_DEFINE([CLAMUKO],1,[enable clamuko])
       fi
    fi
    ;;
cygwin*)
    AC_DEFINE([C_CYGWIN],1,[os is cygwin])
    if test "$test_clamav" = "yes"; then
	if test ! -r /etc/passwd; then
	   test_clamav="no"
	fi
    fi
    if test "$have_pthreads" = "yes"; then
	THREAD_LIBS="-lpthread"
	TH_SAFE="-thread-safe"
	AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
	AC_DEFINE([_REENTRANT],1,[thread safe])
    fi
    use_gethostbyname_r="no"
    ;;
solaris*)
    FRESHCLAM_LIBS="$FRESHCLAM_LIBS -lresolv"
    if test "$have_pthreads" = "yes"; then
	THREAD_LIBS="-lpthread"
	CLAMD_LIBS="$CLAMD_LIBS -lresolv"
	CLAMAV_MILTER_LIBS="$CLAMAV_MILTER_LIBS -lresolv"
	TH_SAFE="-thread-safe"
	AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
	AC_DEFINE([_REENTRANT],1,[thread safe])
    fi
    AC_DEFINE([C_SOLARIS],1,[os is solaris])
    ;;
freebsd[[45]]*)
    if test "$have_pthreads" = "yes"; then
	THREAD_LIBS="-pthread -lc_r"
	TH_SAFE="-thread-safe"
	AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
	AC_DEFINE([_REENTRANT],1,[thread safe])
	if test "$want_clamuko" = "yes"; then
	    AC_DEFINE([CLAMUKO],1,[enable clamuko])
	fi
    fi
    AC_DEFINE([C_BSD],1,[os is freebsd 4 or 5])
    use_gethostbyname_r="no"
    ;;
freebsd*)
    if test "$have_pthreads" = "yes"; then
	THREAD_LIBS="-lthr"
	TH_SAFE="-thread-safe"
	AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
	AC_DEFINE([_REENTRANT],1,[thread safe])
	if test "$want_clamuko" = "yes"; then
	    AC_DEFINE([CLAMUKO],1,[enable clamuko])
	fi
    fi
    AC_DEFINE([C_BSD],1,[os is freebsd 6])
    use_gethostbyname_r="no"
    ;;
dragonfly*)
    if test "$have_pthreads" = "yes"; then
	THREAD_LIBS="-pthread"
	TH_SAFE="-thread-safe"
	AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
	AC_DEFINE([_REENTRANT],1,[thread safe])
	if test "$want_clamuko" = "yes"; then
	    AC_DEFINE([CLAMUKO],1,[enable clamuko])
	fi
    fi
    AC_DEFINE([C_BSD],1,[os is dragonfly])
    use_gethostbyname_r="no"
    ;;
openbsd*)
    if test "$have_pthreads" = "yes"; then
	THREAD_LIBS="-pthread"
	CLAMD_LIBS="$CLAMD_LIBS -pthread"
	CLAMAV_MILTER_LIBS="$CLAMAV_MILTER_LIBS -pthread"
	TH_SAFE="-thread-safe"
	AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
	AC_DEFINE([_REENTRANT],1,[thread safe])
    fi
    AC_DEFINE([C_BSD],1,[os is OpenBSD])
    use_gethostbyname_r="no"
    ;;
bsdi*)
    if test "$have_pthreads" = "yes"; then
	THREAD_LIBS="-pthread"
	TH_SAFE="-thread-safe"
	AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
	AC_DEFINE([_REENTRANT],1,[thread safe])
    fi
    AC_DEFINE([C_BSD],1,[os is BSDI BSD/OS])
    use_gethostbyname_r="no"
    ;;
netbsd*)
     if test "$have_pthreads" = "yes"; then
	THREAD_LIBS="-lpthread"
	AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
	AC_DEFINE([_REENTRANT],1,[thread safe])
     fi
    AC_DEFINE([C_BSD],1,[os is NetBSD])
    use_gethostbyname_r="no"
    ;;
bsd*)
    AC_MSG_RESULT([Unknown BSD detected. Disabling thread support.])
    have_pthreads="no"
    AC_DEFINE([C_BSD],1,[os is bsd flavor])
    use_gethostbyname_r="no"
    ;;
beos*)
    AC_MSG_RESULT([BeOS detected. Disabling thread support.])
    have_pthreads="no"
    AC_DEFINE([C_BEOS],1,[os is beos])
    use_gethostbyname_r="no"
    ;;
darwin*)
    AC_DEFINE([C_BSD],1,[os is bsd flavor])
    AC_DEFINE([C_DARWIN],1,[os is darwin])
    AC_DEFINE([BIND_8_COMPAT],1,[enable bind8 compatibility])
    use_netinfo="yes"
    use_gethostbyname_r="no"
    ;;
os2*)
    FRESHCLAM_LIBS="$FRESHCLAM_LIBS -lsyslog"
    CLAMD_LIBS="$CLAMD_LIBS -lsyslog"
    CLAMAV_MILTER_LIBS="$CLAMAV_MILTER_LIBS -lsyslog"
    if test "$have_pthreads" = "yes"; then
	THREAD_LIBS="-lpthread"
	TH_SAFE="-thread-safe"
	AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
	AC_DEFINE([_REENTRANT],1,[thread safe])
    fi
    AC_DEFINE([C_OS2],1,[os is OS/2])
    ;;
sco*)
    dnl njh@bandsman.sco.uk: SCO Unix port
    dnl FRESHCLAM_LIBS="-lsocket"
    dnl CLAMD_LIBS="-lsocket"
    dnl CLAMAV_MILTER_LIBS="$CLAMAV_MILTER_LIBS -lsocket"
    ;;
hpux*)
    if test "$have_pthreads" = "yes"; then
	THREAD_LIBS="-lpthread"
	TH_SAFE="-thread-safe"
	AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
	AC_DEFINE([_REENTRANT],1,[thread safe])
    fi
    AC_DEFINE([C_HPUX],1,[os is hpux])
    use_gethostbyname_r="no"
    ;;
aix*)
    if test "$have_pthreads" = "yes"; then
	THREAD_LIBS="-lpthread"
	TH_SAFE="-thread-safe"
	AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
	AC_DEFINE([_REENTRANT],1,[thread safe])
    fi
    AC_DEFINE([C_AIX],1,[os is aix])
    ;;
*-*-osf*)
    if test "$have_pthreads" = "yes"; then
	THREAD_LIBS="-pthread"
	TH_SAFE="-thread-safe"
	AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
	AC_DEFINE([_REENTRANT],1,[thread safe])
	AC_DEFINE([_POSIX_PII_SOCKET],1,[POSIX compatibility])
    fi
    AC_DEFINE([C_OSF],1,[os is osf/tru64])
    ;;
nto-qnx*)
     if test "$have_pthreads" = "yes"; then
       AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
       AC_DEFINE([_REENTRANT],1,[thread safe])
     fi
    AC_DEFINE([C_QNX6],1,[os is QNX 6.x.x])
    ;;
irix*)
    if test "$have_pthreads" = "yes"; then
	THREAD_LIBS="-lpthread"
	TH_SAFE="-thread-safe"
	AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
	AC_DEFINE([_REENTRANT],1,[thread safe])
    fi
    AC_DEFINE([C_IRIX],1,[os is irix])
    ;;
interix*)
    AC_DEFINE([C_INTERIX],1,[os is interix])
    if test "$test_clamav" = "yes"; then
	if test ! -r /etc/passwd; then
	   test_clamav="no"
	fi
    fi
    if test "$have_pthreads" = "yes"; then
	THREAD_LIBS="-lpthread"
	TH_SAFE="-thread-safe"
	AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
	AC_DEFINE([_REENTRANT],1,[thread safe])
    fi
    ;;
gnu*)
    AC_DEFINE([C_GNU_HURD],1,[target is gnu-hurd])
    if test "$have_pthreads" = "yes"; then
       THREAD_LIBS="-lpthread"
       TH_SAFE="-thread-safe"
       AC_DEFINE([CL_THREAD_SAFE],1,[thread safe])
       AC_DEFINE([_REENTRANT],1,[thread safe])
    fi
    ;;
*)
    ;;
esac

AC_SUBST([LIBCLAMAV_LIBS])
AC_SUBST([CLAMD_LIBS])
AC_SUBST([CLAMAV_MILTER_LIBS])
AC_SUBST([FRESHCLAM_LIBS])
AC_SUBST([TH_SAFE])
AC_SUBST([THREAD_LIBS])

dnl --enable-milter
if test "$have_milter" = "yes"; then
    dnl libmilter checking code adapted from spamass-milter by
    dnl Tom G. Christensen <tgc@statsbiblioteket.dk>

    dnl Check for libmilter and it's header files in the usual locations
    save_LDFLAGS="$LDFLAGS"
	CLAMAV_MILTER_LIBS="$CLAMAV_MILTER_LIBS -lpthread"
    if test -d /usr/lib/libmilter ; then
	CLAMAV_MILTER_LIBS="$CLAMAV_MILTER_LIBS -L/usr/lib/libmilter"
    fi
    LDFLAGS="$LDFLAGS -lmilter $CLAMAV_MILTER_LIBS"
    AC_CHECK_LIB([milter],[mi_stop],[CLAMAV_MILTER_LIBS="-lmilter $CLAMAV_MILTER_LIBS"],[
	dnl Older sendmails require libsm or libsmutil for support functions
	AC_SEARCH_LIBS([strlcpy], [sm smutil], [test "$ac_cv_search_strlcpy" = "none required" || CLAMAV_MILTER_XLIB="$ac_cv_search_strlcpy"])
	LDFLAGS="$save_LDFLAGS $CLAMAV_MILTER_LIBS $CLAMAV_MILTER_XLIB"
	$as_unset ac_cv_lib_milter_mi_stop
	AC_CHECK_LIB([milter],[mi_stop],[CLAMAV_MILTER_LIBS="-lmilter $CLAMAV_MILTER_XLIB $CLAMAV_MILTER_LIBS"],[
	    AC_MSG_ERROR([Cannot find libmilter])
	])
    ])
    LDFLAGS="$save_LDFLAGS"
    AC_CHECK_HEADERS([libmilter/mfapi.h],[have_milter="yes"],[
	AC_MSG_ERROR([Please install mfapi.h from the sendmail distribution])
    ])
fi

AM_CONDITIONAL([BUILD_CLAMD],[test "$have_pthreads" = "yes"])
AM_CONDITIONAL([HAVE_MILTER],[test "$have_milter" = "yes"])
AM_CONDITIONAL([ENABLE_UNRAR],[test "$want_unrar" = "yes"])

if test "$have_pthreads" = "yes"
then
    save_LIBS="$LIBS"
    LIBS="$THREAD_LIBS $LIBS"
    AC_CHECK_FUNCS([sched_yield pthread_yield])
    LIBS="$save_LIBS"
    AC_DEFINE([BUILD_CLAMD], 1, "build clamd")
fi

if test "$want_unrar" = "yes"
then
    AC_DEFINE([ENABLE_UNRAR], 1, "build unrar code")
fi


dnl tcpwrappers support
dnl rules from http://ma.ph-freiburg.de/tng/tng-technical/2002-01/msg00094.html
AC_ARG_WITH([tcpwrappers],
[  --with-tcpwrappers      support hosts.allow / hosts.deny (default=auto)],
[ case "$withval" in
  yes|no) tcpw="$withval" ;;
  *)      AC_MSG_ERROR([--with-tcpwrappers does not take an argument]) ;;
  esac],
[ tcpw=auto ])

if test "$tcpw" != "no" && test "$have_milter" = "yes" ; then
        AC_CHECK_HEADERS([tcpd.h],[
                AC_MSG_CHECKING([for TCP wrappers library])
                save_LIBS="$LIBS"
                LIBS="$LIBS -lwrap $FRESHCLAM_LIBS"
                AC_TRY_LINK([
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <tcpd.h>
int allow_severity = 0;
int deny_severity  = 0;

struct request_info *req;
                ],[
hosts_access(req)
                ],[AC_MSG_RESULT([-lwrap])
                have_wrappers=yes
		LIBS="$save_LIBS"
                CLAMAV_MILTER_LIBS="$CLAMAV_MILTER_LIBS -lwrap"],[
                dnl try with -lnsl
                LIBS="$LIBS -lnsl $FRESHCLAM_LIBS"
                AC_TRY_LINK([
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <tcpd.h>
int allow_severity = 0;
int deny_severity  = 0;

struct request_info *req;
                ],[
hosts_access(req)
                ],[AC_MSG_RESULT([-lwrap -lnsl])
                have_wrappers=yes
                CLAMAV_MILTER_LIBS="$CLAMAV_MILTER_LIBS -lwrap"],[
                AC_MSG_RESULT(no)
                have_wrappers=no
                LIBS=$save_LIBS])],[
                have_wrappers=no])],[have_wrappers=no])

        if test $have_wrappers = yes ; then
                AC_DEFINE([WITH_TCPWRAP],1, [tcpwrappers support])
        elif test $tcpw = yes ; then
                AC_MSG_ERROR([could not find TCP wrappers])
        else
                AC_MSG_WARN([could not find TCP wrappers, support disabled])
        fi
fi

dnl Check for gethostbyname_r and number of its arguments
dnl Code from libwww/configure.in
AC_MSG_CHECKING([for gethostbyname_r])
if test -z "$ac_cv_gethostbyname_args"; then
AC_TRY_COMPILE(
[
#include <sys/types.h>
#include <netdb.h>
],[
struct hostent *hp;
struct hostent h;
char *name;
char buffer[10];
int  h_errno;
hp = gethostbyname_r(name, &h, buffer, 10, &h_errno);
], ac_cv_gethostbyname_args=5)
fi
if test -z "$ac_cv_gethostbyname_args"; then
AC_TRY_COMPILE(
[
#include <sys/types.h>
#include <netdb.h>
],[
struct hostent h;
struct hostent_data hdata;
char *name;
int  rc;
rc = gethostbyname_r(name, &h, &hdata);
], ac_cv_gethostbyname_args=3)
fi
if test -z "$ac_cv_gethostbyname_args"; then
AC_TRY_COMPILE(
[
#include <sys/types.h>
#include <netdb.h>
], [
struct hostent h;
struct hostent *hp;
char *name;
char buf[10];
int rc;
int h_errno;

rc = gethostbyname_r(name, &h, buf, 10, &hp, &h_errno);
], ac_cv_gethostbyname_args=6)
fi

AC_ARG_ENABLE([gethostbyname_r],
[  --disable-gethostbyname_r	  disable support for gethostbyname_r],
use_gethostbyname_r=$enableval,)

if test "$use_gethostbyname_r" = "no"; then
    AC_MSG_RESULT([support disabled])
elif test -z "$ac_cv_gethostbyname_args"; then
    AC_MSG_RESULT(no)
else
    if test "$ac_cv_gethostbyname_args" = 3; then
	AC_DEFINE([HAVE_GETHOSTBYNAME_R_3],1,[gethostbyname_r takes 3 arguments])
    elif test "$ac_cv_gethostbyname_args" = 5; then
	AC_DEFINE([HAVE_GETHOSTBYNAME_R_5],1,[gethostbyname_r takes 5 arguments])
    elif test "$ac_cv_gethostbyname_args" = 6; then
	AC_DEFINE([HAVE_GETHOSTBYNAME_R_6],1,[gethostbyname_r takes 6 arguments])
    fi
    AC_MSG_RESULT([yes, and it takes $ac_cv_gethostbyname_args arguments])
fi

dnl Check for readdir_r and number of its arguments
dnl Code from libwww/configure.in

AC_MSG_CHECKING([for readdir_r])
if test -z "$ac_cv_readdir_args"; then
    AC_TRY_COMPILE(
    [
#include <sys/types.h>
#include <dirent.h>
    ],
    [
    struct dirent dir, *dirp;
    DIR *mydir;
    dirp = readdir_r(mydir, &dir);
    ], ac_cv_readdir_args=2)
fi
if test -z "$ac_cv_readdir_args"; then
    AC_TRY_COMPILE(
        [
#include <sys/types.h>
#include <dirent.h>
    ],
    [
        struct dirent dir, *dirp;
        DIR *mydir;
        int rc;
        rc = readdir_r(mydir, &dir, &dirp);
    ], ac_cv_readdir_args=3)
fi

AC_ARG_ENABLE([readdir_r],
[  --enable-readdir_r		    enable support for readdir_r],
enable_readdir_r=$enableval, enable_readdir_r="no")

if test "$enable_readdir_r" = "no"; then
    AC_MSG_RESULT(support disabled)
elif test -z "$ac_cv_readdir_args"; then
    AC_MSG_RESULT(no)
else
    if test "$ac_cv_readdir_args" = 2; then
	AC_DEFINE([HAVE_READDIR_R_2],1,[readdir_r takes 2 arguments])
    elif test "$ac_cv_readdir_args" = 3; then
	AC_DEFINE([HAVE_READDIR_R_3],1,[readdir_r takes 3 arguments])
    fi
    AC_MSG_RESULT([yes, and it takes $ac_cv_readdir_args arguments])
fi

AC_MSG_CHECKING([for ctime_r])
if test "$ac_cv_func_ctime_r" = "yes"; then
    AC_TRY_COMPILE([
	#include <time.h>
    ],[
	char buf[31];
	time_t t;
	ctime_r(&t, buf, 30);
    ],[
	ac_cv_ctime_args=3
	AC_DEFINE([HAVE_CTIME_R_3],1,[ctime_r takes 3 arguments])
    ],[
	ac_cv_ctime_args=2
	AC_DEFINE([HAVE_CTIME_R_2],1,[ctime_r takes 2 arguments])
    ])

    AC_MSG_RESULT([yes, and it takes $ac_cv_ctime_args arguments])
fi

dnl Determine socklen_t type. Code from lftp.
AC_MSG_CHECKING([for socklen_t])
AC_CACHE_VAL([ac_cv_socklen_t],
[
    ac_cv_socklen_t=no
    AC_TRY_COMPILE([
	#include <sys/types.h>
        #include <sys/socket.h>
    ],
    [
	socklen_t len;
        getpeername(0,0,&len);
    ],
    [
	ac_cv_socklen_t=yes
    ])
])
AC_MSG_RESULT([$ac_cv_socklen_t])
    if test $ac_cv_socklen_t = no; then
    AC_MSG_CHECKING([for socklen_t equivalent])
    AC_CACHE_VAL([ac_cv_socklen_t_equiv],
    [
	ac_cv_socklen_t_equiv=int
        for t in int size_t unsigned long "unsigned long"; do
	    AC_TRY_COMPILE([
		#include <sys/types.h>
		#include <sys/socket.h>
	    ],
            [
		$t len;
		getpeername(0,0,&len);
            ],
            [
		ac_cv_socklen_t_equiv="$t"
		break
            ])
	done
    ])
    AC_MSG_RESULT([$ac_cv_socklen_t_equiv])
    AC_DEFINE_UNQUOTED([socklen_t], $ac_cv_socklen_t_equiv, [Define to "int" if <sys/socket.h> does not define.])
fi

dnl Check for clamav in /etc/passwd
if test "$test_clamav" = "yes"
then
    dnl parse /etc/passwd
    if test "$use_id" = "no"
    then
	AC_MSG_CHECKING([for $clamav_user in /etc/passwd])
	if test -r /etc/passwd; then
	    clamavuser=`cat /etc/passwd|grep "^$clamav_user:"`
	    clamavgroup=`cat /etc/group|grep "^$clamav_group:"`
	fi
    else
	AC_MSG_CHECKING([for $clamav_user using id])
	id $clamav_user > /dev/null 2>&1
	if test "$?" = 0 ; then
	    clamavuser=1
	    AC_PATH_PROG(GETENT, getent)
	    if test -n "$GETENT" ; then
		clamavgroup=`$GETENT group | grep "^${clamav_group}:"`
	    else
		clamavgroup=`cat /etc/group|grep $clamav_group`
	    fi
	fi
    fi

    if test "$use_netinfo" = "yes"
    then
	if test -x /usr/bin/dscl; then
	    AC_MSG_CHECKING([for $clamav_user using dscl])
	    clamavuser=`/usr/bin/dscl . -list /Users |grep ${clamav_user}`
	    clamavgroup=`/usr/bin/dscl . -list /Groups |grep ${clamav_group}`
	else
	    AC_MSG_CHECKING([for $clamav_user using netinfo])
	    clamavuser=`/usr/bin/nidump passwd . |grep ${clamav_user}`
	    clamavgroup=`/usr/bin/nidump group . |grep ${clamav_group}`
	fi
    fi

    if test "$use_yp" = "yes"
    then
	AC_MSG_CHECKING([for $clamav_user using ypmatch])
        clamavuser=`ypmatch ${clamav_user} passwd`
        clamavgroup=`ypmatch ${clamav_group} group`
    fi

    if test -z "$clamavuser" || test -z "$clamavgroup"
    then
	AC_MSG_RESULT(no)
	AC_MSG_ERROR([User $clamav_user (and/or group $clamav_group) doesn't exist. Please read the documentation !])
    else
	AC_MSG_RESULT([yes, user $clamav_user and group $clamav_group])
        CLAMAVUSER="$clamav_user"
        CLAMAVGROUP="$clamav_group"
        AC_SUBST([CLAMAVUSER])
        AC_SUBST([CLAMAVGROUP])
    fi
fi

dnl check for __attribute__((packed))
dnl but only on compilers claiming to be gcc compatible
dnl because for example Sun's compiler silently ignores the packed attribute.
AC_MSG_CHECKING([for structure packing via __attribute__((packed))])
AC_CACHE_VAL([have_attrib_packed],[
	AC_TRY_COMPILE(,
		[#ifdef __GNUC__
		 struct { int i __attribute__((packed)); } s;
		 #else
		 #error Only checking for packed attribute on gcc-like compilers
		 #endif],
		[have_attrib_packed=yes],
		[have_attrib_packed=no])
	])
AC_MSG_RESULT([$have_attrib_packed])

if test "$have_attrib_packed" = no; then
	AC_MSG_CHECKING([for structure packing via pragma])
	AC_CACHE_VAL([have_pragma_pack],[
		AC_TRY_RUN([int main(int argc, char **argv) {
#pragma pack(1)			/* has to be in column 1 ! */
			struct { char c; long l; } s;
			return sizeof(s)==sizeof(s.c)+sizeof(s.l) ? 0:1; } ],
			[have_pragma_pack=yes],
			[have_pragma_pack=no])
		])
	AC_MSG_RESULT([$have_pragma_pack])
	if test "$have_pragma_pack" = yes; then
		AC_DEFINE([HAVE_PRAGMA_PACK], 1, "pragma pack")
	else
		AC_MSG_CHECKING([for structure packing via hppa/hp-ux pragma])
		AC_CACHE_VAL([have_pragma_pack_hpux],[
			AC_TRY_RUN([
			/* hppa/hp-ux wants pragma outside of function */
#pragma pack 1 /* has to be in column 1 ! */
			struct { char c; long l; } s;
			    int main(int argc, char **argv) {
			return sizeof(s)==sizeof(s.c)+sizeof(s.l) ? 0:1; } ],
			[have_pragma_pack_hpux=yes],
			[have_pragma_pack_hpux=no])
		])
		AC_MSG_RESULT([$have_pragma_pack_hpux])
		AC_DEFINE([HAVE_PRAGMA_PACK_HPPA], 1, "pragma pack hppa/hp-ux style")
	fi
fi

dnl check for __attribute__((aligned))
AC_MSG_CHECKING([for type aligning via __attribute__((aligned))])
AC_CACHE_VAL([have_attrib_aligned],[
	AC_TRY_COMPILE(,
		[typedef int cl_aligned_int __attribute__((aligned));],
		[have_attrib_aligned=yes],
		[have_attrib_aligned=no])
	])
AC_MSG_RESULT([$have_attrib_aligned])

if test "$have_attrib_packed" = no -a "$have_pragma_pack" = no -a "$have_pragma_pack_hpux" = no; then
	AC_MSG_ERROR(Need to know how to pack structures with this compiler)
fi

if test "$have_attrib_packed" = yes; then
	AC_DEFINE([HAVE_ATTRIB_PACKED], 1, [attrib packed])
fi

if test "$have_attrib_aligned" = yes; then
	AC_DEFINE([HAVE_ATTRIB_ALIGNED], 1, [attrib aligned])
fi

dnl Check if <sys/select.h> needs to be included for fd_set
AC_MSG_CHECKING([for fd_set])
AC_HEADER_EGREP([fd_mask], [sys/select.h], [have_fd_set=yes])
if test "$have_fd_set" = yes; then
	AC_DEFINE([HAVE_SYS_SELECT_H], 1, "have <sys/select.h>")
	AC_MSG_RESULT([yes, found in sys/select.h])
else
	AC_TRY_COMPILE([#include <sys/time.h>
			#include <sys/types.h>
			#ifdef HAVE_UNISTD_H
			#include <unistd.h>
			#endif],
		[fd_set readMask, writeMask;], have_fd_set=yes, have_fd_set=no)
	if test "$have_fd_set" = yes; then
		AC_MSG_RESULT([yes, found in sys/types.h])
	else
		AC_DEFINE([NO_FD_SET], 1, "no fd_set")
		AC_MSG_RESULT(no)
	fi
fi

AC_MSG_CHECKING([default FD_SETSIZE value])
AC_TRY_RUN([
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <errno.h>
int main(void) {
        FILE *fp = fopen("conftestval", "w");
	if(fp) {
		if(fprintf (fp, "%d\n", FD_SETSIZE) < 1)  {
			perror("fprintf failed");
			return errno;
		}
	} else {
		perror("fopen failed");
		return errno;
	}
        return 0;
}
],
DEFAULT_FD_SETSIZE=`cat conftestval`,
DEFAULT_FD_SETSIZE=256,
DEFAULT_FD_SETSIZE=256)
AC_MSG_RESULT([$DEFAULT_FD_SETSIZE])
AC_DEFINE_UNQUOTED([DEFAULT_FD_SETSIZE], $DEFAULT_FD_SETSIZE, "default FD_SETSIZE value")

dnl Check signed right shift implementation
AC_MSG_CHECKING([whether signed right shift is sign extended])
AC_TRY_RUN([int main(void){int a=-1;int b=a>>1;return(a!=b);}],
    [have_signed_rightshift_extended=yes],
    [have_signed_rightshift_extended=no],
    [have_signed_rightshift_extended=no])
if test $have_signed_rightshift_extended = yes; then
    AC_DEFINE([HAVE_SAR], 1, [Define signed right shift implementation])
fi
AC_MSG_RESULT([$have_signed_rightshift_extended]);

AC_C_FDPASSING

AC_OUTPUT([
libclamunrar/Makefile
libclamunrar_iface/Makefile
libclamav/Makefile
libclamav/lzma/Makefile
clamscan/Makefile
database/Makefile
docs/Makefile
clamd/Makefile
clamdscan/Makefile
clamav-milter/Makefile
freshclam/Makefile
sigtool/Makefile
clamconf/Makefile
etc/Makefile
test/Makefile
unit_tests/Makefile
Makefile
clamav-config
libclamav.pc
docs/man/clamav-milter.8
docs/man/clamconf.1
docs/man/clamd.8
docs/man/clamd.conf.5
docs/man/clamdscan.1
docs/man/clamscan.1
docs/man/freshclam.1
docs/man/freshclam.conf.5
docs/man/sigtool.1
])

if test "x$cross_compiling" != "xno" || test "x$gcc_check" = "xno" || test "x$zlib_check" = "xno" ; then
AC_MSG_WARN([
****** WARNING:
****** You are either cross compiling to a different host or
****** you have manually disabled important configure checks.
****** Please be aware that this build may be badly broken.
****** DO NOT REPORT BUGS BASED ON THIS BUILD !!!
])
fi

