home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / duucp-1.17 / AU-117b4-src.lha / src / util / uusplit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-24  |  2.5 KB  |  142 lines

  1. /*
  2.  *  UUSPLIT.C
  3.  *
  4.  *  uusplit file [outprefix] [-b #bytes] [-v]
  5.  *
  6.  *  take the binary 'file' and split into sections of 50KBytes each, uuencoding
  7.  *  each section independantly.  Result:
  8.  *
  9.  *    outprefix.01.uue,   outprefix.02.uue ,etc...  uuencoding
  10.  *    outprefix.01,        outprefix.02, etc...
  11.  *
  12.  *  File segments are uuencoded.  uuencode must be in your path.    Calculate
  13.  *  a 1.5 expansion uuencoding the file.
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include "version.h"
  19. #include "protos.h"
  20.  
  21. IDENT(".01");
  22.  
  23. Local void DumpSegment (FILE *, short, long, char *);
  24. Local void run_cmd (char *);
  25. Local void help (void);
  26.  
  27. short    Verbose;
  28.  
  29. int
  30. main(int ac, char **av)
  31. {
  32.     long bytes = 50000;
  33.     short i;
  34.     char *file    = NULL;
  35.     char *outpf = NULL;
  36.     FILE *fi;
  37.     long size;
  38.     long n;
  39.  
  40.     for (i = 1; i < ac; ++i) {
  41.     char *ptr = av[i];
  42.     if (*ptr == '-') {
  43.         ptr += 2;
  44.         switch(ptr[-1]) {
  45.         case 'v':
  46.         Verbose = 1;
  47.         break;
  48.         case 'b':
  49.         if (*ptr)
  50.             bytes = atoi(ptr);
  51.         else
  52.             bytes = atoi(av[++i]);
  53.         break;
  54.         default:
  55.         help();
  56.         }
  57.     } else {
  58.         if (file == NULL)
  59.         file = ptr;
  60.         else if (outpf == NULL)
  61.         outpf = ptr;
  62.         else
  63.         help();
  64.     }
  65.     }
  66.  
  67.     bytes = bytes * 2 / 3;    /*  assume uuencode expands file 1.5x    */
  68.  
  69.     if (file == NULL)
  70.     help();
  71.     if (outpf == NULL)
  72.     outpf = file;
  73.  
  74.     fi = fopen(file, "r");
  75.     if (fi == NULL) {
  76.     printf("couldn't open %s\n", file);
  77.     exit(1);
  78.     }
  79.     fseek(fi, 0L, 2);
  80.     size = ftell(fi);
  81.     for (i = n = 0; n < size; n += bytes) {
  82.     long b = size - n;
  83.  
  84.     if (b > bytes)
  85.         b = bytes;
  86.     fseek(fi, n, 0);
  87.     DumpSegment(fi, (short)(i + 1), b, outpf);
  88.     ++i;
  89.     }
  90.     fclose(fi);
  91.     return(0);
  92. }
  93.  
  94. void
  95. help (void)
  96. {
  97.     puts("uusplit file [outprefix] [-b bytes] [-v]");
  98.     exit (20);
  99. }
  100.  
  101. void
  102. DumpSegment (FILE *fi, short i, long bytes, char *outprefix)
  103. {
  104.     static char Name1[256];
  105.     static char Name2[256];
  106.     static char Name3[256];
  107.     static char Buf[4096];
  108.     long n;
  109.     FILE *fo;
  110.  
  111.     sprintf(Name1, "T:uusplit.%02d", i);
  112.     sprintf(Name2, "%s.%02d", outprefix, i);
  113.     sprintf(Name3, "%s.%02d.uue", outprefix, i);
  114.  
  115.     fo = fopen(Name1, "w");
  116.     if (fo == NULL) {
  117.     printf("couldn't create %s\n", Name1);
  118.     exit(1);
  119.     }
  120.  
  121.     while (bytes) {
  122.     n = (bytes > sizeof(Buf)) ? sizeof(Buf) : bytes;
  123.     fread(Buf, 1, n, fi);
  124.     fwrite(Buf, 1, n, fo);
  125.     bytes -= n;
  126.     }
  127.     fclose(fo);
  128.     sprintf(Buf, "uuencode >%s %s %s", Name3, Name1, Name2);
  129.     run_cmd(Buf);
  130.     remove(Name1);
  131. }
  132.  
  133. void
  134. run_cmd (char *buf)
  135. {
  136.     if (Verbose) {
  137.     puts(buf);
  138.     fflush(stdout);
  139.     }
  140.     Execute ((UBYTE *) buf, 0, 0);
  141. }
  142.