#!/bin/sh
# endian -- determine endian-ness of machine
# Author: Noah Friedman <friedman@splode.com>
# Created: 1993-01-21
# Public domain.

# $Id: endian,v 1.3 2000/02/28 10:45:29 friedman Exp $

# Commentary:

# If `perl' cannot be found on the system, the C compiler is called by this
# shell script.  The environment variable `CC' and `CFLAGS' are used to
# determine exactly what compiler and args to use.  `cc' is the default.

# Code:

if { perl -v; } > /dev/null 2>&1; then
  exec perl -e '
    $e = unpack ("c2", pack ("i", 1)) ? "little" : "big";
    print $e, "-endian\n";'
else
  cd /tmp || exit 1
  file="endian$$"
  trap 'rm -f "$file" "${file}".[acos]' 0 1 2 3 15

  cat > "${file}.c" <<'__EOF__'
     main ()
     {
       union
         {
           long l;
           char c[sizeof (long)];
         } u;

       u.l = 1;
       exit (u.c[sizeof (long) - 1] == 1);
     }
__EOF__

  ${CC-cc} $CFLAGS "${file}.c" -o "./$file" || exit 1

  ./"$file"

  case $? in
     0) echo "little-endian" ;;
     1) echo "big-endian"    ;;
     *) echo "unknown"       ;;
  esac
fi

# eof
