comparison endianchk.c @ 105:9a0aeb9ce9ba

More robust endian check.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 15 Feb 2016 07:00:22 +0200
parents 8e7e08bf7b9e
children 170e447e0984
comparison
equal deleted inserted replaced
104:09ca9d9a8710 105:9a0aeb9ce9ba
1 #include <stdio.h> 1 #include <stdio.h>
2 2
3 int main(int argc, char *argv[]) 3 int main(int argc, char *argv[])
4 { 4 {
5 int val = 1, ret; 5 int val = 0x01020304, ret;
6 char *s = (char *) &val; 6 unsigned char *s = (unsigned char *) &val;
7 char *name = argv[0];
7 (void) argc; 8 (void) argc;
8 (void) argv; 9
9 ret = (int)(*s); 10 if (sizeof(int) != 4)
11 {
12 fprintf(stderr, "%s: sizeof(int) is not 32 bits!\n", name);
13 return -1;
14 }
15
16 if (s[0] == 0x01 && s[1] == 0x02 && s[2] == 0x03 && s[3] == 0x04)
17 ret = 0;
18 else
19 if (s[0] == 0x04 && s[1] == 0x03 && s[2] == 0x02 && s[3] == 0x01)
20 ret = 1;
21 else
22 {
23 fprintf(stderr, "%s: Unsupported endianess.\n", name);
24 return -2;
25 }
26
10 printf( 27 printf(
11 "#ifndef MY_CONFIG_H\n" 28 "#ifndef MY_CONFIG_H\n"
12 "#define MY_CONFIG_H 1\n" 29 "#define MY_CONFIG_H 1\n"
13 "\n" 30 "\n"
14 "#define TH_BYTEORDER TH_%s_ENDIAN\n" 31 "#define TH_BYTEORDER TH_%s_ENDIAN\n"