home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / text / docs / amigafaq / english / src / jwjoin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-21  |  3.7 KB  |  162 lines

  1. /*
  2.     JWJoin.c V1.00
  3.  
  4.     Joins files which are extracted from a larger file using JWSplit. All
  5.     files need to be in a common directory.
  6.  
  7.     Usage:   JWJoin <filename>
  8.  
  9.     Assume <filename> to be file.lha or file. JWJoin looks for files with
  10.     the names file.0, file.1, ... and joins each of this files into a larger
  11.     file called <filename> until file.n isn't found.
  12.     If there are, for example, file.0, file.1 and file.3, the first two
  13.     get joined while file.3 isomitted.
  14.  
  15.     Compiler: Each Ansi-C-Compiler; 32 bit integers are assumed.
  16.  
  17.     Author:   Jochen Wiedmann
  18.           Am Eisteich 9
  19.     72555 Metzingen (Germany)
  20.           Tel. 07123/14881
  21.           E-Mail: wiedmann@mailserv.zdv.uni-tuebingen.de
  22. */
  23.  
  24.  
  25. /*
  26.     Constants
  27.  
  28.     DEFAULTBUFSIZE is the number of bytes which can be read or written
  29.     by each call of fread() or fwrite().
  30. */
  31. #define VERSION "JWJoin V1.00 (Public Domain), 23.02.1993 by  Jochen Wiedmann"
  32. #define DEFAULTBUFSIZE 1024
  33.  
  34. /*
  35.     Include files we need
  36. */
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <stdio.h>
  40. #include <errno.h>
  41.  
  42.  
  43. /*
  44.     GetBaseName() cuts an optional ending from the filename.
  45. */
  46. void GetBaseName (char *file)
  47.  
  48. { char *endung;
  49.  
  50.   if ((endung = strrchr (file, '.')) != NULL)
  51.     { *endung = '\0';
  52.     }
  53. }
  54.  
  55.  
  56. /*
  57.     GetNewName() creates a new filename by appending the ending '.i' where
  58.     i is the value of the variable i.
  59. */
  60. void GetNewName (char *newfile, char *oldfile, int i)
  61.  
  62. { sprintf (newfile, "%s.%d", oldfile, i);
  63. }
  64.  
  65.  
  66. /*
  67.     FileSize() gets the Size of a file which is opened for input. The
  68.     first byte of the file may get written after executing FileSize().
  69. */
  70. int FileSize(FILE *fhin)
  71.  
  72. { int size;
  73.  
  74.   if (fseek (fhin, 0, 2)  !=  0   ||  (size = ftell (fhin)) < 0  ||
  75.       fseek (fhin, 0, 0)  !=  0)
  76.     { perror ("Error while reading : ");
  77.       exit (11);
  78.     }
  79.   return (size);
  80. }
  81.  
  82.  
  83. /*
  84.     The main program.
  85. */
  86. void main (int argc, char *argv[])
  87.  
  88. { int num = 0;
  89.   FILE *fhin, *fhout = NULL;
  90.   int Size, ReadSize;
  91.   char buffer[DEFAULTBUFSIZE];
  92.   char NewName[FILENAME_MAX+1];
  93.   char BaseName[FILENAME_MAX+1];
  94.  
  95. /*
  96.     Checking the arguments
  97. */
  98.   if (argc != 2  ||  *argv[1] == '?'  ||  strcmp (argv[1], "-h") == 0)
  99.     { fprintf (stderr, "Usage: JWJoin <file>\n\n");
  100.       fprintf (stderr, "\tJWJoin looks for files called archive.0, ");
  101.       fprintf (stderr, "archive.1, ...\n");
  102.       fprintf (stderr, "\t(if <file> is archive.lha or archive) created by");
  103.       fprintf (stderr, "JWSplit and\n");
  104.       fprintf (stderr, "\tjoins them together. All files have to be in the");
  105.       fprintf (stderr, "actual directory.\n");
  106.       fprintf (stderr, "\n%s\n", VERSION);
  107.       exit (1);
  108.     }
  109.  
  110. /*
  111.     Looking for files to be read
  112. */
  113.   for (;;)
  114.     { strcpy (BaseName, argv[1]);
  115.       GetBaseName (BaseName);
  116.       GetNewName (NewName, BaseName, num++);
  117.  
  118.       if ((fhin = fopen (NewName, "r"))  ==  NULL)
  119.     { if (num != 1  &&  errno == ENOENT)
  120.         { break;
  121.         }
  122.       fprintf (stderr, "Cannot open %s ", argv[1]);
  123.       perror (" for input: ");
  124.       exit(10);
  125.     }
  126.       Size = FileSize (fhin);
  127.  
  128. /*
  129.     Open output file, if needed
  130. */
  131.       if (fhout == NULL  &&  (fhout = fopen (argv[1], "w")) == NULL)
  132.     { fprintf (stderr, "Cannot open %s ", argv[1]);
  133.       perror (" for output: ");
  134.       exit (12);
  135.     }
  136.  
  137. /*
  138.    Reading file
  139. */
  140.       printf ("Reading %s...", NewName);
  141.       fflush (stdout);
  142.       while (Size > 0)
  143.     { ReadSize = (Size > DEFAULTBUFSIZE) ? DEFAULTBUFSIZE : Size;
  144.      if (fread (buffer, 1, ReadSize, fhin) < ReadSize)
  145.         { perror ("Error while reading: ");
  146.           exit (12);
  147.         }
  148.       if (fwrite (buffer, 1, ReadSize, fhout) < ReadSize)
  149.         { perror ("Error while writing: ");
  150.           exit (13);
  151.         }
  152.       Size -= ReadSize;
  153.     }
  154.       fclose (fhin);
  155.       printf ("Done\n");
  156.     }
  157.  
  158.   fclose (fhout);
  159.   exit (0);
  160. }
  161.  
  162.