home *** CD-ROM | disk | FTP | other *** search
- /**********************************************************************
- *
- * SPLIT: Program to split and recombine binary files
- *
- * USAGE:
- * SPLIT S Ni Complete_file Section_file_prefix
- * or SPLIT S Ki Complete_file Section_file_prefix
- * or SPLIT C Complete_file Section_file_list
- *
- * where
- * S indicates "split" (may be upper or lower case)
- * C indicates "combine" (may be upper or lower case)
- * Ni means "split into i files of equal size"
- * Ki means "split into files of size i Kilobytes"
- * Complete_file is the file to be split or recombined
- * Section_files are the component files after split or
- * to be recombined into the Complete_file
- * Section_file_prefix is the 8-character filename portion
- * of the Section_files. The extension will be
- * numbers generated by the program.
- * Section_file_list is a sequence of Section_files, each
- * separated by a space.
- *
- ***********************************************************************
- *
- * REVISION HISTORY:
- *
- * July 7, 1986: Created (Charlie Beerman)
- *
- **********************************************************************/
-
-
- #include <stdio.h>
- #include <ctype.h>
- #define FBUFSIZ 4096
- #define MAXFILES 99
- #define ERR (-1)
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
-
- /****** start of executable code ******/
-
- /* Check for at least 1 argument */
-
- if (argc < 2)
- userr("",0,0); /* Print usage & exit */
-
- /* Read first argument to decide action */
- switch (tolower(argv[1][0]))
- {
- case 'c': /* Combine */
- if (argc < 4)
- userr("Too few arguments",0,0);
- else
- combine(argc,argv);
- break;
- case 's': /* Split */
- if (argc < 5)
- userr("Too few arguments",0,0);
- else
- split(argc,argv);
- break;
- default:
- userr("Invalid first argument",0,0);
- break; /* Code should never get to this line */
- }
-
- } /* end of main program */
-
- /**********************************************************************
- *
- * USERR: Print error message and/or help info
- *
- * INPUTS:
- * errmsg Text to output (if of nonzero length)
- * file1 File to close (if nonzero)
- * file2 File to close (if nonzero)
- *
- **********************************************************************/
-
- userr(errmsg,file1,file2)
- char *errmsg;
- int file1, file2;
- {
- int status;
-
- /****** start of executable code ******/
-
- /* close files if necessary */
- if (file1 > 0)
- status = close(file1);
- if (file2 > 0)
- status = close(file2);
-
- /* print error message if there is one */
- if (strlen(errmsg) > 0)
- fprintf(stderr,"%s\n",errmsg);
- else
- fprintf(stderr,"%s%s%s%s",
- "Usage:\n",
- " SPLIT C create_file_name section_file_list\n",
- " or SPLIT S Ni source_file_name section_file_prefix\n",
- " or SPLIT S Ki source_file_name section_file_prefix\n");
-
- /* now exit to DOS with errorlevel 1 */
- exit(1);
-
- } /* end USERR */
-
-
- /**********************************************************************
- *
- * COMBINE: Combine a list of files into one file
- *
- * INPUTS:
- * argc Count of command-line arguments
- * argv List of command-line arguments
- *
- * OUTPUTS:
- * none
- *
- **********************************************************************/
-
- combine(argc,argv)
- int argc;
- char *argv[];
- {
- int i;
- int destfil, srcfil;
- int srcpmod = 0x8000;
- int destpmod = 0x8001;
- int nread, nwrote;
- char buf[FBUFSIZ];
-
- /****** start of executable code ******/
-
- /* open file to recombine */
- printf("Creating file %s\n",argv[2]);
- if ((destfil = creat(argv[2],destpmod)) == ERR)
- userr("Error in opening output file",0,0);
-
- /* loop through section files */
- for (i = 3; i < argc; ++i)
- {
-
- /* open source file */
- printf("Reading from file %s\n",argv[i]);
- if ((srcfil = open(argv[i],srcpmod)) == ERR)
- userr("Error in opening input file",destfil,0);
-
- /* loop through source file, reading a buffer at a time
- into the destination file */
- nread = FBUFSIZ;
- while (nread == FBUFSIZ)
- {
- if ((nread = read(srcfil,buf,FBUFSIZ)) == ERR)
- userr("Error in reading file",destfil,srcfil);
- else if (nread > 0)
- if ((nwrote = write(destfil,buf,nread))
- == ERR)
- userr("Error in writing file",
- destfil,srcfil);
- } /* end of while */
-
- /* now close source file */
- if ((srcfil = close(srcfil)) == ERR)
- userr("Error in closing source file",destfil,0);
-
- } /* end of loop over files in argument list */
-
- /* now close destination file */
- if ((destfil = close(destfil)) == ERR)
- userr("Error in closing destination file",0,0);
-
- } /* end of COMBINE */
-
- /**********************************************************************
- *
- * SPLIT: Split a file into components
- *
- * INPUTS:
- * argc count of arguments on command line
- * argv text of arguments on command line
- *
- **********************************************************************/
-
- split(argc,argv)
- int argc;
- char *argv[];
- {
- int i, nfiles;
- long maxsiz, srcsiz, destsiz, pos;
- long lseek();
- int destfil, srcfil;
- int srcpmod = 0x8000;
- int destpmod = 0x8001;
- int nread, nwrote, nbytes;
- char buf[FBUFSIZ], *p;
- char prefix[61], secname[65], numstr[3];
-
- /****** start of executable code ******/
-
- /* figure out number of files to write and their maximum size */
- p = argv[2];
- switch (tolower(*p))
- {
- case 'n': /* user entered number of files */
- maxsiz = -1L;
- if ((nfiles = atoi(++p)) <= 0 ||
- nfiles > MAXFILES)
- userr("Invalid number of files",0,0);
- break;
-
- case 'k': /* user entered maximum filesize */
- nfiles = -1;
- maxsiz = ((long) atoi(++p)) * 1024L;
- if (maxsiz < (long) (FBUFSIZ))
- userr("Maximum size too small",0,0);
- break;
-
- default:
- userr("Invalid second argument",0,0);
- break;
- } /* end of switch */
-
- /* open source file and find its size */
- if ((srcfil = open(argv[3],srcpmod)) == ERR)
- userr("Error in opening source file",0,0);
- else if ((srcsiz = lseek(srcfil,0L,2)) <= (long) (FBUFSIZ))
- userr("Source file smaller than minimum split",srcfil,0);
- else
- {
- printf("Splitting file %s of size %ld\n",argv[3],srcsiz);
- pos = lseek(srcfil,0L,0); /* rewind file */
- }
-
- /* finish doing filesize / # files calculation */
- if (nfiles == -1)
- {
- nfiles = (int) (srcsiz / maxsiz +1);
- if (nfiles > MAXFILES)
- userr("Too many section files",srcfil,0);
- }
- else if (maxsiz == -1L)
- {
- maxsiz = srcsiz / nfiles + 1;
- if (maxsiz < (long) (FBUFSIZ))
- userr("Section file size too small",srcfil,0);
- }
- printf("Number of section files: %2d\n",nfiles);
- printf("Maximum section file size: %ld\n",maxsiz);
-
- /* get section file name prefix and add ".-" identifier */
- if (strlen(argv[4]) <= 0 || strlen(argv[4]) > 60)
- userr("Section file prefix too long",srcfil,0);
- else
- {
- strcpy(prefix,argv[4]);
- strcat(prefix,".-");
- }
-
- /* now loop over the number of files desired */
- pos = 0L;
- for (i = 1; i <= nfiles; ++i)
- {
-
- /* create filename */
- strcpy(secname,prefix);
- nwrote = sprintf(numstr,"%02d",i);
- strcat(secname,numstr);
-
- /* initialize for this file */
- destsiz = 0L;
-
- /* open file */
- printf("Creating section file %s: ",secname);
- if ((destfil = creat(secname,destpmod)) == ERR)
- userr("Error in opening section file",srcfil,0);
-
- /* read buffers from source file into section file
- until maximum section file size is reached */
- nbytes = FBUFSIZ;
- nread = FBUFSIZ;
- while (nread == FBUFSIZ)
- {
-
- /* are we within a bufferful of the maximum filesize? */
- if ((destsiz + (long) nbytes) > maxsiz)
- nbytes = (int) (maxsiz - destsiz);
-
- /* read from source */
- if (nbytes > 0)
- nread = read(srcfil,buf,nbytes);
- else
- nread = 0;
- if (nread == ERR)
- userr("Error in reading source file",
- srcfil,destfil);
-
- /* write to destination */
- if (nread > 0)
- {
- nwrote = write(destfil,buf,nread);
- destsiz += nwrote;
- }
- else
- nwrote = 0;
- if (nwrote == ERR)
- userr("Error in writing section file",
- srcfil,destfil);
-
- } /* end while */
-
- /* close this destination file */
- pos += destsiz;
- if ((destfil = close(destfil)) == ERR)
- userr("Error in closing section file\n",srcfil,0);
- else
- printf(" %ld bytes\n",destsiz);
-
- } /* end for loop */
-
- /* close source file */
- if ((srcfil = close(srcfil)) == ERR)
- userr("Error in closing source file\n",0,0);
- else
- printf("Total length: %ld bytes\n",pos);
-
- } /* end SPLIT */