home *** CD-ROM | disk | FTP | other *** search
- /*
- JWJoin.c V1.00
-
- Joins files which are extracted from a larger file using JWSplit. All
- files need to be in a common directory.
-
- Usage: JWJoin <filename>
-
- Assume <filename> to be file.lha or file. JWJoin looks for files with
- the names file.0, file.1, ... and joins each of this files into a larger
- file called <filename> until file.n isn't found.
- If there are, for example, file.0, file.1 and file.3, the first two
- get joined while file.3 isomitted.
-
- Compiler: Each Ansi-C-Compiler; 32 bit integers are assumed.
-
- Author: Jochen Wiedmann
- Am Eisteich 9
- 72555 Metzingen (Germany)
- Tel. 07123/14881
- E-Mail: wiedmann@mailserv.zdv.uni-tuebingen.de
- */
-
-
- /*
- Constants
-
- DEFAULTBUFSIZE is the number of bytes which can be read or written
- by each call of fread() or fwrite().
- */
- #define VERSION "JWJoin V1.00 (Public Domain), 23.02.1993 by Jochen Wiedmann"
- #define DEFAULTBUFSIZE 1024
-
- /*
- Include files we need
- */
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <errno.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 = NULL;
- int Size, ReadSize;
- char buffer[DEFAULTBUFSIZE];
- char NewName[FILENAME_MAX+1];
- char BaseName[FILENAME_MAX+1];
-
- /*
- Checking the arguments
- */
- if (argc != 2 || *argv[1] == '?' || strcmp (argv[1], "-h") == 0)
- { fprintf (stderr, "Usage: JWJoin <file>\n\n");
- fprintf (stderr, "\tJWJoin looks for files called archive.0, ");
- fprintf (stderr, "archive.1, ...\n");
- fprintf (stderr, "\t(if <file> is archive.lha or archive) created by");
- fprintf (stderr, "JWSplit and\n");
- fprintf (stderr, "\tjoins them together. All files have to be in the");
- fprintf (stderr, "actual directory.\n");
- fprintf (stderr, "\n%s\n", VERSION);
- exit (1);
- }
-
- /*
- Looking for files to be read
- */
- for (;;)
- { strcpy (BaseName, argv[1]);
- GetBaseName (BaseName);
- GetNewName (NewName, BaseName, num++);
-
- if ((fhin = fopen (NewName, "r")) == NULL)
- { if (num != 1 && errno == ENOENT)
- { break;
- }
- fprintf (stderr, "Cannot open %s ", argv[1]);
- perror (" for input: ");
- exit(10);
- }
- Size = FileSize (fhin);
-
- /*
- Open output file, if needed
- */
- if (fhout == NULL && (fhout = fopen (argv[1], "w")) == NULL)
- { fprintf (stderr, "Cannot open %s ", argv[1]);
- perror (" for output: ");
- exit (12);
- }
-
- /*
- Reading file
- */
- printf ("Reading %s...", NewName);
- fflush (stdout);
- while (Size > 0)
- { ReadSize = (Size > DEFAULTBUFSIZE) ? DEFAULTBUFSIZE : Size;
- 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);
- }
- Size -= ReadSize;
- }
- fclose (fhin);
- printf ("Done\n");
- }
-
- fclose (fhout);
- exit (0);
- }
-
-