OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / user / hping / byteorder.c
1 #if 0
2 #
3 # Compile with:
4 #       $sh byteorder.c
5 #
6 cc byteorder.c -o byteorder || exit 1
7 echo successfully compiled
8 exit
9 #endif /* 0 */
10
11 /* Coypright (C) 1999-2003 Salvatore Sanfilippo */
12
13 /*
14  * 0.1 first version
15  * 0.2 add Strchr, so it's possibile remove string.h
16  * 0.3 more portable thx to Pancrazio De Mauro 'TrantIT'!!!
17  * 0.4 better debug output
18  */
19
20 /* $Id: byteorder.c,v 1.2 2003/09/01 00:22:06 antirez Exp $ */
21
22 #include <stdio.h>
23
24 char *Strchr(char *s, char c)
25 {
26         while(*s)
27                 if (*s++ == c)
28                         return s;
29
30         return (char*) 0;
31 }
32
33 int main(int argc, char **argv)
34 {
35         unsigned int test = 1;
36         unsigned char *x;
37         int macro = 0, debug = 0, help = 0, j;
38
39         for (j = 1; j < argc; j++) {
40                 if (Strchr(argv[j], 'm')) macro = 1;
41                 if (Strchr(argv[j], 'd')) debug = 1;
42                 if (Strchr(argv[j], 'h')) help = 1;
43         }
44
45         if (help) {
46                 printf( "-m     macro output\n"
47                         "-d     debug\n"
48                         "-h     help\n");
49                 return 0;
50         }
51                 
52         x = (unsigned char*) &test;
53
54         if (*x == 0x00) {
55                 if (macro)
56                         printf("__BIG_ENDIAN_BITFIELD\n");
57                 else
58                         printf("big endian\n");
59         }
60         else if (*x == 0x01) {
61                 if (macro)
62                         printf("__LITTLE_ENDIAN_BITFIELD\n");
63                 else
64                         printf("little endian\n");
65         } else {
66                 printf("\nWARNING!!! byteorder exception\n\n");
67                 debug = 1;
68         }
69
70         if (debug) {
71                 printf("sizeof(unsigned int) = %d\n", sizeof(unsigned int));
72                 printf("unsigned int test = 1;\n");
73                 printf("in memory as: ");
74                 for (j = 0; j < sizeof(unsigned int); j++)
75                         printf("%02x ", x[j]);
76                 printf("\n");
77         }
78         return 0;
79 }