home *** CD-ROM | disk | FTP | other *** search
- /*
- * make endiandef.h include file for this host
- *
- * Andy Duplain, August 1992.
- * Niklas Röjemo, April 1994 added possibility to use long if int is to small
- */
- #include <stdlib.h>
- #include <stdio.h>
-
- union {
- int i;
- char c[4];
- } wordInt;
-
- union {
- long i;
- char c[4];
- } wordLong;
-
- int main(void)
- {
- FILE *ofp;
- register i;
-
- if (sizeof(int) != 4 && sizeof(long) != 4) {
- fprintf(stderr, "Sorry pal, you cannot use this program on this host (sizeof(int) and sizeof(long) != 4)\n");
- exit(1);
- }
- #ifdef UNIX
- ofp = fopen("endiandef.h", "w");
- #else
- ofp = fopen("h.endiandef", "w");
- #endif
- if (!ofp) {
- perror("endiandef.h");
- exit(1);
- }
-
- fprintf(ofp, "/*\n * endiandef.h\tdefines for local host byte sex.\n */\n");
- fprintf(ofp, "#ifndef __ENDIANDEF_H\n#define __ENDIANDEF_H\n");
-
- if(sizeof(int)==4) {
- fprintf(ofp, "typedef unsigned int WORD;\n");
- wordInt.i = 0x01020304;
-
- for (i = 0; i < 4; i++) {
- fprintf(ofp, "#define BYTE%dSHIFT %d\n", i, (wordInt.c[i] - 1) * 8);
- }
-
- if (wordInt.c[0] == 1 && wordInt.c[1] == 2 && wordInt.c[2] == 3 && wordInt.c[3] == 4)
- fprintf(ofp, "#define BIGENDIAN\n");
- else
- fprintf(ofp, "#undef BIGENDIAN\n");
-
- if (wordInt.c[0] == 4 && wordInt.c[1] == 3 && wordInt.c[2] == 2 && wordInt.c[3] == 1)
- fprintf(ofp, "#define LITTLEENDIAN\n");
- else
- fprintf(ofp, "#undef LITTLEENDIAN\n");
- fprintf(ofp, "#endif /* __ENDIANDEF_H */\n");
- fclose(ofp);
- exit(0);
- }
- if(sizeof(long)==4) {
- fprintf(ofp, "typedef unsigned long WORD;\n");
- wordLong.i = 0x01020304;
-
- for (i = 0; i < 4; i++) {
- fprintf(ofp, "#define BYTE%dSHIFT %d\n", i, (wordLong.c[i] - 1) * 8);
- }
-
- if (wordLong.c[0] == 1 && wordLong.c[1] == 2 && wordLong.c[2] == 3 && wordLong.c[3] == 4)
- fprintf(ofp, "#define BIGENDIAN\n");
- else
- fprintf(ofp, "#undef BIGENDIAN\n");
-
- if (wordLong.c[0] == 4 && wordLong.c[1] == 3 && wordLong.c[2] == 2 && wordLong.c[3] == 1)
- fprintf(ofp, "#define LITTLEENDIAN\n");
- else
- fprintf(ofp, "#undef LITTLEENDIAN\n");
- fprintf(ofp, "#endif /* __ENDIANDEF_H */\n");
- fclose(ofp);
- exit(0);
- }
-
- }
-