/contrib/ntp/util/byteorder.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 56 lines · 42 code · 4 blank · 10 comment · 22 complexity · ec52fc2b6c58d763ee9e463726bf464f MD5 · raw file

  1. /*
  2. * This works on:
  3. * Crays
  4. * Conven
  5. * sparc's
  6. * Dec mip machines
  7. * Dec alpha machines
  8. * RS6000
  9. * SGI's
  10. */
  11. #include <stdio.h>
  12. int
  13. main(
  14. int argc,
  15. char *argv[]
  16. )
  17. {
  18. int i;
  19. int big;
  20. union {
  21. unsigned long l;
  22. char c[sizeof(long)];
  23. } u;
  24. #if defined(LONG8)
  25. u.l = (((long)0x08070605) << 32) | (long)0x04030201;
  26. #else
  27. u.l = 0x04030201;
  28. #endif
  29. if (sizeof(long) > 4) {
  30. if (u.c[0] == 0x08) big = 1;
  31. else big = 0;
  32. } else {
  33. if (u.c[0] == 0x04) big = 1;
  34. else big = 0;
  35. }
  36. for (i=0; i< sizeof(long); i++) {
  37. if (big == 1 && (u.c[i] == (sizeof(long) - i))) {
  38. continue;
  39. } else if (big == 0 && (u.c[i] == (i+1))) {
  40. continue;
  41. } else {
  42. big = -1;
  43. break;
  44. }
  45. }
  46. if (big == 1) {
  47. printf("XNTP_BIG_ENDIAN\n");
  48. } else if (big == 0) {
  49. printf("XNTP_LITTLE_ENDIAN\n");
  50. }
  51. exit(0);
  52. }