home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / split.zip / SPLIT.C < prev    next >
Text File  |  1994-08-03  |  5KB  |  113 lines

  1. /*******************************************************************
  2. * Split a file into multiple pieces
  3. *
  4. * Syntax: split FileToSplit OutputFilePrefix
  5. *
  6. *******************************************************************/
  7. #define INCL_DOS
  8. #include <os2.h>
  9.  
  10. #include <stdio.h>            // printf & sprintf
  11.  
  12. #define BUFFER_SIZE 8192      // Size of read buffer
  13.  
  14. int main (int argc, char **argv)
  15. {
  16. APIRET      rc;                 // Return code
  17. HFILE       hFileToSplit;       // File handle for file to split
  18. char        *FileToSplit;       // Point to File to Split string
  19. HFILE       hNewFile;           // File handle for Output File
  20. char        *Prefix;            // Point to output file prefix
  21. char        szNewFile[261];     // Output file
  22. FILESTATUS3 FileStatus;         // File information for FileToSplit
  23. ULONG       ulFileSize;         // File size for FileToSplit
  24. ULONG       action;             // Action taken for a DosOpen call
  25. ULONG       ulNewPointer;       // New pointer location for DosSetFilePtr
  26. ULONG       ulPosition;         // Location in FileToSplit
  27. ULONG       ulBytesRead;        // How many bytes were read on DosRead
  28. ULONG       ulBytesWritten;     // How many bytes were written on DosWrite
  29. ULONG       fileCount;          // Keep track of how many files FileToSplit
  30.                                 // splits into
  31. BYTE        buffer[BUFFER_SIZE];// Read/Write buffer
  32. ULONG       ulSize;             // Keep track of how many bytes were written
  33.                                 // per file. When we go over 500000, create
  34.                                 // a new output file.
  35.  
  36.    /**********************************************************************
  37.    * Check the number of command line arguments. We are supposed to have
  38.    * 2 arguements: The file to split and the output file prefix.
  39.    **********************************************************************/
  40.    rc=0;
  41.    if (argc==3)
  42.       {
  43.       ulPosition=0;
  44.       ulSize=0;
  45.       fileCount=1;
  46.       FileToSplit=argv[1];
  47.       Prefix=argv[2];
  48.       rc=DosOpen(FileToSplit, &hFileToSplit, &action, 0L, 0L,
  49.               OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
  50.               OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY,
  51.               0L);
  52.       if (rc==0)
  53.          {
  54.          rc=DosQueryFileInfo(hFileToSplit, 1, &FileStatus, sizeof(FileStatus));
  55.          ulFileSize=FileStatus.cbFile;
  56.          } /* end IF */
  57.       if (rc==0)
  58.          {
  59.          rc=DosSetFilePtr(hFileToSplit, 0, FILE_BEGIN, &ulNewPointer);
  60.          } /* end IF */
  61.       if (rc==0)
  62.          {
  63.          sprintf(szNewFile, "%s.%d", Prefix, fileCount);
  64.          printf("Creating %s\n",szNewFile);
  65.          rc=DosOpen(szNewFile, &hNewFile, &action, 0L, 0L,
  66.                  OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
  67.                  OPEN_SHARE_DENYREADWRITE | OPEN_ACCESS_WRITEONLY,
  68.                  0L);
  69.          } /* end IF */
  70.       /**************************************************************
  71.       * Read from FileToSplit and loop until we hit the end of file
  72.       **************************************************************/
  73.       while (ulPosition<ulFileSize && rc==0)
  74.          {
  75.          rc=DosRead(hFileToSplit, buffer, BUFFER_SIZE, &ulBytesRead);
  76.          if (rc==0)
  77.             {
  78.             ulPosition+=ulBytesRead;
  79.             ulSize+=ulBytesRead;
  80.             rc=DosWrite(hNewFile, buffer, ulBytesRead, &ulBytesWritten);
  81.             if (ulSize>=450000 && rc==0)
  82.                {
  83.                printf("  File size is %d\n", ulSize);
  84.                DosClose(hNewFile);
  85.                ulSize=0;
  86.                fileCount++;
  87.                sprintf(szNewFile, "%s.%d", Prefix, fileCount);
  88.                printf("Creating %s\n",szNewFile);
  89.                rc=DosOpen(szNewFile, &hNewFile, &action, 0L, 0L,
  90.                        OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
  91.                        OPEN_SHARE_DENYREADWRITE | OPEN_ACCESS_WRITEONLY,
  92.                        0L);
  93.                } /* end IF */
  94.             } /* end IF */
  95.          } /* end while */
  96.       printf("  File size is %d\n", ulSize);
  97.       DosClose(hNewFile);
  98.       DosClose(hFileToSplit);
  99.       } /* end if */
  100.    else /* */
  101.       {
  102.       printf("  Syntax is: split FileToSplit OutputFilePrefix\n\n");
  103.       printf("  For example: 'split oem.wav oem' results in 3 files:\n");
  104.       printf("     OEM.1, OEM.2, and OEM.3\n");
  105.       } /* end ELSE */
  106.    /*****************************************************
  107.    * If we had an error condition, print out a message
  108.    *****************************************************/
  109.    if (rc)
  110.       printf("We encountered an error code on a Dos call: %d\n", rc);
  111.    return(rc);
  112. }
  113.