home *** CD-ROM | disk | FTP | other *** search
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- * *
- * msplit -- Version 1.3 -- from Feb 07, 1994 *
- * *
- * Copyright (c) by Rene Tschirley *
- * *
- * All rights reserved *
- * *
- * *
- * This is not public domain but freeware! *
- * *
- * Permission is granted to make and distribute verbatim copies of this *
- * program's code and documentation as you receive it, in any medium, provided *
- * that you conspicuously and appropriately publish only the original, *
- * unmodified program and documentation, with all copyright notices of *
- * warranty intact and including anything else that came with the original. *
- * *
- * There is no warranty for this software package. Although the author has *
- * tried to prevent errors, he can't guarantee that the software package *
- * described in this document is 100% reliable. You are therefore using this *
- * material at your own risk. The author cannot be made responsible for any *
- * damage which is caused by using this software package. *
- * *
- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
-
-
- /* these three should be existant on EVERY system */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
-
- /* we use an Amiga, UNIX System-V or ConvexOS system, this file should be included */
- #include <fcntl.h>
-
- /* we have a BSD4.3 UNIX like Sun-OS, include this */
- /*
- #include <sys/file.h>
- */
-
- /* if we use a MS-DOS machine, include this */
- /*
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <io.h>
- */
-
- /* for VMS usage, use this */
- /*
- #include <file.h>
- */
-
- /* if we have anything else, hope that fcntl.h */
- /* will fit it. Look at the error messages concerning undefined functions... */
- /*
- #include <fcntl.h>
- */
-
-
-
-
- /* here some other used macros... */
-
- #
- #ifndef FILENAME_MAX
- #define FILENAME_MAX 256
- #endif
- #
- #ifndef S_IREAD
- #define S_IREAD 0
- #endif
- #ifndef S_IWRITE
- #define S_IWRITE 0
- #endif
- #
- #ifdef BUFSIZ
- #if BUFSIZ < 1024
- #define BUFFERSIZE 1024
- #else
- #define BUFFERSIZE BUFSIZ
- #endif
- #else
- #define BUFFERSIZE 1024
- #endif
- #
- #define M_CONCAT 1
- #define M_DELETE 2
- #define S_VERSION "MSPLIT Version 1.3 from Feb 07, 1994 (c) by R.Tschirley\n"
- #define S_USAGE "msplit -h | [-s]|-c [-n <size>[k]] [-d] <file> [to <dest>]\n"
- #
-
-
- /**
- ** Prototypes
- **/
-
- char * Parse_command(int targc, char **targv);
- void Split_file(char *filename);
- void Concat_file(char *filename);
- void error(int code, char *string);
- void Usage(void);
- void Help(void);
-
-
- /**
- ** global variables
- **/
-
- int mode =0;
- unsigned long int size =720000;
- char * destdir = "";
-
-
-
-
- /**
- ** Function: main
- **/
-
- int main(int argc, char **argv)
- {
- char *filename = NULL;
-
- if (!(filename = Parse_command(argc,argv)))
- error(3,NULL);
-
- printf("%s",S_VERSION);
-
- if (mode & M_CONCAT)
- Concat_file(filename);
- else
- Split_file(filename);
-
- return 1;
- }
-
-
-
- /**
- ** Function: Parse_command
- **
- ** Parses the commandline options. 'size', 'destdir' and 'mode' are defined
- ** global, the filename will be returned to avoid some confusion. Commandline
- ** is checked for logical sence.
- **/
-
- char *Parse_command(int targc, char **targv)
- {
- char *filename = NULL;
- int i = 1, c, d;
-
- while (targc > i)
- {
- if (targv[i][0] == '-')
- {
- switch (targv[i][1])
- {
- case 'c':
- mode = mode | M_CONCAT; break;
- case 'd':
- mode = mode | M_DELETE; break;
- case 's':
- mode = mode ^ M_CONCAT ; break;
- case 'n':
- i++;
- if (isdigit( c = targv[i][d = strlen(targv[i])-1] ))
- size = (atol(targv[i]) / BUFFERSIZE) * BUFFERSIZE;
- else
- {
- targv[i][d] = '\0';
- if (c == 'k' || c == 'K')
- size = (atol(targv[i]) * 1000 / BUFFERSIZE) * BUFFERSIZE;
- else
- error(6,(char *)c);
- }
- if (!size) error(7, NULL);
- break;
- case 'h': case '?':
- Help();
- default:
- error(4,&targv[i][1]);
- }
- }
- else
- {
- if (!(strcmp(targv[i],"to")))
- destdir = targv[++i];
- else
- filename = targv[i];
- }
- i++;
- }
- return filename;
- }
-
-
-
- /**
- ** Function: Split_file
- **
- ** Does the whole splitting of the large masterfile.
- **/
-
- void Split_file(char *filename)
- {
- int infile, outfile;
- char buffer[BUFFERSIZE];
- char outfilename[FILENAME_MAX];
- register unsigned int i=0;
- register unsigned long int j=0;
- register int result;
-
- if (-1 == (infile = open(filename, O_RDONLY, 0))) error(1,filename);
-
- do
- {
- sprintf(outfilename,"%s%s.M%d",destdir,filename,++i);
- printf("Writing file %s\n",outfilename);
- if (-1 == (outfile = creat(outfilename, S_IREAD | S_IWRITE)))
- error(0,outfilename);
-
- do
- {
- if (-1 == (result = read(infile, buffer, BUFFERSIZE)))
- {
- close(infile); close(outfile);
- error(1, filename);
- }
- if (-1 == (result = write(outfile, buffer, result)))
- {
- close(infile); close(outfile);
- error(0, outfilename);
- }
- j += result;
-
- }
-
- while(result && (j + BUFFERSIZE <= size));
-
- close(outfile);
- j=0;
- }
- while(result);
-
- close(infile);
- printf("Split file %s into %d parts.\n",filename,i);
- if (mode & M_DELETE)
- {
- printf("Deleting input file %s.\n",filename);
- unlink(filename);
- }
- return;
- }
-
-
-
- /**
- ** Function: Concat_file
- **
- ** The complete concatenation of the files.
- **/
-
- void Concat_file(char *filename)
- {
- int infile, outfile;
- char buffer[BUFFERSIZE];
- char infilename[FILENAME_MAX];
- register unsigned int i=0;
- register int result;
-
- sprintf(infilename,"%s%s",destdir,filename);
- if (-1 == (outfile = creat(infilename, S_IREAD | S_IWRITE)))
- error(0,infilename);
-
- while(1)
- {
- sprintf(infilename,"%s.M%d",filename,++i);
- printf("Reading file %s\n",infilename);
- if (-1 == (infile = open(infilename,O_RDONLY))) break;
-
- do
- {
- if (-1 == (result = read(infile, buffer, BUFFERSIZE)))
- {
- close(infile); close(outfile);
- error(1, infilename);
- }
- if (-1 == (result = write(outfile, buffer, result)))
- {
- close(infile); close(outfile);
- error(0,filename);
- }
- }
- while(result);
- close(infile);
- if (mode & M_DELETE)
- {
- printf("Deleting input file %s.\n",infilename);
- unlink(infilename);
- }
- }
-
- close(outfile);
- if (!(--i)) error(2,NULL);
- printf("Concated file %s from %d parts.\n",filename,i);
- return;
- }
-
-
-
- /**
- ** Function: error
- **
- ** Here are all exits caused by any error.
- **/
-
- void error(int code, char *string)
- {
- char msg[256];
-
- switch (code)
- {
- case 0:
- perror(strcat(strcpy(msg,"MSPLIT: error on output file "),string));
- break;
- case 1:
- perror(strcat(strcpy(msg,"MSPLIT: error on input file "),string));
- break;
- case 2:
- printf("MSPLIT:\tFound to files to concat.\n");
- break;
- case 3:
- printf("MSPLIT:\tNo input file specified.\n");
- Usage();
- break;
- case 4:
- printf("Unknown commandline option %s!\n",string);
- Usage();
- break;
- case 5:
- printf("MSPLIT: No file specified.\n");
- Usage();
- break;
- case 6:
- printf("MSPLIT:\tIllegal character '%c' in commandline option parameter -n.\n",string);
- break;
- case 7:
- printf("MSPLIT:\tIllegal size!\n");
- break;
- }
- exit(0);
- }
-
-
-
- /**
- ** Function: Usage
- **
- ** Obvious.
- **/
-
- void Usage(void)
- {
- printf("\tUSAGE: %s\tFor detailed information, please read msplit.doc!\n",S_USAGE);
- exit(0);
- }
-
-
-
- /**
- ** Function: Help
- **
- ** Obvious.
- **/
-
- void Help(void)
- {
- char *a="This program is no Public Domain but Freeware. (c) by Rene Tschirley.\n",
- *b=" Permission is granted to make and distribute verbatim copies of this\n",
- *c="program's code and documentation as you receive it, in any medium, provided\n",
- *d="that you conspicuously and appropriately publish only the original,\n",
- *e="unmodified programcode and documentation, with all copyright notices of\n",
- *f="warranty intact. For further information, please read the document.\n\n",
- *g="MSPLIT USAGE: ",
- *i=" The order of options is ignored. Here are the options:\n",
- *j=" -s msplit works in splitting mode (default)\n",
- *k=" The filenames will get a suffix '.Mn' where n is\n",
- *l=" the number of the part.\n",
- *m=" -c msplit works in concatenating mode\n",
- *n=" msplit will search files named filename.Mn where n\n",
- *o=" counts up from 1. If the file won't be found,\n",
- *p=" msplit assumes that the work is done.\n",
- *q=" -n <size>[k] If 'k' is not specified, msplit computes the\n",
- *r=" amount of bytes that will fit as much blocks as\n",
- *s=" possible but not more than size bytes. If a 'k' is\n",
- *t=" specified, size is interpreted as the amount of\n",
- *u=" 1000 bytes. Default size is 720000 bytes.\n",
- *x=" -d Delete the input file after processing it.\n",
- *v=" <file> Process file named file.\n",
- *w=" to <dest> Write outputfiles to directory dest.\n";
-
- printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
- S_VERSION,a,b,c,d,e,f,g,S_USAGE,j,k,l,m,n,o,p,q,r,s,t,u,x,v,w);
- exit(0);
- }
-
-