/* Splitter - split file p1 and create file p2 containing p3 bytes
	from the p4th byte of the input file.
        In order to split GNUCC file from Chip Blach - M.C. 94/3/6. */

#define NOTEST
#include <stdio.h>
/*#include <conio.h>*/
#include <stdlib.h>

int main(int argc, char *argv[])
{
char *stopstring;
unsigned long ul1, ul2;
unsigned long counti, counto, i, j, skip;
int base, count;
char letter;

FILE *fpi, *fpo;


/* Set up the parms */

#ifdef TEST
  for (count=0; count < argc; count++)
      printf("argc=%i, argv=%s\n", count, argv[count]);
      getch();
#endif

count=0; counti=0; counto=0; skip=0;
base=10;

ul1 = strtoul( argv[3], &stopstring, base);
ul2 = strtoul( argv[4], &stopstring, base);

printf("SPLITTER will write %ld bytes from byte %ld.\n", ul1, ul2);
printf("SPLITTER will create file %s from input file %s.\n",
        argv[2], argv[1]);

if ((fpi = fopen (argv[1],"rb")) == NULL)
        {
        printf ("Error opening input file %s\n", argv[1]);
        exit(98);
        }
else printf("Open fpi value %x\n", fpi);

if ((fpo = fopen (argv[2],"wb")) == NULL)
        {
        printf ("Error opening output file %s!\n", argv[2]);
        exit(99);
        }
else printf("Open fpo value %x\n", fpo);

i = j = 0;

for (;;)
        {
        letter = fgetc(fpi);
        counti = counti + 1;
        #ifdef TEST
        	printf("Char read was %c: %x; counti was %d\n", letter, letter,
        	counti);
        	printf("EOF is %x\n", EOF);
      		printf("eof is %x\n", feof(fpi));
        #endif

        if ( feof(fpi)) break;

        if ((counti >= ul2) && (counto < ul1))
                {
     		counto = counto + 1;
      		fputc (*(&letter), fpo);
      		#ifdef TEST
                         printf("counto was %d\n", counto);
        		 getch();
        	#endif
                }
        }

fclose(fpi);
fclose(fpo);

printf("Input file count was %ld, output file count was %ld.\n",
	 counti, counto);

exit(0);
}
