home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / util / ssplit-1.3.lha / SSplit / ssplit.c < prev    next >
C/C++ Source or Header  |  1994-02-14  |  10KB  |  267 lines

  1. /*
  2.  *  ssplit
  3.  *
  4.  *  Copyright © 1992-1994 Stefan Sticht
  5.  *  All Rights Reserved
  6.  *
  7.  *  V1.2   5-12-93 Stefan  made options[] global to initialize it with 0s
  8.  *  V1.3  14-02-93 Stefan  recompiled with SAS/C 6.51
  9.  */
  10.  
  11. #define VERSION  "1.3"
  12. #define PROGNAME "ssplit"
  13. #define DATE     "14.2.94"
  14.  
  15. /******************************************************************************
  16.  *                                  includes                                  *
  17.  ******************************************************************************/
  18. #include <limits.h>
  19. #include <math.h>
  20. #include <stdlib.h>
  21. #define USE_BUILTIN_MATH
  22. #include <string.h>
  23. #include <exec/execbase.h>
  24. #include <exec/memory.h>
  25. #include <clib/alib_protos.h>
  26. #include <clib/alib_stdio_protos.h>
  27. #include <clib/dos_protos.h>
  28. #include <pragmas/dos_pragmas.h>
  29. #include <clib/exec_protos.h>
  30. #include <pragmas/exec_sysbase_pragmas.h>
  31.  
  32. /******************************************************************************
  33.  *                                 global data                                *
  34.  ******************************************************************************/
  35. /*
  36.  *  programtitle and version for Version command
  37.  */
  38. static char version[] = "\0$VER: " PROGNAME " " VERSION " (" DATE ") © 1992-1994 Stefan Sticht";
  39. #define TITLE_OFFSET 7l
  40. /*
  41.  *  command line args
  42.  */
  43. #define TEMPLATE "FROM/A,TO/A,SIZE/K/N,BUFFER/K/N,DDOFS/S,DDMSDOS/S,HDFFS/S,HDMSDOS/S"
  44.  
  45. #define OPTN_FROM       0
  46. #define OPTN_TO         1
  47. #define OPTN_SIZE       2
  48. #define OPTN_BUFFER     3
  49. #define OPTN_DDOFS      4
  50. #define OPTN_DDMSDOS    5
  51. #define OPTN_HDFFS      6
  52. #define OPTN_HDMSDOS    7
  53. #define OPTN_COUNT      8
  54.  
  55. #define FROM            (unsigned char *)options[OPTN_FROM]
  56. #define TO              (unsigned char *)options[OPTN_TO]
  57. #define DDOFS           options[OPTN_DDOFS]
  58. #define DDMSDOS         options[OPTN_DDMSDOS]
  59. #define HDFFS           options[OPTN_HDFFS]
  60. #define HDMSDOS         options[OPTN_HDMSDOS]
  61.  
  62. #define DEFAULT_SIZE    1730    /* double density fastfilesystem */
  63. #define BLOCKSIZE       512
  64. #define MAXFILENAMELEN  30
  65.  
  66. ULONG __saveds
  67. pseudomain(void)
  68. {
  69.     struct Library *SysBase = (*(struct Library **)4);
  70.     struct Library *DOSBase;
  71.     struct Process *thisprocess;
  72.     ULONG rc = 0;
  73.  
  74.     thisprocess = (struct Process *)FindTask(NULL);
  75.     if (!(thisprocess->pr_CLI))
  76.     {
  77.         /*
  78.          *  startet from Workbench: return immediatly
  79.          */
  80.         struct WBStartup *msg;
  81.         WaitPort(&(thisprocess->pr_MsgPort));
  82.         msg = (struct WBStartup *)GetMsg(&(thisprocess->pr_MsgPort));
  83.         Forbid();
  84.         ReplyMsg((struct Message *)msg);
  85.         return(0);
  86.     }
  87.  
  88.     if (DOSBase = OpenLibrary("dos.library", 37))
  89.     {
  90.         long options[OPTN_COUNT];
  91.         struct RDArgs *args;
  92.         memset(options, 0, sizeof(long) * OPTN_COUNT);
  93.         /*
  94.          *  parse command line
  95.          */
  96.         if (args = ReadArgs((UBYTE *)TEMPLATE, options, NULL))
  97.         {
  98.             unsigned long maxbuffers = ULONG_MAX;
  99.             unsigned long size = DEFAULT_SIZE;
  100.  
  101.             if (options[OPTN_BUFFER]) maxbuffers = *((long *)options[OPTN_BUFFER]);
  102.             if (options[OPTN_SIZE]) size = *((long *)options[OPTN_SIZE]);
  103.             else if (DDOFS) size = 1648;
  104.             else if (DDMSDOS) size = 1425;
  105.             else if (HDFFS) size = 3466;
  106.             else if (HDMSDOS) size = 2854;
  107.  
  108.             size *= BLOCKSIZE;
  109.  
  110.             if (FROM && *FROM)
  111.             {
  112.                 BPTR fromfh;
  113.  
  114.                 if (fromfh = Open(FROM, MODE_OLDFILE))
  115.                 {
  116.                     char *dest;
  117.                     unsigned long destsize = (TO ? strlen(TO) : 0) + MAXFILENAMELEN + 2;
  118.  
  119.                     if (dest = AllocMem(destsize, MEMF_ANY))
  120.                     {
  121.                         char *filename = FilePart(FROM);
  122.                         void *buffer;
  123.  
  124.                         strcpy(dest, TO);
  125.  
  126.                         if (AddPart(dest, filename, destsize))
  127.                         {
  128.                             unsigned long buffersize = AvailMem(MEMF_LARGEST) / 2;
  129.                             buffersize = min(size, buffersize);
  130.                             buffersize = min(maxbuffers, buffersize);
  131.  
  132.                             if (buffer = AllocMem(buffersize, MEMF_ANY))
  133.                             {
  134.                                 BPTR destfh = NULL;
  135.                                 long readsize;
  136.                                 unsigned long part = 1;
  137.                                 unsigned long read;
  138.                                 unsigned long toread;
  139.                                 unsigned short destlen = strlen(dest);
  140.                                 unsigned short filenamelen = strlen(filename);
  141.                                 unsigned short newfilenamelen;
  142.                                 unsigned char ready = FALSE;
  143.                                 char partbuffer[12];
  144.  
  145.                                 while (!ready)
  146.                                 {
  147.                                     read = 0;
  148.                                     toread = buffersize;
  149.  
  150.                                     while ((read < size) && !ready)
  151.                                     {
  152.                                         readsize = Read(fromfh, buffer, toread);
  153.  
  154.                                         if (readsize == -1)
  155.                                         {
  156.  
  157.                                             PrintFault(IoErr(), NULL);
  158.                                             ready = TRUE;
  159.                                             rc = RETURN_ERROR;
  160.  
  161.                                         }
  162.                                         else if (readsize != 0)
  163.                                         {
  164.                                             read += readsize;
  165.                                             toread = min(buffersize, size - read);
  166.  
  167.                                             if (!destfh)
  168.                                             {
  169.                                                 sprintf(partbuffer, ".%lu", part);
  170.                                                 newfilenamelen = filenamelen + strlen(partbuffer);
  171.                                                 strcpy(&dest[(newfilenamelen > MAXFILENAMELEN) ? destlen - (newfilenamelen - MAXFILENAMELEN) : destlen], partbuffer);
  172.  
  173.                                                 if (!(destfh = Open(dest, MODE_NEWFILE)))
  174.                                                 {
  175.                                                     PrintFault(IoErr(), NULL);
  176.                                                     ready = TRUE;
  177.                                                     rc = RETURN_ERROR;
  178.                                                 }
  179.  
  180.                                             }
  181.                                             if (!rc)
  182.                                             {
  183.                                                 if (Write(destfh, buffer, readsize) != readsize)
  184.                                                 {
  185.                                                     PrintFault(IoErr(), NULL);
  186.                                                     ready = TRUE;
  187.                                                     rc = RETURN_ERROR;
  188.                                                 }
  189.  
  190.                                             }
  191.                                         }
  192.                                         else ready = TRUE; /* EOF reached */
  193.  
  194.                                     } /* while ((read <= size) && !ready) */
  195.  
  196.                                     if (destfh)
  197.                                     {
  198.                                         Close(destfh);
  199.                                         destfh = NULL;
  200.                                     }
  201.  
  202.                                     if (!rc)
  203.                                     {
  204.                                         if (part < ULONG_MAX) part++;
  205.                                         else
  206.                                         {
  207.                                             PrintFault(ERROR_BUFFER_OVERFLOW, NULL);
  208.                                             ready = TRUE;
  209.                                             rc