_Building Software for Portability_
by Greg Blackham

[EXAMPLE 1]


/*	MACHINE.H                                      */
/*	MACHINE DEPENDENT DATA TYPE DEFINITIONS	       */
/*	Copyright (C) 1987			       */
/*	WordPerfect Corp.                              */

#ifdef	IBM-PC
	typedef	unsigned char	BYTE;    /* unsigned,  8 bits */
	typedef	int             WORD;    /* signed,   16 bits */
	typedef	unsigned        UWORD;   /* unsigned, 16 bits */
	typedef	long		DWORD;   /* signed,   32 bits */
	typedef	unsigned long	UDWORD;	 /* unsigned, 32 bits */
#endif

#ifdef HP
	typedef unsigned char   BYTE;     /* unsigned, 8 bits */
	typedef short	        WORD;     /* signed,  16 bits */
	typedef unsigned short	UWORD;	  /* unsigned, 16 bits */
	typedef int             DWORD;    /* signed,   32 bits */
	typedef unsigned int	UDWORD;   /* unsigned, 32 bits */
#endif

#ifdef	ATT6386
	typedef unsigned char	BYTE;     /* unsigned,  8 bits */
	typedef short	        WORD;     /* signed,   16 bits */
	typedef unsigned short	UWORD;    /* unsigned, 16 bits */
	typedef int             DWORD;    /* signed,   32 bits */
	typedef unsigned int	UDWORD;   /* unsigned, 32 bits */
#endif

. . .

[EXAMPLE 2]

/*******************************************************************
 * This example program writes a data structure directly to disk.  *
 * Only five bytes of data are significant, but more than 5 bytes  *
 * are written to the file because of word alignment.  The         *
 * component bytes of the integer data are also written in a       *
 * different order depending on machine architecture.              *
 *******************************************************************/

#include <fcntl.h>
#include "machine.h"

/* 5 byte structure definition */
/* word alignment may occur between elements 1 and 2! */

struct {
	BYTE element1;
	UDWORD element2;
} s = {'A',             /* recognizable values for each byte */
        0x08040201};    /* each byte represented by 2 hex digits */

main()
{
	int fd;
	fd = open("data.fil",O_RDWR | O_CREAT,0666);
	write(fd,(char *)&s,sizeof(s));   /* open and write file */
	close(fd);
}

Contents of data.fil created on HP 9000 Model 350:

65,             This is the "A"
0,     This byte is inserted by the compiler for word alignment
8,     Bytes of element2 stored with the highest order byte first
4,
2,		
1               Lowest order byte of element2

Contents of data.fil created on AT&T 6386:

65,             This is the "A"
0,     These 3 bytes are inserted by the compiler for word alignment
0,     The 6386 compiler aligns on 4-byte boundaries
0,
1,     Bytes of element2 stored with the highest order byte first
2,
4,
8              Highest order byte of element2


[EXAMPLE 3]

/********************************************************************
 * This new version of the example program writes the same data	    *
 * structure to disk in a portable way.  It creates a memory image  *
 * of what the disk file should look like, then writes the file     *
 * in one block.  When writing the integer data the bytes are       *
 * written starting with the least significant byte.                *
 *******************************************************************/

#include <fcntl.h>
#include "machine.h"

/* 5 byte structure definition */
/* word alignment may occur between elements 1 and 2! */

struct {
	BYTE element1;
	UDWORD element2;       /* 4 byte integer */
} s = {'A',                    /* recognizable values for each byte */
		0x08040201};   /* each byte represented by 2 hex digits */

main()
{
	int fd,               /* file descriptor */
		i;            /* counter variable */
	BYTE array[5];	      /* output buffer */
	UDWORD tmp;           /* a 4-byte integer */

	array[0] = s.element1;   /* 1-byte variables are easy */
	tmp = s.element2;        /* Don't clobber the real data */
	for (i=1; i<= 4; i++) {  /* put each byte of the UDWORD */
		array[i] = (BYTE)(0xff & tmp);	/* in separately */
		tmp >>= 8;       /* get next 8-bits */
	}
	
	fd = open("data.fil",O_RDWR | O_CREAT,0666);	
	write(fd,array,5);       /* open and write file */
	close(fd);
}

Contents of data.fil created on all machines using this code:

65,          This is the "A"
1,     Bytes of element2 stored with the lowest order byte first
2,
4,		
8	     Lowest order byte of element2


[END OF BLACKHAM]

