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

  1. /*
  2.  *  SPLITMBOX.C
  3.  *
  4.  *  SPLITMBOX [-c] outemplate bytes filetemplate [outstart [filestart]]
  5.  */
  6.  
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include "version.h"
  11.  
  12. IDENT(".03");
  13.  
  14. static int TNo;
  15. char TmpBuf[256];
  16. short CompressOpt = 0;
  17.  
  18. int _bufsiz = 16384;
  19.  
  20. void Dump(FILE *, long, char *);
  21.  
  22. int main(int ac, char **av)
  23. {
  24.     short i;
  25.     short j;
  26.     short inNo = 1;
  27.     long bytes;
  28.     char *outTpl;
  29.     char *inTpl;
  30.  
  31.     if (ac <= 3) {
  32.     puts("SPLITMBOX [-c] outtemplate bytes intemplate [outstart# [instart#]]");
  33.     exit(1);
  34.     }
  35.     for (j = 0, i = 1; i < ac; ++i) {
  36.     char *ptr = av[i];
  37.     if (*ptr == '-') {
  38.         ptr += 2;
  39.         switch(ptr[-1]) {
  40.         case 'c':
  41.         CompressOpt = 1;
  42.         break;
  43.         default:
  44.         puts("Illegal option");
  45.         exit(1);
  46.         }
  47.     } else {
  48.         switch(j) {
  49.         case 0:
  50.         outTpl = ptr;
  51.         break;
  52.         case 1:
  53.         bytes = atoi(ptr);
  54.         break;
  55.         case 2:
  56.         inTpl = ptr;
  57.         break;
  58.         case 3:
  59.         TNo = atoi(ptr) - 1;
  60.         break;
  61.         case 4:
  62.         inNo = atoi(ptr);
  63.         break;
  64.         default:
  65.         puts("Unexpected parameter");
  66.         exit(1);
  67.         }
  68.         ++j;
  69.     }
  70.     }
  71.     if (j < 3) {
  72.     puts("not enough parameters");
  73.     exit(1);
  74.     }
  75.  
  76.     for (;; ++inNo) {
  77.     FILE *fi;
  78.     char buf[128];
  79.  
  80.     sprintf(buf, "%s.%03d", inTpl, inNo);
  81.  
  82.     if (fi = fopen(buf, "r")) {
  83.         printf("%s\n", buf);
  84.         Dump(fi, bytes, outTpl);
  85.         fclose(fi);
  86.     } else {
  87.         printf("Unable to open %s\n", buf);
  88.         break;
  89.     }
  90.     }
  91.     return(0);
  92. }
  93.  
  94. void
  95. Dump (FILE *fi, long bytes, char *tpl)
  96. {
  97.     static long bytecnt;
  98.     static FILE *fo;
  99.     static char Buf[2048];
  100.  
  101.     strcpy(Buf, "\n");
  102.     while (fgets(Buf, sizeof(Buf), fi)) {
  103.     if (strncmp(Buf, "From ", 5) == 0) {
  104.         if (bytecnt >= bytes) {
  105.         if (fo) {
  106.             fclose(fo);
  107.             if (CompressOpt) {
  108.             sprintf(TmpBuf, "Compress <%s.%03d >%s.%03d.Z",
  109.                 tpl, TNo, tpl, TNo
  110.             );
  111.             Execute((UBYTE *) TmpBuf, 0, 0);
  112.             sprintf(TmpBuf, "%s.%03d", tpl, TNo);
  113.             remove(TmpBuf);
  114.             }
  115.         }
  116.         fo = NULL;
  117.         }
  118.     }
  119.     if (fo == NULL) {
  120.         char buf[64];
  121.  
  122.         sprintf(buf, "%s.%03d", tpl, ++TNo);
  123.         fo = fopen(buf, "w");
  124.         if (fo == NULL) {
  125.         printf("Unable to create %s\n", buf);
  126.         break;
  127.         }
  128.         bytecnt -= bytes;
  129.         if (bytecnt < 0 || bytecnt > bytes / 2)
  130.         bytecnt = 0;
  131.     }
  132.     fputs(Buf, fo);
  133.     bytecnt += strlen(Buf);
  134.     }
  135.     if (Buf[0] != '\n')
  136.     fputs("\n", fo);
  137. }
  138.