home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 4
/
FreshFish_May-June1994.bin
/
new
/
util
/
cli
/
msplit
/
msplit.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-02-06
|
10KB
|
407 lines
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* 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();