File: byteorder.c

package info (click to toggle)
xntp3 5.93-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 7,644 kB
  • ctags: 7,419
  • sloc: ansic: 59,474; perl: 3,633; sh: 2,623; awk: 417; makefile: 311; asm: 37
file content (52 lines) | stat: -rw-r--r-- 864 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
 * This works on:
 *   Crays
 *   Conven
 *   sparc's
 *   Dec mip machines
 *   Dec alpha machines
 *   RS6000
 *   SGI's
 */

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

#if defined(LONG8)
    u.l = (((long)0x08070605) << 32) | (long)0x04030201;
#else
    u.l = 0x04030201;
#endif
    if (sizeof(long) > 4) {
	if (u.c[0] == 0x08) big = 1;
	else		    big = 0;
    } else {
	if (u.c[0] == 0x04) big = 1;
	else		    big = 0;
    }
    for (i=0; i< sizeof(long); i++) {
	if (big == 1 && (u.c[i] == (sizeof(long) - i))) { 
		continue; 
       	} else if (big == 0 && (u.c[i] == (i+1))) {
		continue;
	} else {
	     big = -1;
	     break;
	}
    }

    if (big == 1) {
	printf("XNTP_BIG_ENDIAN\n");
    } else if (big == 0) {
	printf("XNTP_LITTLE_ENDIAN\n");
    }
  exit(0);
}