[comp.sources.misc] v09i043: Byteord: simple program to determine byte order

allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc) (12/05/89)

Posting-number: Volume 9, Issue 43
Submitted-by: gwollman%JHUNIX.BITNET@JHMAIL.HCF.JHU.EDU
Archive-name: byteord

Here is a short program which has exactly one purpose:  it tells you
what byte-order your machine uses.  You can probably look this up in a
manual, but it's nice to have a program that anyone can use, to tell
them what the order is.
This is useful for programs, such as the recently posted Unzip,
which need to know the byte order before compilation.

--------cut here--------------
#! /bin/sh
# This file was wrapped with "dummyshar".  "sh" this file to extract.
# Contents:  byteord.c
echo extracting 'byteord.c'
if test -f 'byteord.c' -a -z "$1"; then echo Not overwriting 'byteord.c'; else
sed 's/^X//' << \EOF > 'byteord.c'
Xextern int printf();
X#define LONG_VAL (long)0x12345678
X#define I_LONG_L (short)0x5678
X#define SHORT_VAL  (short)0xcdae
X#define I_SHORT_L  (char)0xae
X
X
Xlong something = LONG_VAL;
Xshort sth_else = SHORT_VAL;
X
Xmain() {
X        char *p = (char *)&sth_else;
X	short *p2 = (short *)&something;
X
X	printf("Your byte order is: ");
X	if(*p==I_SHORT_L)
X		printf("LSB first (Intel).\n");
X	else
X		printf("MSB first (non-Intel).\n");
X	printf("Your word [16-bit] order is: ");
X	if(*p2==I_LONG_L)
X		printf("LSW first (Intel).\n");
X	else
X		printf("MSW first (non-Intel).\n");
X	printf("Evidence: %02x (char) %04x (short)\n",
X		(unsigned short)*p,*p2);
X}
EOF
chars=`wc -c < 'byteord.c'`
if test $chars !=      634; then echo 'byteord.c' is $chars characters, should be      634 characters!; fi
fi
exit 0
-- 
|An intellectual is a man who can't do arithmetic with his shoes on, and is  |
|is proud of the lack.                          -Jubal Harshaw               |
   Garrett A. Wollman, Box 2289, JHU, 34th and Charles, Baltimore MD 21218
#include <disclaimer.h>               /* +1 301 338 5792 */