home *** CD-ROM | disk | FTP | other *** search
- /*----------------------------------------------------------------------------
- * File: FSPLIT.C (Turbo C 1.5)
- *
- * Purpose: Splits a large file into smaller files for diskette storage.
- * Restores the same, remembering the extension of the old file.
- *
- * Author: Steven "Noji" Ratzlaff
- *
- * Date: 23 Sep 1989
- *
- * Note: FSPLIT must be compiled in the 8086 instruction set.
- *--------------------------------------------------------------------------*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include <conio.h>
- #include <dir.h>
- #include <dos.h>
- /*--------------------------------------------------------------------------*/
- #define EXT_CNT 4
- #define MAX_NAME 80
- #define MAX_BUF 30720
- /*--------------------------------------------------------------------------*/
- int valid(); /* reports validity of the option */
- void restore(), /* restores the original file */
- split(); /* splits up the original file */
- char *Version =
- "Fsplit 1.2 (C) Sep 1989 by Steven \"Noji\" Ratzlaff";
- /*--------------------------------------------------------------------------*/
- main(argc, argv) /* FSPLIT */
- int argc;
- char **argv;
- {
- char *fname = *++argv; /* name of the file to split/restore */
- char *option = *++argv; /* the function option */
- /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
- if (argc == 3 && valid(option))
- {
- option++;
- if (tolower(*option) == 'r')
- restore(fname);
- else
- split(fname, atoi(option));
- }
- else
- printf("%s\n\n%s", Version,
- " *** Usage: fsplit filename < /(size) or /R >\n"
- " Example: fsplit finance.fil /700"
- " (to split into 700K sections) or\n"
- " fsplit finance /R "
- " (to restore finance.fil)\n");
- return 0; /* successful completion */
- } /* end of main() */
- /*----------------------------------------------------------------------------
- * restore(): Restores the specified files back into the original.
- *--------------------------------------------------------------------------*/
- void
- restore(fname)
- char *fname;
- {
- FILE *infile, /* the input files */
- *outfile; /* the output file */
- struct ffblk
- block; /* structure of file sttributes */
- char drive[MAXDRIVE], /* the drive named by dir.h */
- dir[MAXDIR], /* " dir " " " */
- filen[MAXFILE], /* " file " " " */
- exten[MAXEXT]; /* " ext " " " */
- char oldfile[MAX_NAME], /* the old filenames */
- oldext[MAX_NAME], /* the old file's extension */
- buffer[MAX_BUF], /* buffer for data transfer */
- *ext = ".AAA"; /* the first file's extension */
- long count = 4L, /* the total count of characters */
- total; /* the total file size of original */
- int i, /* arbitrary counter */
- bufcnt = 0, /* buffer counter */
- done; /* is the directory search done? */
- /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
- fnsplit(fname, drive, dir, filen, exten);
- fnmerge(oldfile, drive, dir, filen, ext);
- done = findfirst(oldfile, &block, 0); /* find the file */
- if (!done) /* if file is found */
- {
- total = block.ff_fsize; /* get its size */
- infile = fopen(oldfile, "rb"); /* open it */
- oldext[0] = fgetc(infile);
- if (oldext[0] == '.') /* if it's a 1.2 format */
- {
- for (i = 1; i < EXT_CNT; i++) /* get the first 4 bytes */
- {
- oldext[i] = fgetc(infile);
- if (oldext[i] == '\xfe')
- oldext[i] = '\0';
- }
- oldext[EXT_CNT] = '\0';
- fnmerge(fname, drive, dir, filen, oldext);
- }
- else /* if it's a 1.1 format */
- {
- count = 1L;
- buffer[bufcnt++] = oldext[0];
- }
- printf("Restoring %s from\n", strupr(fname));
- printf(" %s\n", strupr(oldfile));
- outfile = fopen(fname, "wb");
- while (!done) /* for all files */
- {
- count++; /* increment chars in file */
- buffer[bufcnt++] = fgetc(infile); /* read a character */
- if (bufcnt == MAX_BUF || count == total)
- {
- for (i = 0; i < bufcnt; i++)
- fputc(buffer[i], outfile);
- bufcnt = 0;
- if (count == total) /* if this file is done */
- { /* begin a new one */
- count = 0L;
- fclose(infile);
- for (i = 1; i < EXT_CNT; i++)
- ext[i]++;
- fnmerge(oldfile, drive, dir, filen, ext);
- done = findfirst(oldfile, &block, 0);
- if (!done)
- {
- printf(" %s\n", strupr(oldfile));
- total = block.ff_fsize;
- infile = fopen(oldfile, "rb");
- }
- } /* end of if (count == total)... */
- } /* end of if (bufcnt == MAX_BUF)... */
- } /* end of while (!done)... */
- fclose(outfile); /* close the output file */
- printf("%s restored\n", fname);
- printf("Delete old split files? ");
- while (i != 'y' && i != 'n')
- i = tolower(getch());
- printf("%c\n", i);
- if (i == 'y')
- {
- strcpy(ext, ".AAA");
- do {
- fnmerge(fname, drive, dir, filen, ext);
- for (i = 1; i < EXT_CNT; i++)
- ext[i]++;
- } while (!unlink(fname));
- }
- } /* end of if (!done) */
- else
- printf(" *** File '%s' not found\n", oldfile);
- } /* end of restore() */
- /*----------------------------------------------------------------------------
- * split(): Splits up a file into fragments.
- *--------------------------------------------------------------------------*/
- void
- split(fname, size)
- char *fname; /* name of file to split up */
- int size; /* desired size of each fragment */
- {
- FILE *infile, /* the file to split up */
- *outfile; /* the new, fragmented file */
- struct ffblk
- block; /* structure of file sttributes */
- char drive[MAXDRIVE], /* the drive named by dir.h */
- dir[MAXDIR], /* " dir " " " */
- filen[MAXFILE], /* " file " " " */
- exten[MAXEXT]; /* " ext " " " */
- char newfile[MAX_NAME], /* the new filename */
- oldext[MAX_NAME], /* the old file's extension */
- buffer[MAX_BUF], /* buffer for data transfer */
- *ext = ".AAA"; /* the first file's extension */
- long count = 4L, /* the current file's size */
- each, /* the desired size of each file */
- tcnt = 0L, /* the total count of characters */
- total; /* the total file size of original */
- int bufcnt = 0, /* buffer counter */
- i; /* arbitrary counter */
- /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
- if (infile = fopen(fname, "rb")) /* open the file to split up */
- {
- fnsplit(fname, drive, dir, filen, oldext);
- i = (int) strlen(oldext);
- switch (i)
- {
- case 0 : strcpy(oldext, ".");
- case 1 : strcat(oldext, "\xfe");
- case 2 : strcat(oldext, "\xfe");
- case 3 : strcat(oldext, "\xfe");
- case 4 : break;
- default : printf(" *** String error in split(): oldext = %s\n",
- oldext);
- }
- strupr(oldext);
- for (i = 0; i < EXT_CNT; i++)
- buffer[bufcnt++] = oldext[i];
- findfirst(strupr(fname), &block, 0);
- total = block.ff_fsize; /* find its size */
- if ((total / ((long) size * 1024L) + 1) > 26)
- printf("%s%s\n%s%d%s\n%s%ld%s\n",
- " *** More than 26 files will result if ", fname,
- " is split into ", size, "K byte sections.",
- " Specify sections of ",
- ((total / 26624L) + 1L), /* 26624 == 26K */
- "K bytes or larger for this file.");
- else
- {
- printf("Splitting %s into\n", fname);
- each = (long) size * 1024L; /* get the maximum size of each file */
- fnsplit(fname, drive, dir, filen, exten);
- fnmerge(newfile, drive, dir, filen, ext);
- outfile = fopen(newfile, "wb");
- printf(" %s\n", strupr(newfile));
- while (tcnt < total) /* for the whole file */
- {
- tcnt++; /* increment total count */
- count++;
- buffer[bufcnt++] = fgetc(infile); /* read a character */
- if (bufcnt == MAX_BUF || tcnt == total || count == each)
- {
- for (i = 0; i < bufcnt; i++)
- fputc(buffer[i], outfile);
- bufcnt = 0;
- if (count == each) /* if this file is full */
- { /* begin a new one */
- count = 0L;
- fclose(outfile);
- for (i = 1; i < EXT_CNT; i++)
- ext[i]++;
- fnmerge(newfile, drive, dir, filen, ext);
- outfile = fopen(newfile, "wb");
- printf(" %s\n", strupr(newfile));
- } /* end of if (count == each)... */
- } /* end of if (bufcnt == MAX_BUF... */
- } /* end of while (tcnt < total)... */
- fclose(outfile); /* close the output file */
- fclose(infile); /* close the input file */
- printf("%ld bytes transferred\n", tcnt);
- printf("Delete %s ? ", fname);
- while (i != 'y' && i != 'n')
- i = tolower(getch());
- printf("%c\n", i);
- if (i == 'y')
- unlink(fname);
- } /* end of else of if ((total / ...) */
- }
- else
- printf(" *** File '%s' not found\n", fname);
- } /* end of split() */
- /*----------------------------------------------------------------------------
- * valid(): Reports the validity of the specified option. (A macro would
- * have been used if the expression wasn't so long.)
- *--------------------------------------------------------------------------*/
- int
- valid(opt)
- char *opt;
- {
- return (opt[0] == '/' && (tolower(opt[1]) == 'r' ||
- (isdigit(opt[1]) && opt[1] != '0')));
- } /* end of valid() */
- /*----------------------------------------------------------------------------
- * End of FSPLIT.C
- *--------------------------------------------------------------------------*/