/************************************************************************
*                       - INFOFLEX System -
* This source code is CONFIDENTIAL and PROPRIETARY to INFOFLEX ,
* INC. . The information contained herein may not be used, copied or
* disclosed in whole or in part except as permitted by written agreement
* signed by an officer of INFOFLEX,INC..
*
* COPYRIGHT (c) 1986 INFOFLEX , INC., BURLINGAME, CALIFORNIA.
* ALL RIGHTS RESERVED.
*************************************************************************/


/*  This programs massages the output from cflow  */

#include <stdio.h>
#define YES 1
#define NO  0 

static char inbuf[1024];

#include <fcntl.h>
#ifdef UNIX
#   define PERMMODE 0664
#   define O_BINARY 0x0000
#endif
#ifdef MSDOS
#   include <sys\stat.h>
#   define PERMMODE  (_S_IREAD | _S_IWRITE) 
#   define O_BINARY 0x8000
#endif

#define JOIN  1
#define SPLIT 2

static char joinfilename[50];

void main( argc, argv)
int argc;
char *argv[];
{
    if (argc < 2) {
       printf("split [join] filename\n");
       exit(1);
       }

    if (strcmp(argv[1], "join") == 0) {
       if (argc < 3) {
          printf("split [join] filename\n");
          exit(1);
          }
       strcpy(joinfilename, argv[2]);
       joinfile();
       exit(0);
       }
 
   strcpy(joinfilename, argv[1]);
   splitfile();
   exit(0);
}



splitfile()
{
    long readcount;
    int splitcount;
    int readsize;
    int writesize;
    int fdjoin;
    int fdsplit;
    char splitname[50];

    if ( (fdjoin = open(joinfilename, O_RDONLY | O_BINARY, PERMMODE)) < 0)  {
        printf("Invalid filename %s\n", joinfilename);
        return(0);
        }

    for (splitcount=0;;++splitcount) {
        sprintf(splitname, "xx%d", splitcount); 
        if ( (fdsplit = open(splitname, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, PERMMODE)) < 0)  {
            printf("Open Error %s\n", splitname);
            break;
            }
        readcount = 0L;
        for (;readcount < 1399 ;++readcount) {
            readsize  = read(fdjoin, inbuf, sizeof(inbuf));
            writesize = write(fdsplit, inbuf, readsize);
            if (readsize < sizeof(inbuf)) 
               break;
            }
        close(fdsplit);
        if (readsize < sizeof(inbuf)) 
           break;
        }

    close(fdjoin);
} /* main */

joinfile()
{
    long readcount;
    int splitcount;
    int readsize;
    int writesize;
    int fdjoin;
    int fdsplit;
    char splitname[50];

    if ( (fdjoin = open(joinfilename, O_RDWR | O_CREAT | O_BINARY, PERMMODE)) < 0)  {
        printf("Open error %s\n",joinfilename);
        return(0);
        }

    for (splitcount=0;;++splitcount) {
        sprintf(splitname, "xx%d", splitcount); 
        if ( (fdsplit = open(splitname, O_RDONLY | O_BINARY, PERMMODE)) < 0)  {
            break;
            printf("Open Error %s\n", splitname);
            break;
            }
        for (;;) {
            readsize  = read(fdsplit, inbuf, sizeof(inbuf));
            writesize = write(fdjoin, inbuf, readsize);
            if (readsize < sizeof(inbuf)) 
               break;
            }
        close(fdsplit);
        unlink(splitname);
        }

    close(fdjoin);
} /* main */





