/*---------------------------------------------------------------------------
 *		
 * NAME:	od11
 *
 * USAGE:	od11 <filename>
 *
 * DESCRIPTION:	Produces an octal dump of a pdp11 executable, without
 *		the leading 8 (short) words of size information.
 *
 * ARGUMENTS:	<filename> -- name of the file to be dumped. It is
 *		assumed that the file is stored in pdp11 byte order
 *		(i.e., littleEndian).
 *
 * AUTHOR/DATE:	John Lloyd, at USC, March 11, 1990
 *
 * UPDATES:
 *
 *--------------------------------------------------------------------------*/

#include <stdio.h>

char *filename = NULL;
char **argvec;
int bigEndianHost;

testForBigEndian()
{
	union {
	  unsigned short s;
	  unsigned char c[2];
	  } swap;

	swap.s = 1;
	return (swap.c[1] == 1);
}

unsigned short swapShort (s)
unsigned short s;
{
	union {
	  unsigned short s;
	  unsigned char c[2];
	  } swap;
	unsigned char tmp;

	swap.s = s;
	tmp = swap.c[0];
	swap.c[0] = swap.c[1];
	swap.c[1] = tmp;
	return (swap.s);
}

main(argc, argv)
int argc;
char **argv;
{
	FILE *fp;
	unsigned short word;
	int i, column;

	argvec = argv;
	argc--; argv++;
	while (argc > 0)
	 { if (filename == NULL)
	    { filename = *argv;
	    }
	   else
	    { argc = -1;
	      break;
	    }
	   argc--; argv++;
	 }
	if (argc == -1)
	 { fprintf (stderr, "Usage: %s <filename>\n", argvec[0]);
	   exit (-1);
	 }
	if ((fp = fopen (filename, "r")) == NULL)
	 { fprintf (stderr, "%s: can't open file %s\n", argvec[0], filename);
	   exit (-1);
	 }

	bigEndianHost = testForBigEndian();
	
	/* skip the leading eight word header */
	for (i=0; i<8; i++)
	 { fread (&word, sizeof(unsigned short), 1, fp);
	 }
	column = 0;
	while (fread (&word, sizeof(unsigned short), 1, fp))
	 { if (bigEndianHost)
	    { word = swapShort (word);
	    }
	   printf (" %06ho", word);
	   if (++column == 8)
	    { putchar ('\n');
	      column = 0;
	    }
	 }
	if (column != 0)
	 { putchar ('\n');
	 }
	exit (0);
}
