/**************************************************************************
*
*                           SplitFile
*
*                            Written
*                      By Thomas C. DeVeau
*                       November 11, 1990
*
*                     This is Public Domain
**************************************************************************/
#include <libraries/dosextens.h>

#define BUFFSIZE 256                /* buffer size */
#define MAXSPLITSIZE 51200L         /* max splitfile size - easier editing */

void main(argc,argv)
int argc;
char *argv[];
{
    struct FileHandle *fh1, *fh2, *Open();           /* file handles */
    int size1, size2, nfcnt = 0, pathok = 0;
    char buffer[BUFFSIZE],nfbuf[BUFFSIZE];  /* buffer holds command line filename */
                                            /* nfbuf holds splitfile names */
    char dir[80];                           /* path */

    switch(argc)
    {
        case 1:
            printf("Usage: SplitFile <file> [TO <path>]\n");
            exit(1);
        case 2:
            break;
        case 3:
            printf("SplitFile Error: Invalid Path specification.\n");
            exit(1);
        case 4:
            strcpy(&dir[0],argv[3]);
            pathok = 1;
            break;
        default:
            exit(1);
    }
    fh1 = Open(argv[1],MODE_OLDFILE);       /* open the original file */
    if(fh1 == 0)                            /* if won't open, show error */
    {
        printf("File \"%s\": Not Found.\n",argv[1]);
        exit(1);
    }

    for(;;)                                 /* loop forever until broken */
    {
        if(pathok)          /* get splitfile name */
            sprintf(&nfbuf[0],"%s%s.sf%d",dir,argv[1],nfcnt);
        else
            sprintf(&nfbuf[0],"%s.sf%d",argv[1],nfcnt);
        fh2 = Open(&nfbuf[0],MODE_NEWFILE);         /* open splitfile */
        size1 = size2 = 0;
        printf("Writing to file: %s\n",nfbuf);
        while(size1 < MAXSPLITSIZE)           /* check for splitfile size */
        {
            size2 = Read(fh1,buffer,BUFFSIZE); /* read from original file */
            Write(fh2,buffer,size2);           /* write to splitfile */
            if(size2 < BUFFSIZE)
                goto endit;      /* if read less than needed, end program */
            size1 += size2;      /* add sizes to get max size */
        }
        Close(fh2);              /* close the splitfile */
        nfcnt++;                 /* increment splitfile count */
    }
endit:
    if(fh2) Close(fh2);          /* if splitfile open, close it */
    Close(fh1);                  /* close original file */
    exit(1);                     /* end of program */
}



