#include <stdio.h>

/*
 * Header prepended to each Atari ST .prg file
 */
struct aexec {
	 short	a_magic;	/* magic number */
unsigned long	a_text;		/* size of text segment */
unsigned long	a_data;		/* size of initialized data */
unsigned long	a_bss;		/* size of uninitialized data */
unsigned long	a_syms;		/* size of symbol table */
unsigned long	a_AZero1;	/* always zero */
unsigned long	a_AZero2;	/* always zero */
unsigned short	a_isreloc;	/* is reloc info present */
};
#define	CMAGIC	0x601A		/* contiguous text */
#define	ISRELOCINFO	0	/* relocation information is present */
				/*  any other value - no reloc info  */

extern FILE *fopen();

main(argc, argv)
int argc;
char **argv;
{
#ifdef atarist
	_binmode(1);
#endif
	while(--argc > 0)
	    showfile(*++argv);
	return 0;
}

showfile(name)
char *name;
{
	register FILE *fp;
	struct aexec h;
	
	if((fp = fopen(name, "r")) == (FILE *)NULL)
	{
		perror(name);
		exit(1);
	}
# ifndef WORD_ALIGNED
	if(fread(&h, sizeof(struct aexec), 1, fp) != 1)
	{
		perror(name);
		exit(2);
	}
#else
	aligned_read(&h, fp);
#endif
	fclose(fp);
	
	if(h.a_magic != CMAGIC)
	{
		fprintf(stderr,"Bad Magic #: 0X%x\n", h.a_magic);
		exit(3);
	}
	printf("%s:\n\ttext size\t%ld\n\tdata size\t%ld\n\tbss size\t%ld\
\n\tsymbol size\t%ld\n\tFile %s relocatable\n\
\n\t%s\n", name, h.a_text,
	 h.a_data, h.a_bss, h.a_syms,
	(h.a_isreloc == ISRELOCINFO)? "is":"is not",
	(h.a_AZero2 & 1)? "Only BSS cleared on startup" :
			 "BSS and high mem cleared on startup");
}

# ifdef WORD_ALIGNED
void ckfread(buf, siz, n, fp)
char *buf;
int siz, n;
FILE *fp;
{
	if(fread(buf, siz, n, fp) != n)
	{
	    perror("reading");
	    exit(3);
	}
}

aligned_read(h, fp)
struct aexec *h;
FILE *fp;
{
	short i;
	long j;

	ckfread(&i, 2, 1, fp);
	h->a_magic = i;
	ckfread(&j, 4, 1, fp);
	h->a_text = j;
	ckfread(&j, 4, 1, fp);
	h->a_data = j;
	ckfread(&j, 4, 1, fp);
	h->a_bss = j;
	ckfread(&j, 4, 1, fp);
	h->a_syms = j;
	ckfread(&j, 4, 1, fp);
	h->a_AZero1 = j;
	ckfread(&j, 4, 1, fp);
	h->a_AZero2 = j;
	ckfread(&i, 2, 1, fp);
	h->a_isreloc = i;
}
#endif

#ifdef CROSSHPUX
char *xmalloc(n)
unsigned n;
{
    extern char *malloc();
    char *ret = malloc(n);
    
    if(!ret)
    {
        fprintf(stderr,"Out of memory!\n");
        exit(1);
    }
    return ret;
}
#endif
