forked from antirez/hping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
byteorder.c
80 lines (68 loc) · 1.46 KB
/
byteorder.c
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#if 0
#
# Compile with:
# $sh byteorder.c
#
cc byteorder.c -o byteorder || exit 1
echo successfully compiled
exit
#endif /* 0 */
/* Coypright (C) 1999-2003 Salvatore Sanfilippo */
/*
* 0.1 first version
* 0.2 add Strchr, so it's possibile remove string.h
* 0.3 more portable thx to Pancrazio De Mauro 'TrantIT'!!!
* 0.4 better debug output
*/
/* $Id: byteorder.c,v 1.2 2003/09/01 00:22:06 antirez Exp $ */
#include <stdio.h>
char *Strchr(char *s, char c)
{
while(*s)
if (*s++ == c)
return s;
return (char*) 0;
}
int main(int argc, char **argv)
{
unsigned int test = 1;
unsigned char *x;
int macro = 0, debug = 0, help = 0, j;
for (j = 1; j < argc; j++) {
if (Strchr(argv[j], 'm')) macro = 1;
if (Strchr(argv[j], 'd')) debug = 1;
if (Strchr(argv[j], 'h')) help = 1;
}
if (help) {
printf( "-m macro output\n"
"-d debug\n"
"-h help\n");
return 0;
}
x = (unsigned char*) &test;
if (*x == 0x00) {
if (macro)
printf("__BIG_ENDIAN_BITFIELD\n");
else
printf("big endian\n");
}
else if (*x == 0x01) {
if (macro)
printf("__LITTLE_ENDIAN_BITFIELD\n");
else
printf("little endian\n");
} else {
printf("\nWARNING!!! byteorder exception\n\n");
debug = 1;
}
if (debug) {
printf("sizeof(unsigned int) = %d\n",
(int)sizeof(unsigned int));
printf("unsigned int test = 1;\n");
printf("in memory as: ");
for (j = 0; j < sizeof(unsigned int); j++)
printf("%02x ", x[j]);
printf("\n");
}
return 0;
}