/* will read a file consisting of 32-bit integers, truncate them to 
16-bit integers and write out a new file.
syntax is:    convert infilename outfilename
will work with any size file */

#include <stdio.h>
#include <sys/file.h>
#define BUFSIZE 32

main(argc,argv)
char *argv[];
{
	int i,fd,outfd,number[BUFSIZE],nbytes,numberread;
	short out[BUFSIZE];
	char *inname,*outname;

	inname = argv[1];
	outname = argv[2];
	printf("reading %s, writing %s\n",inname,outname);
	if((fd = open(inname,0)) <= 0) {
		printf("Can't open file\n");
		exit(-1);
	}
	if((outfd = open(outname,O_CREAT|O_RDWR,0644)) <= 0) {
		printf("Can't open outputfile\n");
		exit(-1);
	}
	while(1) {
		if((numberread = 
		    read(fd,(char *)number,BUFSIZE * sizeof(int))) <= 0) {
			printf("reached eof on file\n");
			exit(-1);
		}
		for(i=0; i<numberread/sizeof(int); i++) out[i] = number[i];
		if(write(outfd,(char *)out, numberread/2) <= 0) {
			printf("trouble writing to file\n");
			exit(-3);
		}
	}
}
