#!/bin/sh

# Here is a *modified* _fragment_ of a shell script called "Configure", distributed
# with perl and other utilities by Larry Wall, that determines the local
# machine's byte order.  The relevant C program is included too.

CC=cc
guess=BIG_ENDIAN

cat <<'EOM'
  
                        Checking Byte Order

In the following, larger digits indicate more significance.  A big-endian
machine like a Pyramid or a Motorola 680?0 chip will come out to 4321.  A
little-endian machine like a Vax or an Intel 80?86 chip would be 1234.  Other
machines may have weird orders like 3412.  A Cray will report 87654321.  If
the test program works the default is probably right.

I'm now running the test program...
EOM

echo "(C compiler: $CC, Initial guess: $guess)"

    cat >byteorder.c <<'EOCP'
#include <stdio.h>
main()
{
    int i;
    union {
	unsigned long l;
	char c[sizeof(long)];
    } u;

    if (sizeof(long) > 4)
	u.l = (0x08070605<<32) | 0x04030201;
    else
	u.l = 0x04030201;
    for (i=0; i < sizeof(long); i++)
	printf("%c",u.c[i]+'0');
    printf("\n");
}
EOCP

if $CC byteorder.c -o byteorder >/dev/null 2>&1 ; then
	word=`./byteorder`
	case "$word" in
	????|????????) echo "(The test program ran ok [$word])"
	  echo ""
          case "$word" in
          4321|87654321) echo "This platform seems to be: BIG_ENDIAN, you guessed $guess";;
          1234) echo "This platform seems to be: LITTLE_ENDIAN, you guessed $guess";;
          *) echo "Can't figure out the byte order, you guessed $guess";;
          esac;;
	*) echo "(The test program didn't run right for some reason.  Guessing $guess...)";;
	esac
else
	word='4321'
	echo "(I can't seem to compile the test program.  Guessing $guess...)"
fi

/bin/rm -f byteorder.c byteorder






