home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / packer / splitt / split.c next >
Encoding:
C/C++ Source or Header  |  1986-07-12  |  8.4 KB  |  333 lines

  1. /**********************************************************************
  2. *
  3. *    SPLIT:  Program to split and recombine binary files
  4. *
  5. *    USAGE:
  6. *        SPLIT  S  Ni  Complete_file  Section_file_prefix
  7. *    or    SPLIT  S  Ki  Complete_file  Section_file_prefix
  8. *    or    SPLIT  C  Complete_file  Section_file_list
  9. *
  10. *    where
  11. *        S indicates "split"    (may be upper or lower case)
  12. *        C indicates "combine"    (may be upper or lower case)
  13. *        Ni means "split into i files of equal size"
  14. *        Ki means "split into files of size i Kilobytes"
  15. *        Complete_file is the file to be split or recombined
  16. *        Section_files are the component files after split or
  17. *            to be recombined into the Complete_file
  18. *        Section_file_prefix is the 8-character filename portion
  19. *            of the Section_files.  The extension will be
  20. *            numbers generated by the program.
  21. *        Section_file_list is a sequence of Section_files, each
  22. *            separated by a space.
  23. *
  24. ***********************************************************************
  25. *
  26. *    REVISION HISTORY:
  27. *
  28. *        July 7, 1986: Created (Charlie Beerman)
  29. *
  30. **********************************************************************/
  31.  
  32.  
  33. #include <stdio.h>
  34. #include <ctype.h>
  35. #define FBUFSIZ 4096
  36. #define MAXFILES 99
  37. #define ERR (-1)
  38.  
  39. main(argc,argv)
  40. int argc;
  41. char *argv[];
  42. {
  43.  
  44. /****** start of executable code ******/
  45.  
  46. /* Check for at least 1 argument */
  47.  
  48.     if (argc < 2)
  49.         userr("",0,0);  /* Print usage & exit */
  50.  
  51. /* Read first argument to decide action */
  52.     switch (tolower(argv[1][0]))
  53.     {
  54.         case 'c':  /* Combine */
  55.             if (argc < 4)
  56.                 userr("Too few arguments",0,0);
  57.             else
  58.                 combine(argc,argv);
  59.             break;
  60.         case 's':  /* Split */
  61.             if (argc < 5)
  62.                 userr("Too few arguments",0,0);
  63.             else
  64.                 split(argc,argv);
  65.             break;
  66.         default:
  67.             userr("Invalid first argument",0,0);
  68.             break;    /* Code should never get to this line */
  69.     }
  70.  
  71. } /* end of main program */
  72.  
  73. /**********************************************************************
  74. *
  75. *    USERR:   Print error message and/or help info
  76. *
  77. *    INPUTS:
  78. *        errmsg    Text to output (if of nonzero length)
  79. *        file1    File to close (if nonzero)
  80. *        file2    File to close (if nonzero)
  81. *
  82. **********************************************************************/
  83.  
  84. userr(errmsg,file1,file2)
  85. char *errmsg;
  86. int file1, file2;
  87. {
  88.     int status;
  89.  
  90. /****** start of executable code ******/
  91.  
  92. /* close files if necessary */
  93.     if (file1 > 0)
  94.         status = close(file1);
  95.     if (file2 > 0)
  96.         status = close(file2);
  97.  
  98. /* print error message if there is one */
  99.     if (strlen(errmsg) > 0)
  100.         fprintf(stderr,"%s\n",errmsg);
  101.     else
  102.         fprintf(stderr,"%s%s%s%s",
  103. "Usage:\n",
  104. "       SPLIT  C  create_file_name  section_file_list\n",
  105. " or    SPLIT  S  Ni  source_file_name  section_file_prefix\n",
  106. " or    SPLIT  S  Ki  source_file_name  section_file_prefix\n");
  107.  
  108. /* now exit to DOS with errorlevel 1 */
  109.     exit(1);
  110.  
  111. } /* end USERR */
  112.  
  113.  
  114. /**********************************************************************
  115. *
  116. *    COMBINE:  Combine a list of files into one file
  117. *
  118. *    INPUTS:
  119. *        argc    Count of command-line arguments
  120. *        argv    List of command-line arguments
  121. *
  122. *    OUTPUTS:
  123. *        none
  124. *
  125. **********************************************************************/
  126.  
  127. combine(argc,argv)
  128. int argc;
  129. char *argv[];
  130. {
  131.     int i;
  132.     int destfil, srcfil;
  133.     int srcpmod = 0x8000;
  134.     int destpmod = 0x8001;
  135.     int nread, nwrote;
  136.     char buf[FBUFSIZ];
  137.  
  138. /****** start of executable code ******/
  139.  
  140. /* open file to recombine */
  141.     printf("Creating file %s\n",argv[2]);
  142.     if ((destfil = creat(argv[2],destpmod)) == ERR)
  143.         userr("Error in opening output file",0,0);
  144.  
  145. /* loop through section files */
  146.     for (i = 3; i < argc; ++i)
  147.     {
  148.  
  149. /* open source file */
  150.         printf("Reading from file %s\n",argv[i]);
  151.         if ((srcfil = open(argv[i],srcpmod)) == ERR)
  152.             userr("Error in opening input file",destfil,0);
  153.  
  154. /* loop through source file, reading a buffer at a time
  155.    into the destination file */
  156.         nread = FBUFSIZ;
  157.         while (nread == FBUFSIZ)
  158.         {
  159.             if ((nread = read(srcfil,buf,FBUFSIZ)) == ERR)
  160.                 userr("Error in reading file",destfil,srcfil);
  161.             else if (nread > 0)
  162.                 if ((nwrote = write(destfil,buf,nread))
  163.                     == ERR)
  164.                     userr("Error in writing file",
  165.                           destfil,srcfil);
  166.         }  /* end of while */
  167.  
  168. /* now close source file */
  169.         if ((srcfil = close(srcfil)) == ERR)
  170.             userr("Error in closing source file",destfil,0);
  171.  
  172.     } /* end of loop over files in argument list */
  173.  
  174. /* now close destination file */
  175.     if ((destfil = close(destfil)) == ERR)
  176.         userr("Error in closing destination file",0,0);
  177.  
  178. } /* end of COMBINE */
  179.  
  180. /**********************************************************************
  181. *
  182. *    SPLIT:  Split a file into components
  183. *
  184. *    INPUTS:
  185. *        argc    count of arguments on command line
  186. *        argv    text of arguments on command line
  187. *
  188. **********************************************************************/
  189.  
  190. split(argc,argv)
  191. int argc;
  192. char *argv[];
  193. {
  194.     int i, nfiles;
  195.     long maxsiz, srcsiz, destsiz, pos;
  196.     long lseek();
  197.     int destfil, srcfil;
  198.     int srcpmod = 0x8000;
  199.     int destpmod = 0x8001;
  200.     int nread, nwrote, nbytes;
  201.     char buf[FBUFSIZ], *p;
  202.     char prefix[61], secname[65], numstr[3];
  203.  
  204. /****** start of executable code ******/
  205.  
  206. /* figure out number of files to write and their maximum size */
  207.     p = argv[2];
  208.     switch (tolower(*p))
  209.     {
  210.         case 'n':  /* user entered number of files */
  211.             maxsiz = -1L;
  212.             if ((nfiles = atoi(++p)) <= 0 ||
  213.                  nfiles > MAXFILES)
  214.                 userr("Invalid number of files",0,0);
  215.             break;
  216.  
  217.         case 'k':  /* user entered maximum filesize */
  218.             nfiles = -1;
  219.             maxsiz = ((long) atoi(++p)) * 1024L;
  220.             if (maxsiz < (long) (FBUFSIZ))
  221.                 userr("Maximum size too small",0,0);
  222.             break;
  223.  
  224.         default:
  225.             userr("Invalid second argument",0,0);
  226.             break;
  227.     } /* end of switch */
  228.  
  229. /* open source file and find its size */
  230.     if ((srcfil = open(argv[3],srcpmod)) == ERR)
  231.         userr("Error in opening source file",0,0);
  232.     else if ((srcsiz = lseek(srcfil,0L,2)) <= (long) (FBUFSIZ))
  233.         userr("Source file smaller than minimum split",srcfil,0);
  234.     else
  235.     {
  236.         printf("Splitting file %s of size %ld\n",argv[3],srcsiz);
  237.         pos = lseek(srcfil,0L,0);    /* rewind file */
  238.     }
  239.  
  240. /* finish doing filesize / # files calculation */
  241.     if (nfiles == -1)
  242.     {
  243.         nfiles = (int) (srcsiz / maxsiz +1);
  244.         if (nfiles > MAXFILES)
  245.             userr("Too many section files",srcfil,0);
  246.     }    
  247.     else if (maxsiz == -1L)
  248.     {
  249.         maxsiz = srcsiz / nfiles + 1;
  250.         if (maxsiz < (long) (FBUFSIZ))
  251.             userr("Section file size too small",srcfil,0);
  252.     }
  253.     printf("Number of section files: %2d\n",nfiles);
  254.     printf("Maximum section file size:  %ld\n",maxsiz);
  255.  
  256. /* get section file name prefix and add ".-" identifier */
  257.     if (strlen(argv[4]) <= 0 || strlen(argv[4]) > 60)
  258.         userr("Section file prefix too long",srcfil,0);
  259.     else
  260.     {
  261.         strcpy(prefix,argv[4]);
  262.         strcat(prefix,".-");
  263.     }
  264.  
  265. /* now loop over the number of files desired */
  266.     pos = 0L;
  267.     for (i = 1; i <= nfiles; ++i)
  268.     {
  269.  
  270. /* create filename */
  271.         strcpy(secname,prefix);
  272.         nwrote = sprintf(numstr,"%02d",i);
  273.         strcat(secname,numstr);
  274.  
  275. /* initialize for this file */
  276.         destsiz = 0L;
  277.  
  278. /* open file */
  279.         printf("Creating section file %s:  ",secname);
  280.         if ((destfil = creat(secname,destpmod)) == ERR)
  281.             userr("Error in opening section file",srcfil,0);
  282.  
  283. /* read buffers from source file into section file
  284.    until maximum section file size is reached */
  285.         nbytes = FBUFSIZ;
  286.         nread = FBUFSIZ;
  287.         while (nread == FBUFSIZ)
  288.         {
  289.  
  290. /* are we within a bufferful of the maximum filesize? */
  291.             if ((destsiz + (long) nbytes) > maxsiz)
  292.                 nbytes = (int) (maxsiz - destsiz);
  293.  
  294. /* read from source */
  295.             if (nbytes > 0)
  296.                 nread = read(srcfil,buf,nbytes);
  297.             else
  298.                 nread = 0;
  299.             if (nread == ERR)
  300.                 userr("Error in reading source file",
  301.                        srcfil,destfil);
  302.  
  303. /* write to destination */
  304.             if (nread > 0)
  305.             {
  306.                 nwrote = write(destfil,buf,nread);
  307.                 destsiz += nwrote;
  308.             }
  309.             else
  310.                 nwrote = 0;
  311.             if (nwrote == ERR)
  312.                 userr("Error in writing section file",
  313.                        srcfil,destfil);
  314.  
  315.         } /* end while */
  316.  
  317. /* close this destination file */
  318.         pos += destsiz;
  319.         if ((destfil = close(destfil)) == ERR)
  320.             userr("Error in closing section file\n",srcfil,0);
  321.         else
  322.             printf(" %ld bytes\n",destsiz);
  323.  
  324.     } /* end for loop */
  325.  
  326. /* close source file */
  327.     if ((srcfil = close(srcfil)) == ERR)
  328.         userr("Error in closing source file\n",0,0);
  329.     else
  330.         printf("Total length:  %ld bytes\n",pos);
  331.  
  332. } /* end SPLIT */
  333.