home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / FLOPPIES / NOJSPLIT.ZIP / FSPLIT.C next >
Encoding:
C/C++ Source or Header  |  1989-09-23  |  12.4 KB  |  263 lines

  1. /*----------------------------------------------------------------------------
  2.  *  File:     FSPLIT.C  (Turbo C 1.5)
  3.  *
  4.  *  Purpose:  Splits a large file into smaller files for diskette storage.
  5.  *            Restores the same, remembering the extension of the old file.
  6.  *
  7.  *  Author:   Steven "Noji" Ratzlaff
  8.  *
  9.  *  Date:     23 Sep 1989
  10.  *
  11.  *  Note:     FSPLIT must be compiled in the 8086 instruction set.
  12.  *--------------------------------------------------------------------------*/
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <ctype.h>
  16. #include <string.h>
  17. #include <conio.h>
  18. #include <dir.h>
  19. #include <dos.h>
  20. /*--------------------------------------------------------------------------*/
  21. #define       EXT_CNT        4
  22. #define       MAX_NAME      80
  23. #define       MAX_BUF    30720
  24. /*--------------------------------------------------------------------------*/
  25.   int         valid();                 /* reports validity of the option    */
  26.   void        restore(),               /* restores the original file        */
  27.               split();                 /* splits up the original file       */
  28.   char        *Version =
  29.                 "Fsplit 1.2 (C) Sep 1989 by Steven \"Noji\" Ratzlaff";
  30. /*--------------------------------------------------------------------------*/
  31. main(argc, argv)                       /* FSPLIT                            */
  32.   int         argc;
  33.   char        **argv;
  34. {
  35.   char        *fname = *++argv;        /* name of the file to split/restore */
  36.   char        *option = *++argv;       /* the function option               */
  37.   /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38.   if (argc == 3 && valid(option))
  39.   {
  40.     option++;
  41.     if (tolower(*option) == 'r')
  42.       restore(fname);
  43.     else
  44.       split(fname, atoi(option));
  45.   }
  46.   else
  47.     printf("%s\n\n%s", Version,
  48.            "  *** Usage:    fsplit  filename  < /(size)  or  /R >\n"
  49.            "      Example:  fsplit  finance.fil /700"
  50.            "   (to split into 700K sections) or\n"
  51.            "                fsplit  finance /R      "
  52.            "   (to restore finance.fil)\n");
  53.   return 0;                            /* successful completion             */
  54. }                                      /* end of main()                     */
  55. /*----------------------------------------------------------------------------
  56.  *  restore():  Restores the specified files back into the original.
  57.  *--------------------------------------------------------------------------*/
  58.   void
  59. restore(fname)
  60.   char        *fname;
  61. {
  62.   FILE        *infile,                 /* the input files                   */
  63.               *outfile;                /* the output file                   */
  64.   struct ffblk
  65.               block;                   /* structure of file sttributes      */
  66.   char        drive[MAXDRIVE],         /* the drive named by dir.h          */
  67.               dir[MAXDIR],             /*  "  dir     "    "   "            */
  68.               filen[MAXFILE],          /*  "  file    "    "   "            */
  69.               exten[MAXEXT];           /*  "  ext     "    "   "            */
  70.   char        oldfile[MAX_NAME],       /* the old filenames                 */
  71.               oldext[MAX_NAME],        /* the old file's extension          */
  72.               buffer[MAX_BUF],         /* buffer for data transfer          */
  73.               *ext = ".AAA";           /* the first file's extension        */
  74.   long        count = 4L,              /* the total count of characters     */
  75.               total;                   /* the total file size of original   */
  76.   int         i,                       /* arbitrary counter                 */
  77.               bufcnt = 0,              /* buffer counter                    */
  78.               done;                    /* is the directory search done?     */
  79.   /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  80.   fnsplit(fname, drive, dir, filen, exten);
  81.   fnmerge(oldfile, drive, dir, filen, ext);
  82.   done = findfirst(oldfile, &block, 0);          /* find the file           */
  83.   if (!done)                                     /* if file is found        */
  84.   {
  85.     total = block.ff_fsize;                      /* get its size            */
  86.     infile = fopen(oldfile, "rb");               /* open it                 */
  87.     oldext[0] = fgetc(infile);
  88.     if (oldext[0] == '.')                        /* if it's a 1.2 format    */
  89.     {
  90.       for (i = 1; i < EXT_CNT; i++)              /* get the first 4 bytes   */
  91.       {
  92.         oldext[i] = fgetc(infile);
  93.         if (oldext[i] == '\xfe')
  94.           oldext[i] = '\0';
  95.       }
  96.       oldext[EXT_CNT] = '\0';
  97.       fnmerge(fname, drive, dir, filen, oldext);
  98.     }
  99.     else                                         /* if it's a 1.1 format    */
  100.     {
  101.       count = 1L;
  102.       buffer[bufcnt++] = oldext[0];
  103.     }
  104.     printf("Restoring %s from\n", strupr(fname));
  105.     printf("  %s\n", strupr(oldfile));
  106.     outfile = fopen(fname, "wb");
  107.     while (!done)                                /* for all files           */
  108.     {
  109.       count++;                                   /* increment chars in file */
  110.       buffer[bufcnt++] = fgetc(infile);          /* read a character        */
  111.       if (bufcnt == MAX_BUF || count == total)
  112.       {
  113.         for (i = 0; i < bufcnt; i++)
  114.           fputc(buffer[i], outfile);
  115.         bufcnt = 0;
  116.         if (count == total)                      /* if this file is done    */
  117.         {                                        /*   begin a new one       */
  118.           count = 0L;
  119.           fclose(infile);
  120.           for (i = 1; i < EXT_CNT; i++)
  121.             ext[i]++;
  122.           fnmerge(oldfile, drive, dir, filen, ext);
  123.           done = findfirst(oldfile, &block, 0);
  124.           if (!done)
  125.           {
  126.             printf("  %s\n", strupr(oldfile));
  127.             total = block.ff_fsize;
  128.             infile = fopen(oldfile, "rb");
  129.           }
  130.         }                              /* end of if (count == total)...     */
  131.       }                                /* end of if (bufcnt == MAX_BUF)...  */
  132.     }                                  /* end of while (!done)...           */
  133.     fclose(outfile);                   /* close the output file             */
  134.     printf("%s restored\n", fname);
  135.     printf("Delete old split files?  ");
  136.     while (i != 'y' && i != 'n')
  137.       i = tolower(getch());
  138.     printf("%c\n", i);
  139.     if (i == 'y')
  140.     {
  141.       strcpy(ext, ".AAA");
  142.       do {
  143.         fnmerge(fname, drive, dir, filen, ext);
  144.         for (i = 1; i < EXT_CNT; i++)
  145.           ext[i]++;
  146.       } while (!unlink(fname));
  147.     }
  148.   }                                    /* end of if (!done)                 */
  149.   else
  150.     printf("  *** File '%s' not found\n", oldfile);
  151. }                                      /* end of restore()                  */
  152. /*----------------------------------------------------------------------------
  153.  *  split():  Splits up a file into fragments.
  154.  *--------------------------------------------------------------------------*/
  155.   void
  156. split(fname, size)
  157.   char        *fname;                  /* name of file to split up          */
  158.   int         size;                    /* desired size of each fragment     */
  159. {
  160.   FILE        *infile,                 /* the file to split up              */
  161.               *outfile;                /* the new, fragmented file          */
  162.   struct ffblk
  163.               block;                   /* structure of file sttributes      */
  164.   char        drive[MAXDRIVE],         /* the drive named by dir.h          */
  165.               dir[MAXDIR],             /*  "  dir     "    "   "            */
  166.               filen[MAXFILE],          /*  "  file    "    "   "            */
  167.               exten[MAXEXT];           /*  "  ext     "    "   "            */
  168.   char        newfile[MAX_NAME],       /* the new filename                  */
  169.               oldext[MAX_NAME],        /* the old file's extension          */
  170.               buffer[MAX_BUF],         /* buffer for data transfer          */
  171.               *ext = ".AAA";           /* the first file's extension        */
  172.   long        count = 4L,              /* the current file's size           */
  173.               each,                    /* the desired size of each file     */
  174.               tcnt = 0L,               /* the total count of characters     */
  175.               total;                   /* the total file size of original   */
  176.   int         bufcnt = 0,              /* buffer counter                    */
  177.               i;                       /* arbitrary counter                 */
  178.   /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  179.   if (infile = fopen(fname, "rb"))     /* open the file to split up         */
  180.   {
  181.     fnsplit(fname, drive, dir, filen, oldext);
  182.     i = (int) strlen(oldext);
  183.     switch (i)
  184.     {
  185.       case  0   :  strcpy(oldext, ".");
  186.       case  1   :  strcat(oldext, "\xfe");
  187.       case  2   :  strcat(oldext, "\xfe");
  188.       case  3   :  strcat(oldext, "\xfe");
  189.       case  4   :  break;
  190.       default   :  printf("  *** String error in split():  oldext = %s\n",
  191.                           oldext);
  192.     }
  193.     strupr(oldext);
  194.     for (i = 0; i < EXT_CNT; i++)
  195.       buffer[bufcnt++] = oldext[i];
  196.     findfirst(strupr(fname), &block, 0);
  197.     total = block.ff_fsize;            /* find its size                     */
  198.     if ((total / ((long) size * 1024L) + 1) > 26)
  199.       printf("%s%s\n%s%d%s\n%s%ld%s\n",
  200.              "  *** More than 26 files will result if ", fname,
  201.              "           is split into ", size, "K byte sections.",
  202.              "      Specify sections of ",
  203.              ((total / 26624L) + 1L),            /* 26624 == 26K            */
  204.              "K bytes or larger for this file.");
  205.     else
  206.     {
  207.       printf("Splitting %s into\n", fname);
  208.       each = (long) size * 1024L;      /* get the maximum size of each file */
  209.       fnsplit(fname, drive, dir, filen, exten);
  210.       fnmerge(newfile, drive, dir, filen, ext);
  211.       outfile = fopen(newfile, "wb");
  212.       printf("  %s\n", strupr(newfile));
  213.       while (tcnt < total)                       /* for the whole file      */
  214.       {
  215.         tcnt++;                                  /* increment total count   */
  216.         count++;
  217.         buffer[bufcnt++] = fgetc(infile);        /* read a character        */
  218.         if (bufcnt == MAX_BUF || tcnt == total || count == each)
  219.         {
  220.           for (i = 0; i < bufcnt; i++)
  221.             fputc(buffer[i], outfile);
  222.           bufcnt = 0;
  223.           if (count == each)                     /* if this file is full    */
  224.           {                                      /*   begin a new one       */
  225.             count = 0L;
  226.             fclose(outfile);
  227.             for (i = 1; i < EXT_CNT; i++)
  228.               ext[i]++;
  229.             fnmerge(newfile, drive, dir, filen, ext);
  230.             outfile = fopen(newfile, "wb");
  231.             printf("  %s\n", strupr(newfile));
  232.           }                            /* end of if (count == each)...      */
  233.         }                              /* end of if (bufcnt == MAX_BUF...   */
  234.       }                                /* end of while (tcnt < total)...    */
  235.       fclose(outfile);                 /* close the output file             */
  236.       fclose(infile);                  /* close the input file              */
  237.       printf("%ld bytes transferred\n", tcnt);
  238.       printf("Delete %s ?  ", fname);
  239.       while (i != 'y' && i != 'n')
  240.         i = tolower(getch());
  241.       printf("%c\n", i);
  242.       if (i == 'y')
  243.         unlink(fname);
  244.     }                                  /* end of else of if ((total / ...)  */
  245.   }
  246.   else
  247.     printf("  *** File '%s' not found\n", fname);
  248. }                                      /* end of split()                    */
  249. /*----------------------------------------------------------------------------
  250.  *  valid():  Reports the validity of the specified option.  (A macro would
  251.  *            have been used if the expression wasn't so long.)
  252.  *--------------------------------------------------------------------------*/
  253.   int
  254. valid(opt)
  255.   char        *opt;
  256. {
  257.   return (opt[0] == '/' && (tolower(opt[1]) == 'r' ||
  258.      (isdigit(opt[1]) && opt[1] != '0')));
  259. }                                      /* end of valid()                    */
  260. /*----------------------------------------------------------------------------
  261.  *  End of FSPLIT.C
  262.  *--------------------------------------------------------------------------*/
  263.