/* Splits a binary file into 1Mb chunks */ /* Note: -m flag makes it split into 1.4 Mb chunks (26/7/99 AJD)*/ #include #define BUFFER_SIZE 16384 /* 2^14 bytes */ #define ONE_MEGABYTE 1048576 /* 64 buffers (2^20 bytes) */ #define MAX_FILE_SIZE 1409024 /* 86 buffers (86*2^14 bytes) */ int BUFFERS_PER_FILE; main (argc, argv) int argc; char* argv[]; { FILE* infile; FILE* outfile = NULL; char * infilename; char outfilename [255]; char buffer [BUFFER_SIZE]; int finished = 0; int file_number = 0; int buffer_number = 0; int bytes_read; int bytes_written; if (argc<2 || argc>4) { fprintf(stderr,"\nUSAGE: %s [ -m | -s ] filename\n\n" " -m = Use maximum size chunks which can fit on a floppy disk\n" " -s n = Use chunks which are n megabytes in size\n\n" " Default chunk size is 1 MB\n\n",argv[0]); exit(1); } if (!strcmp(argv[1],"-m")) { infilename=argv[2]; BUFFERS_PER_FILE = MAX_FILE_SIZE/BUFFER_SIZE; } else if (!strcmp(argv[1],"-s")) { int nMBChunks = atoi(argv[2]); infilename=argv[3]; BUFFERS_PER_FILE = (nMBChunks*ONE_MEGABYTE)/BUFFER_SIZE; } else { infilename=argv[1]; BUFFERS_PER_FILE = ONE_MEGABYTE/BUFFER_SIZE; } if ((infile=fopen(infilename,"rb"))==NULL) { fprintf(stderr,"Could not open %s\n",infilename); } file_number++; sprintf(outfilename,"%s-%d",infilename,file_number); printf("Creating %s.\n",outfilename); if ((outfile=fopen(outfilename,"wb"))==NULL) { fprintf(stderr,"Could not open %s for write.\n",outfilename); fclose(infile); exit(1); } buffer_number = 0; while (!finished) { bytes_read = fread(buffer, sizeof(char), BUFFER_SIZE, infile); if (bytes_read==0) { if (outfile!=NULL) fclose(outfile); fclose(infile); finished=1; } else { if (buffer_number>=BUFFERS_PER_FILE) { fclose(outfile); outfile=NULL; file_number++; sprintf(outfilename,"%s-%d",infilename,file_number); printf("Creating %s.\n",outfilename); if ((outfile=fopen(outfilename,"wb"))==NULL) { fprintf(stderr,"Could not open %s for write\n",outfilename); fclose(infile); exit(1); } buffer_number = 0; } bytes_written = fwrite(buffer, sizeof(char), bytes_read, outfile); if (bytes_written!=bytes_read) { fprintf(stderr,"bytes_written!=bytes_read...\n"); } buffer_number++; } } if (outfile!=NULL) fclose(outfile); fclose(infile); }