#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/bigloo/autoconf/getrlimitfull        */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Wed Aug  9 13:27:23 1995                          */
#*    Last change :  Wed Jul  5 18:01:01 2023 (serrano)                */
#*    -------------------------------------------------------------    */
#*    Check if getrlimit exists. Return 0, 1.                          */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    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/actest$USER
aout=$TMP/Xactest$USER

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

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

#*---------------------------------------------------------------------*/
#*    Test                                                             */
#*---------------------------------------------------------------------*/
cat > $file.c <<EOF
#include <stdio.h>
#include <sys/resource.h>

long getstklimit() {
   struct rlimit rlimit;

   getrlimit( RLIMIT_CORE, &rlimit );
   getrlimit( RLIMIT_CPU, &rlimit );
   getrlimit( RLIMIT_DATA, &rlimit );
   getrlimit( RLIMIT_FSIZE, &rlimit );
   getrlimit( RLIMIT_LOCKS, &rlimit );
   getrlimit( RLIMIT_MEMLOCK, &rlimit );
   getrlimit( RLIMIT_MSGQUEUE, &rlimit );
   getrlimit( RLIMIT_NICE, &rlimit );
   getrlimit( RLIMIT_NOFILE, &rlimit );
   getrlimit( RLIMIT_NPROC, &rlimit );
   getrlimit( RLIMIT_RSS, &rlimit );
   getrlimit( RLIMIT_RTTIME, &rlimit );
   getrlimit( RLIMIT_SIGPENDING, &rlimit );
   getrlimit( RLIMIT_STACK, &rlimit );

   return rlimit.rlim_cur;
}

int main( int argc, char *argv[] ) {
   fprintf( stderr, "%d\n", getstklimit() );
}
EOF

#*---------------------------------------------------------------------*/
#*    Compilation test                                                 */
#*---------------------------------------------------------------------*/
if eval "$BUILDSH $compile"; then
   \rm -f $file.*
   rm -f $aout
   rm -f $aout*
   echo 1
else
   echo 0
fi

exit 0
