home *** CD-ROM | disk | FTP | other *** search
- /*
- JWSplit.c V1.00
-
- Splits a given file into some smaller files. This can be joined using
- Join (AmigaDOS), cat (Unix) or JWJoin (others).
-
- Usage: JWSplit <filename> [<size>]
-
- The file <filename> (file.lha or file for example) gets splitted into
- files which are not larger than <size> bytes. (Default of size is
- 720000 bytes, so each file should fit on a DSDD disk formatted for
- MS-Dos.) The smaller files get the names file.0, file.1, ...
-
- Compiler: Each Ansi-Compiler; 32 bit integers are assumed
-
- Author: Jochen Wiedmann
- Am Eisteich 9
- 7430 Metzingen
- Tel. 07123/14881
- E-Mail: wiedmann@mailserv.zdv.uni-tuebingen.de
- */
-
-
- /*
- Constants
-
- DEFAULTBUFSIZE is the number of bytes which are read or written by
- each call of fread() or fwrite().
- DEFAULTMAXSIZE is the number of bytes a created file may have.
- */
- #define VERSION "JWSplit V1.00 (Public Domain), 23.02.1993 by Jochen Wiedmann"
- #define DEFAULTBUFSIZE 1024
- #define DEFAULTMAXSIZE 720000
-
- /*
- Include files we need
- */
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
-
-
- /*
- GetBaseName() cuts an optional ending from the filename.
- */
- void GetBaseName (char *file)
-
- { char *endung;
-
- if ((endung = strrchr (file, '.')) != NULL)
- { *endung = '\0';
- }
- }
-
-
- /*
- GetNewName() creates a new filename by appending the ending '.i' where
- i is the value of the variable i.
- */
- void GetNewName (char *newfile, char *oldfile, int i)
-
- { sprintf (newfile, "%s.%d", oldfile, i);
- }
-
-
- /*
- FileSize() gets the Size of a file which is opened for input. The
- first byte of the file may get written after executing FileSize().
- */
- int FileSize(FILE *fhin)
-
- { int size;
-
- if (fseek (fhin, 0, 2) != 0 || (size = ftell (fhin)) < 0 ||
- fseek (fhin, 0, 0) != 0)
- { perror ("Error while reading : ");
- exit (11);
- }
- return (size);
- }
-
-
- /*
- The main program
- */
- void main (int argc, char *argv[])
-
- { int num = 0;
- FILE *fhin, *fhout;
- int ReadSize, NewSize, MaxSize = DEFAULTMAXSIZE;
- char buffer[DEFAULTBUFSIZE];
- char NewName[FILENAME_MAX+1];
- int Size;
-
- /*
- Checking the arguments
- */
- if (argc < 2 || argc >3 ||
- *argv[1] == '?' || strcmp (argv[1], "-h") == 0 ||
- (argc == 3 && (MaxSize = atoi (argv[2])) == 0))
- { fprintf (stderr, "Usage: JWSplit <file> [<size>]\n\n");
- fprintf (stderr, "\tJWSplit splits the given <file> into smaller ");
- fprintf (stderr, "files containing up to\n");
- fprintf (stderr, "\t<size> Bytes. JWJoin connects these files again.\n\n");
- fprintf (stderr, "\tIf <file> is 'archive.lha' or 'archive', the ");
- fprintf (stderr, "created files will be\n");
- fprintf (stderr, "\tcalled archive.0, archive.1,...\n");
- fprintf (stderr, "\tDefault-value of <size> is %d\n", DEFAULTMAXSIZE);
- fprintf (stderr, "\n%s\n", VERSION);
- exit (1);
- }
-
- /*
- Opening the input file
- */
- if ((fhin = fopen (argv[1], "r")) == NULL)
- { fprintf (stderr, "Cannot open %s ", argv[1]);
- perror (" for input: ");
- exit(10);
- }
-
- GetBaseName (argv[1]);
- Size = FileSize (fhin);
-
- /*
- Read the input file and create smaller output files.
- */
- while (Size > 0)
- { NewSize = (Size > MaxSize) ? MaxSize : Size;
- GetNewName(NewName, argv[1], num++);
- printf ("Creating %s...", NewName);
- fflush (stdout);
- if ((fhout = fopen (NewName, "w")) == NULL)
- { fprintf (stderr, "Cannot open %s ", NewName);
- perror (" for output: ");
- exit (14);
- }
- Size -= NewSize;
-
- while (NewSize > 0)
- { ReadSize = (NewSize > DEFAULTBUFSIZE) ? DEFAULTBUFSIZE : NewSize;
- if (fread (buffer, 1, ReadSize, fhin) < ReadSize)
- { perror ("Error while reading: ");
- exit (12);
- }
- if (fwrite (buffer, 1, ReadSize, fhout) < ReadSize)
- { perror ("Error while writing: ");
- exit (13);
- }
- NewSize -= ReadSize;
- }
- fclose (fhout);
- printf ("Done\n");
- }
- fclose (fhin);
- exit (0);
- }
-