/* $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

#ident	"$Header: striphdr.c 1.3 89/08/07 $"

/* $Log:	striphdr.c,v $
 */

/*
 * striphdr - strip out the COFF section headers from a binary
 *
 * This must be done in as portable a manner as possible, since the same
 * program must be used in the cross-build environment (initially Dynix-BSD) as
 * well as the native (DYNIX/V) environment.
 */

#include <stdio.h>
#include <a.out.h>
#include <fcntl.h>
#include <string.h>

#define	USAGE	"%s: usage: striphdr COFFfile [ 8Kbootstrap ]\n"
#define TMPNAM	"/tmp/strphXXXXX"

void	perror(), exit();
long	lseek();

struct filehdr f;
struct aouthdr a;
struct scnhdr  s;
char *name, *tmpname;
char rawimage[8192];

main(argc, argv)
int	argc;
char	** argv;
{
	int pid, fd, tfd, nosecs, n;
	long currloc;
	char *file, *outfile, *ptr;

	name = *argv;
	argc--; argv++;

	if (argc != 1 && argc != 2) {
		(void)fprintf(stderr, USAGE, name);
		exit(1);
	}

	file = *argv;
	argc--; argv++;
	if (argc != 0)
		outfile = *argv;
	else
		outfile = file;
	if ((fd = open(file, O_RDONLY)) < 0) {
		(void)fprintf(stderr, "%s: ", name);
		perror(file);
		exit(1);
	}

	/*
	 * very simple-minded approach to temp naming, but method needs to
	 * be portable (ie, can't use tmpnam(3S)).
	 */
	tmpname = TMPNAM;
	pid = getpid();
	ptr = strchr(tmpname, 'X');
	(void)sprintf(ptr, "%d", pid);
	if (access(tmpname, 0) == 0) {
		(void)fprintf(stderr, "%s: tmp file exists", name);
		exit(1);
	}
	if ((tfd = creat(tmpname, 0644)) < 0) {
		(void)fprintf(stderr, "%s: ", name);
		perror(file);
		exit(1);
	}

	/*
	 * read the filehdr and the aouthdr
	 */
	if (read(fd, &f, sizeof (struct filehdr)) != sizeof(struct filehdr))
		sread(file);
	
	if (read(fd, &a, sizeof(struct aouthdr)) != sizeof(struct aouthdr))
		sread(file);

#ifdef notdef
	/*
	 * Now write these out to the temp file
	 */
	if (write(tfd, &f, sizeof(struct filehdr)) != sizeof(struct filehdr))
		swrite(tmpname);

	if (write(tfd, &a, sizeof(struct aouthdr)) != sizeof(struct aouthdr))
		swrite(tmpname);
#endif

	/*
	 * loop through the section headers and write the raw data
	 * of all text and data sections to the tmp file.
	 */
	for (nosecs = f.f_nscns; nosecs; nosecs--) {
		if (read(fd, &s, sizeof(struct scnhdr)) != sizeof(struct scnhdr))
			sread(file);

		if ((currloc = lseek(fd, 0, 1)) < 0) {
			(void)fprintf(stderr, "%s: ", name);
			perror(file);
			exit(1);
		}

		if ((s.s_flags & (STYP_TEXT|STYP_DATA)) == 0)
			continue;

		if (lseek(fd, s.s_scnptr, 0) < 0) {
			(void)fprintf(stderr, "%s: cannot seek on ", name);
			perror(file);
			(void)unlink(tmpname);
			exit(1);
		}

		if (read(fd, rawimage, (unsigned)s.s_size) != s.s_size)
			sread(file);
		
		if (write(tfd, rawimage, (unsigned)s.s_size) != s.s_size)
			swrite(tmpname);

		if (lseek(fd, currloc, 0) < (long)0) {
			(void)fprintf(stderr, "%s: ", name);
			perror(file);
			(void)unlink(tmpname);
			exit(1);
		}
	}
	(void)close(fd);
	(void)close(tfd);

	if ((fd = creat(outfile, 0644)) < 0) {
		(void)fprintf(stderr, "%s: ", name);
		perror(file);
		(void)unlink(tmpname);
		exit(1);
	}
	if ((tfd = open(tmpname, O_RDONLY)) < 0) {
		(void)fprintf(stderr, "%s: ", name);
		perror(file);
		(void)unlink(tmpname);
		exit(1);
	}

	while ((n = read(tfd, rawimage, (unsigned)8192)) > 0) {
		if (write(fd, rawimage, (unsigned)n) < 0)
			swrite(file);
	}

	if (n < 0)
		sread(tmpname);
	
	(void)close(fd);
	(void)close(tfd);
	(void)unlink(tmpname);
	exit(0);
	/*NOTREACHED*/
}

swrite(fi)
char *fi;
{
	(void)fprintf(stderr, "%s: ", name);
	perror(fi);
	(void)unlink(tmpname);
	exit(1);
}

sread(fi)
char *fi;
{
	(void)fprintf(stderr, "%s: ", name);
	perror(fi);
	(void)unlink(tmpname);
	exit(1);
}
