#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

static int in,out,count,p,lastp,len;
static unsigned char buf[40000];

generate(int count, int from, int to)
{
  char fn[100];
  sprintf(fn, "post198split%02d.flx", count);
  out=open(fn, O_CREAT|O_WRONLY|O_TRUNC, 0664);
  if(out<0)
  {
    fprintf(stderr, "Can't open file %s for writing.\n", fn);
    exit(-1);
  }
  write(out, buf+from, to-from+1);
  close(out);
}

main()
{

  in=open("post198.flx",O_RDONLY);
  if(in<0)
  {
    fprintf(stderr, "Can't open post198.flx.\n");
    exit(-1);
  }

  len=read(in, buf, sizeof(buf));
  close(in);

  count=1;
  lastp=0;
  for(p=1; p<len-4; p++)
  {
    if(buf[p  ]==buf[0] &&
       buf[p+1]==buf[1] &&
       buf[p+2]==buf[2] &&
       buf[p+3]==buf[3])
    {
      generate(count++, lastp, p-1);
      lastp=p;
    }
  }
  generate(count, lastp, len-1);
}

