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

  1. /*
  2.     JWSplit.c V1.00
  3.  
  4.     Splits a given file into some smaller files. This can be joined using
  5.     Join (AmigaDOS), cat (Unix) or JWJoin (others).
  6.  
  7.     Usage:   JWSplit <filename> [<size>]
  8.  
  9.     The file <filename> (file.lha or file for example) gets splitted into
  10.     files which are not larger than <size> bytes. (Default of size is
  11.     720000 bytes, so each file should fit on a DSDD disk formatted for
  12.     MS-Dos.) The smaller files get the names file.0, file.1, ...
  13.  
  14.     Compiler: Each Ansi-Compiler; 32 bit integers are assumed
  15.  
  16.     Author:   Jochen Wiedmann
  17.           Am Eisteich 9
  18.      7430 Metzingen
  19.           Tel. 07123/14881
  20.           E-Mail: wiedmann@mailserv.zdv.uni-tuebingen.de
  21. */
  22.  
  23.  
  24. /*
  25.     Constants
  26.  
  27.     DEFAULTBUFSIZE is the number of bytes which are read or written by
  28.     each call of fread() or fwrite().
  29.     DEFAULTMAXSIZE is the number of bytes a created file may have.
  30. */
  31. #define VERSION "JWSplit V1.00 (Public Domain), 23.02.1993 by  Jochen Wiedmann"
  32. #define DEFAULTBUFSIZE 1024
  33. #define DEFAULTMAXSIZE 720000
  34.  
  35. /*
  36.     Include files we need
  37. */
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <stdio.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;
  90.   int ReadSize, NewSize, MaxSize = DEFAULTMAXSIZE;
  91.   char buffer[DEFAULTBUFSIZE];
  92.   char NewName[FILENAME_MAX+1];
  93.   int Size;
  94.  
  95. /*
  96.     Checking the arguments
  97. */
  98.   if (argc < 2  ||  argc >3  ||
  99.       *argv[1] == '?'  ||  strcmp (argv[1], "-h") == 0  ||
  100.       (argc == 3  &&  (MaxSize = atoi (argv[2])) == 0))
  101.     { fprintf (stderr, "Usage: JWSplit <file> [<size>]\n\n");
  102.       fprintf (stderr, "\tJWSplit splits the given <file> into smaller ");
  103.       fprintf (stderr, "files containing up to\n");
  104.       fprintf (stderr, "\t<size> Bytes. JWJoin connects these files again.\n\n");
  105.       fprintf (stderr, "\tIf <file> is 'archive.lha' or 'archive', the ");
  106.       fprintf (stderr, "created files will be\n");
  107.       fprintf (stderr, "\tcalled archive.0, archive.1,...\n");
  108.       fprintf (stderr, "\tDefault-value of <size> is %d\n", DEFAULTMAXSIZE);
  109.       fprintf (stderr, "\n%s\n", VERSION);
  110.       exit (1);
  111.     }
  112.  
  113. /*
  114.     Opening the input file
  115. */
  116.   if ((fhin = fopen (argv[1], "r"))  ==  NULL)
  117.     { fprintf (stderr, "Cannot open %s ", argv[1]);
  118.       perror (" for input: ");
  119.       exit(10);
  120.     }
  121.  
  122.   GetBaseName (argv[1]);
  123.   Size = FileSize (fhin);
  124.  
  125. /*
  126.     Read the input file and create smaller output files.
  127. */
  128.   while (Size > 0)
  129.     { NewSize = (Size > MaxSize) ? MaxSize : Size;
  130.       GetNewName(NewName, argv[1], num++);
  131.       printf ("Creating %s...", NewName);
  132.       fflush (stdout);
  133.       if ((fhout = fopen (NewName, "w"))  ==  NULL)
  134.     { fprintf (stderr, "Cannot open %s ", NewName);
  135.       perror (" for output: ");
  136.       exit (14);
  137.     }
  138.       Size -= NewSize;
  139.  
  140.       while (NewSize > 0)
  141.     { ReadSize = (NewSize > DEFAULTBUFSIZE) ? DEFAULTBUFSIZE : NewSize;
  142.       if (fread (buffer, 1, ReadSize, fhin) < ReadSize)
  143.         { perror ("Error while reading: ");
  144.           exit (12);
  145.         }
  146.       if (fwrite (buffer, 1, ReadSize, fhout) < ReadSize)
  147.         { perror ("Error while writing: ");
  148.           exit (13);
  149.         }
  150.       NewSize -= ReadSize;
  151.     }
  152.       fclose (fhout);
  153.       printf ("Done\n");
  154.     }
  155.   fclose (fhin);
  156.   exit (0);
  157. }
  158.