1# $Id: endian.m4 595 2004-11-19 16:17:17Z aquamaniac $
2# (c) 2006 Martin Preuss<martin@libchipcard.de>
3# This function check for endianness
4
5AC_DEFUN([AQ_ENDIAN],[
6dnl PREREQUISITES:
7dnl   none
8dnl IN:
9dnl   - default type for crosscompiling
10dnl OUT:
11dnl   Variables:
12dnl     aq_endian
13dnl   Defines:
14
15# check for type to use
16AC_MSG_CHECKING(for endianness)
17rm -f conf.endian
18AC_TRY_RUN([
19#include <stdlib.h>
20#include <stdio.h>
21
22int main (){
23  const char *s;
24  FILE *f;
25  union {
26    unsigned int i;
27    unsigned char c[4];
28  } u;
29
30  u.i=0x12345678;
31  if (u.c[0]==0x78)
32   s="little";
33  else
34   s="big";
35
36  f=fopen("conf.endian", "w+");
37  if (!f) {
38    printf("Could not create file conf.endian\n");
39    exit(1);
40  }
41  fprintf(f, "%s\n", s);
42  if (fclose(f)) {
43   printf("Could not close file.\n");
44   exit(1);
45  }
46  exit(0);
47}
48 ],
49 [aq_endian=`cat conf.endian`; AC_MSG_RESULT($aq_endian)],
50 [AC_MSG_ERROR(Could not determine endianness)],
51 [aq_endian="$1"; AC_MSG_RESULT([Crosscompiling, assuming $1])]
52)
53rm -f conf.endian
54])
55
56