#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/bigloo/autoconf/sendfile             */
#*    -------------------------------------------------------------    */
#*    Author      :  Christian Loitsch                                 */
#*    Creation    :  Wed Apr 16 12:11:17 2003                          */
#*    Last change :  Mon Jul 10 13:55:31 2023 (serrano)                */
#*    -------------------------------------------------------------    */
#*    Check if sendfile exists.                                        */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    flags                                                            */
#*---------------------------------------------------------------------*/
cflags=

#*---------------------------------------------------------------------*/
#*    We parse the arguments                                           */
#*---------------------------------------------------------------------*/
while : ; do
  case $1 in
    "")
      break;;

    --cflags=*|-cflags=*)
      cflags="`echo $1 | sed 's/^[-a-z]*=//'`";;

    -*)
      echo "Unknown option \"$1\", ignored" >&2;;
  esac
  shift
done

file=$TMP/sftest$USER
aout=$TMP/SFtest$USER

#*---------------------------------------------------------------------*/
#*    compile                                                          */
#*---------------------------------------------------------------------*/
compile="$CC $CFLAGS $cflags $file.c -o $aout >/dev/null"

#*---------------------------------------------------------------------*/
#*    The test C file                                                  */
#*---------------------------------------------------------------------*/
if( test -f $file.c ); then
   rm -f $file.c || exit $?
fi

#*---------------------------------------------------------------------*/
#*    Linux test                                                       */
#*---------------------------------------------------------------------*/
cat > $file.c <<EOF
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <signal.h>

extern ssize_t sendfile();

int main( int argc, char *argv[] ) {
   char buffer [50];
   FILE *fd = fopen( "/etc/hosts", "r" );
   int s = socket( AF_INET, SOCK_STREAM, 0 );
   int res;

   signal( SIGPIPE, SIG_IGN );
   
   if( s < 0 ) return s;

   res = sendfile( s, fileno( fd ), 0, 8 );

   if( res >= 0 ) return 0;
   if( errno == EPIPE ) return 0;
   return 1;
}
EOF

if eval "$BUILDSH $compile"; then
   if eval "$HOSTSH $aout"; then
     rm -f $file.*
     rm -rf $aout*

     echo "BGL_SENDFILE_LINUX"
     exit 0
   fi
   rm -f $file.*
   rm -rf $aout*
else
  rm -f $file.*
fi

#*---------------------------------------------------------------------*/
#*    Bsd test                                                         */
#*---------------------------------------------------------------------*/
cat > $file.c <<EOF
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>

int main( int argc, char *argv[] )
{
   char buffer [50];
   FILE *fd = fopen( "/etc/hosts", "r" );
   int s = socket( AF_INET, SOCK_STREAM, 0 );
   int res;

   signal( SIGPIPE, SIG_IGN );
   
   if( s < 0 ) return s;

   res = sendfile( fileno( fd ), s, 0, 8, 0, 0 );

   if( res >= 0 ) return 0;
   if( errno == EPIPE ) return 0;
   return 1;
}
EOF

if eval "$BUILDSH $compile"; then
   if eval "$HOSTSH $aout"; then
     rm -f $file.*
     rm -rf $aout*

     echo "BGL_SENDFILE_BSD"
     exit 0
   fi
   rm -f $file.*
   rm -rf $aout*
else
  rm -f $file.*
fi

echo "BGL_SENDFILE_NO"
