home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d07xx / d0764.lha / CLI-Tools / Split.c < prev    next >
C/C++ Source or Header  |  1992-11-21  |  3KB  |  144 lines

  1. /* Split
  2.  *
  3.  * by Thies Wellpott
  4.  *
  5.  * Usage: Split <infile> <outfile> size1 [size2 [size3] ...]
  6.  * Splits infile
  7.  * sizes in byte (e. g. "1204"), or KB (e. g. "4K")
  8.  *
  9.  * compile (SAS C V5.10a):
  10.  *   LC -cisu -rr -v -O Split
  11.  * link:
  12.  *   BLink FROM LIB:c.o,Split.o TO Split SC SD ND LIB LIB:lcr.lib
  13.  *
  14.  *
  15.  * HISTORY
  16.  *
  17.  * V1.00  28.6.92
  18.  *   first version
  19.  *
  20.  *
  21.  * TODO
  22.  *
  23.  * -
  24.  *
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include <proto/exec.h>
  32. #include <proto/dos.h>
  33. #include <exec/memory.h>
  34. #include <dos/dos.h>
  35.  
  36. #define VERSION "1.00"
  37. #define DATE "28.06.92"
  38. #define BUFMIN (4 * 1024)        /* 4 KB */
  39. #define BUFMAX (128 * 1024)      /* 128 KB */
  40.  
  41.  
  42. /* for DOS-command Version (OS 2.0) */
  43. char version[] = "$VER: Split V"VERSION" ("DATE")";
  44. UBYTE *buf = NULL;
  45. int buflen = BUFMAX;
  46. BPTR infhd = NULL, outfhd = NULL;
  47.  
  48.  
  49. void close_all(char *s, int err)
  50. /* close/free all and exit */
  51. {
  52.    if (outfhd) Close(outfhd);
  53.    if (infhd) Close(infhd);
  54.    if (buf) FreeMem(buf, buflen);
  55.  
  56.    if (s) printf("%s!\n", s);
  57.    exit(err);
  58. }
  59.  
  60.  
  61. void usage(void)
  62. /* print usage and exit */
  63. {
  64.    printf("\2334m%s\2330m by Thies Wellpott\n\n", &version[6]);
  65.    printf("Usage: Split <infile> <outfile> size1 [size2 [size3] ...]\n");
  66.  
  67.    exit(5);
  68. }
  69.  
  70.  
  71. void main(int argc, char *argv[])
  72. {
  73.    int i, mul, sizes;
  74.    long size[17], num_to_read, read, to_read;
  75.    char outname[128];
  76.  
  77.    if ((argc < 4) || (*argv[1] == '?'))      /* print usage? */
  78.       usage();
  79.  
  80.    sizes = min(argc-3, 16);                  /* get sizes */
  81.    for (i = 0; i < sizes; i++)
  82.    {
  83.       mul = 1;
  84.       if (toupper(argv[i+3][strlen(argv[i+3])-1]) == 'K')
  85.       {
  86.      argv[i+3][strlen(argv[i+3])-1] == 0;
  87.      mul = 1024;
  88.       }
  89.       size[i] = atoi(argv[i+3]) * mul;
  90.    }
  91.  
  92.    size[sizes] = 1<<31 - 1;             /* = 2^31-1, rest of infile */
  93.  
  94.    while (buflen >= BUFMIN)                  /* get buffer */
  95.    {
  96.       if (buf = AllocMem(buflen, MEMF_CLEAR))
  97.      break;
  98.       buflen /= 2;                 /* try half size */
  99.    }
  100.    if (!buf)                                 /* got buffer? */
  101.       close_all("Out of memory", 20);
  102.  
  103.    if (!(infhd = Open(argv[1], MODE_OLDFILE)))
  104.       close_all("Can`t open infile", 20);
  105.  
  106.    for (i = 0; i <= sizes; i++)
  107.    {
  108.       sprintf(outname, "%s.%d", argv[2], i);
  109.       if (!(outfhd = Open(outname, MODE_NEWFILE)))
  110.      close_all("Can`t open outfile", 20);
  111.  
  112.       to_read = size[i];
  113.  
  114.       while (to_read != 0)
  115.       {
  116.      num_to_read = min(buflen, to_read);
  117.      to_read -= num_to_read;
  118.  
  119.      read = Read(infhd, buf, num_to_read);
  120.      if (read == -1)
  121.         close_all("infile: read error", 20);
  122.      if (read < num_to_read)
  123.      {
  124.         if (i != sizes)
  125.         {
  126.            printf("outfile %d truncated!\n", i);
  127.            i = sizes+1;                  /* break loop */
  128.         }
  129.         else
  130.            to_read = 0;
  131.      }
  132.  
  133.      if (Write(outfhd, buf, read) != read)
  134.         close_all("outfile: write error", 20);
  135.       } /* while (to_read > 0) */
  136.  
  137.       Close(outfhd);
  138.       outfhd = NULL;
  139.    } /* for (i) */
  140.  
  141.    close_all(NULL, 0);
  142. }
  143.  
  144.