/*
 * XSplit - Split a file into more <smaller> files.
 *            
 *
 * Copyright © 1995 The Xperts Group Inc. All Rights Reserved.
 * Author: Manolis S Pappas.
 * Version: 1.1a
 *
 *
 */

#include <stdio.h>
#include <string.h>

#define VERSION "1.1a"
#define EXIT_FAILURE 1

static const char versid[]="$VER: XSplit v1.1a (7.1.96)";

FILE *open_vol(name, no)
char *name;
long no;
{
  char work[1024];

  if (no)
    sprintf(work, "%s.%02d", name, no);
  else
    sprintf(work, "%s.000", name);

  return fopen(work, "wb");
}

int main(argc, argv)
int argc;
char **argv;
{
  char buffer[1024];              /* Small I/O buffer */
  FILE *in, *out;
  long size, i, j = 1024, vol_no = 0;

  if (argc != 4) {
    printf("XSplit v%s\n",VERSION);
    printf("Copyright (©) 1995-96, The Xperts Group Inc.\n");
    printf("All Rights Reserved Worldwide.\n");
    printf("Author: Manolis S Pappas.\n\n");
    printf("Usage: %s <basefilesize in KB> <infile> <outbasename>\n", argv[0]);
    exit(EXIT_FAILURE);
  }

  size = atoi(argv[1]);

  if (in = fopen(argv[2], "rb")) {
    while (!feof(in)) {
      if (out = open_vol(argv[3], vol_no++)) {
        i = size;

        while((i--) && (j == 1024)) {
          j = fread(buffer, 1, 1024, in);
          fwrite(buffer, 1, j, out);
        }
        fclose(out);
      } else {
        fclose(in);
        printf("Error opening volume %d!\n\n", vol_no);
        exit(EXIT_FAILURE);
      }
    }
  } else {
    printf("Error opening source!!\n\n");
    exit(EXIT_FAILURE);
  }

  return 0;
}
