home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / program / language / as / source / c / mkendian < prev    next >
Encoding:
Text File  |  1994-05-23  |  2.1 KB  |  86 lines

  1. /*
  2.  * make endiandef.h include file for this host
  3.  * 
  4.  * Andy Duplain, August 1992.
  5.  * Niklas Röjemo, April 1994 added possibility to use long if int is to small
  6.  */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9.  
  10. union {
  11.   int i;
  12.   char c[4];
  13. } wordInt;
  14.  
  15. union {
  16.   long i;
  17.   char c[4];
  18. } wordLong;
  19.  
  20. int main(void)
  21. {
  22.   FILE *ofp;
  23.   register i;
  24.  
  25.   if (sizeof(int) != 4 && sizeof(long) != 4) {
  26.     fprintf(stderr, "Sorry pal, you cannot use this program on this host (sizeof(int) and sizeof(long) != 4)\n");
  27.     exit(1);
  28.   }
  29. #ifdef UNIX
  30.   ofp = fopen("endiandef.h", "w");
  31. #else
  32.   ofp = fopen("h.endiandef", "w");
  33. #endif
  34.   if (!ofp) {
  35.     perror("endiandef.h");
  36.     exit(1);
  37.   }
  38.  
  39.   fprintf(ofp, "/*\n * endiandef.h\tdefines for local host byte sex.\n */\n");
  40.   fprintf(ofp, "#ifndef __ENDIANDEF_H\n#define __ENDIANDEF_H\n");
  41.  
  42.   if(sizeof(int)==4) {
  43.     fprintf(ofp, "typedef unsigned int WORD;\n");
  44.     wordInt.i = 0x01020304;
  45.  
  46.     for (i = 0; i < 4; i++) {
  47.       fprintf(ofp, "#define BYTE%dSHIFT %d\n", i, (wordInt.c[i] - 1) * 8);
  48.     }
  49.  
  50.     if (wordInt.c[0] == 1 && wordInt.c[1] == 2 && wordInt.c[2] == 3 && wordInt.c[3] == 4)
  51.       fprintf(ofp, "#define BIGENDIAN\n");
  52.     else
  53.       fprintf(ofp, "#undef BIGENDIAN\n");
  54.  
  55.     if (wordInt.c[0] == 4 && wordInt.c[1] == 3 && wordInt.c[2] == 2 && wordInt.c[3] == 1)
  56.       fprintf(ofp, "#define LITTLEENDIAN\n");
  57.     else
  58.       fprintf(ofp, "#undef LITTLEENDIAN\n");
  59.     fprintf(ofp, "#endif /* __ENDIANDEF_H */\n");
  60.     fclose(ofp);
  61.     exit(0);
  62.   }
  63.   if(sizeof(long)==4) {
  64.     fprintf(ofp, "typedef unsigned long WORD;\n");
  65.     wordLong.i = 0x01020304;
  66.  
  67.     for (i = 0; i < 4; i++) {
  68.       fprintf(ofp, "#define BYTE%dSHIFT %d\n", i, (wordLong.c[i] - 1) * 8);
  69.     }
  70.  
  71.     if (wordLong.c[0] == 1 && wordLong.c[1] == 2 && wordLong.c[2] == 3 && wordLong.c[3] == 4)
  72.       fprintf(ofp, "#define BIGENDIAN\n");
  73.     else
  74.       fprintf(ofp, "#undef BIGENDIAN\n");
  75.  
  76.     if (wordLong.c[0] == 4 && wordLong.c[1] == 3 && wordLong.c[2] == 2 && wordLong.c[3] == 1)
  77.       fprintf(ofp, "#define LITTLEENDIAN\n");
  78.     else
  79.       fprintf(ofp, "#undef LITTLEENDIAN\n");
  80.     fprintf(ofp, "#endif /* __ENDIANDEF_H */\n");
  81.     fclose(ofp);
  82.     exit(0);
  83.   }
  84.  
  85. }
  86.