Endianness checker

c++ - December 2, 2016 - 06:37
  Check endianness and generate endianness.cfg. This small program can be used in a first step of compilation. endianness.cfg file can be included in Makefiles.
#include <stdio.h>

int main()
{
	printf ( "Endianness checker by Ludo (aka Ludorg) <ludo@chaman.net> (c) 2001 Chaman Productions\n");

	unsigned short word = 0x0001;
	char * c= ( char * ) &word;

	bool bIsBig= true;

	if ( c [ 0 ] )
		bIsBig = false;

	FILE * f = fopen ( "endianness.cfg", "w" );
	if ( bIsBig )
	{
		fprintf ( f, "ENDIAN_TYPE=__BIG_ENDIAN__\n" );
		printf ( "System is BIG ENDIAN. File endianness.cfg updated.\n" );
	}
	else
	{
		fprintf ( f, "ENDIAN_TYPE=__LITTLE_ENDIAN__\n" );
		printf ( "System is LITTLE ENDIAN. File endianness.cfg updated.\n" );
	}

	fclose ( f );	
}