Category : Word Processors
Archive   : WIDGET.ZIP
Filename : WIDGET.C

 
Output of file : WIDGET.C contained in archive : WIDGET.ZIP

/********************************************************************/
/* */
/* Program: WIDGET - Version 1.0.0 */
/* Author: K. Scott Piel */
/* Date: 8/01/89 */
/* */
/* Break any one large file into a number of smaller files or join */
/* the resulting smaller files back into one file. This is a binary */
/* file manipulation and does no padding. The main intent is to */
/* make a simple way to transport one very large file on several */
/* disk without having to use backup or other suck fudged methods. */
/* */
/* This program is public domain - but please do not remove my */
/* credit...after all I _DID_ do it. */
/********************************************************************/

#include
#include
#include
#include
#include

long count = 0; /* total bytes transfered */
char buff[1024]; /* transfer buffer */

/****************************************************************/

void usage( void )

{
printf( "\n\nUsage: widget filename mode [size]\n\n" );

printf( " filename - The full file name of either the original\n" );
printf( " file which is to be broken up when using\n" );
printf( " the 'B' mode or the name of the file to be\n" );
printf( " created when using the 'J' mode. The name\n" );
printf( " may optionally include a path and or drive.\n" );
printf( " modes - B: break a file into smaller parts\n" );
printf( " J: join a broken file into one file\n" );
printf( " size - The size argument specifies the maximum\n" );
printf( " size of each of the output files created\n" );
printf( " when breaking up a larger file. This value\n" );
printf( " is only required when using the 'B' mode.\n\n" );

printf( "File names: when breaking a file into blocks this program will\n");
printf( "retain all parts of the file name, including the drive and\n");
printf( "directory portions, except for the extension for the purpose of\n");
printf( "naming each of the block files. The extension will be a three\n");
printf( "digit number from 001 to 999 depending on the number of files\n");
printf( "required. The program will halt and request verification before\n");
printf( "overwriting any existing files. Only if the user specifies to do\n");
printf( "so will operation and the file be overwritten.\n\n");

exit( 0 );
}

/****************************************************************/


int update( void )

{
/************************************************************/
/* update count every 1024 bytes */
/************************************************************/

printf( "\b\b\b\b\b\b\b%07ld", count );

return( 0 );
}

/****************************************************************/

int blockmove( char *filename, char *name, FILE *ifp, FILE *ofp, long size )

{
long j = 0; /* byte counter for blocks */
size_t c; /* characters being transfered */
size_t increment = 1024; /* increment size */

/************************************************************/
/* transfer 512 character at a time until eof or size bytes */
/************************************************************/

while( j < size && feof( ifp ) == 0 ) {

/********************************************************/
/* read a character from the input file */
/********************************************************/

if( (c = fread( buff, (size_t)1, increment, ifp )) != increment ) {

/****************************************************/
/* trap any read errors */
/****************************************************/

if( ferror( ifp ) != 0 && feof( ifp ) == 0 ) {
printf( "\n\nA read error has occurred in [%s].\n", filename);
fclose( ifp );
fclose( ofp );
exit( 0 );
}
}

/********************************************************/
/* write the character to the output file */
/********************************************************/

if (fwrite( buff, (size_t)1, c, ofp ) != c ) {

/****************************************************/
/* trap any write errors */
/****************************************************/

if( ferror( ofp ) != 0 ) {
printf( "\n\nA write error has occurred in %s\n", name );
fclose( ifp );
fclose( ofp );
exit( 0 );
}
}

/********************************************************/
/* update the various counters */
/********************************************************/

j += (long)c;
count += (long)c;
update();

/********************************************************/
/* if we are nearing the block end adjust the increment */
/********************************************************/

while( (j + (long)increment) > size && increment > 1 ) {
increment /= 2;
}
}

return( 0 );
}

/****************************************************************/

int verifyfile( char *name )

{
char input = '\0';

printf( "\n\nThe file [%s] already exist!\n\n", name );
printf( "Overwrite this file? (Y/N) " );

do {
input = toupper( fgetc( stdin ) );
} while( input != 'Y' && input != 'N' );

input -= 'N';

if( !input ) {
printf( "\nOperator interrupted execution.\n" );
} else {
printf( "\nBytes transfered: %07ld", count );
}

return( input );
}

/****************************************************************/

int breakfile( char* filename, char *rootname, long size )

{
FILE *ifp; /* input file pointer */
FILE *ofp; /* output file pointer */
int i; /* file counter */
char name[257]; /* name of output file */

void usage( void );
int update( void );

/****************************************************************/
/* open the input file for reading */
/****************************************************************/

if( (ifp = fopen( filename , "rb" )) == (FILE *)NULL ) {
printf( "\n\nCan not open [%s] for reading.\n", filename);
usage();
exit( 0 );
}

/****************************************************************/
/* no point in breaking up a smaller file! */
/****************************************************************/

if( filelength( fileno( ifp ) ) <= size ) {
printf( "\n\n%s is already less than %ld bytes long!\n\n", filename, size );
exit( 0 );
}

/****************************************************************/
/* continue looping through output files, never filling above */
/* size characters until all of ifp has been read */
/****************************************************************/

for( i = 0; feof( ifp ) == 0; i++ ) {

/************************************************************/
/* verify that the output file does not exist */
/************************************************************/

sprintf( name, "%s%c%03d", rootname, '.', i );

if( access( name, 0 ) != -1 ) {
if( !verifyfile( name ) ) {
fclose( ifp );
exit( 0 );
}
}

/************************************************************/
/* create the output file name and open it for writing */
/************************************************************/

if( (ofp = fopen( name, "wb" )) == (FILE *)NULL ) {
printf( "\n\nCan not create the block file [%s].\n", name);
exit( 0 );
}

/************************************************************/
/* copy until eof or size bytes is reached */
/************************************************************/

blockmove( filename, name, ifp, ofp, size );
fclose( ofp );

}

/****************************************************************/
/* close the input file and let the user know we are done */
/****************************************************************/

fclose( ifp );
return( i );
}

/****************************************************************/

int joinfile( char* filename, char *rootname )

{
FILE *ifp; /* input file pointer */
FILE *ofp; /* output file pointer */
int i = 0; /* file counter */
char name[257]; /* name of output file */
long size;

/****************************************************************/
/* verify that the output file does not exist */
/****************************************************************/

if( access( filename, 0 ) != -1 ) {
if( !verifyfile( filename ) ) {
exit( 0 );
}
}

/****************************************************************/
/* open the output file for writing */
/****************************************************************/

if( (ofp = fopen( filename , "wb" )) == (FILE *)NULL ) {
printf( "\n\nCan not open [%s] for writing.\n", filename);
usage();
exit( 0 );
}

do {

/************************************************************/
/* create the input file name and verify it exists */
/************************************************************/

sprintf( name, "%s%c%03d", rootname, '.', i++ );

if( access( name, 0 ) != -1 ) {

/********************************************************/
/* open the input file for reading */
/********************************************************/

if( (ifp = fopen( name, "rb" )) == (FILE *)NULL) {
printf( "\n\nCan not open the block file [%s].\n", name);
exit( 0 );
}

/********************************************************/
/* get the size of the block file */
/********************************************************/

size = filelength( fileno( ifp ) );

/********************************************************/
/* copy the entire contents of the file */
/********************************************************/

blockmove( filename, name, ifp, ofp, size );
fclose( ifp );

}
} while( access( name, 0 ) != -1 );

/****************************************************************/
/* close the input file and let the user know we are done */
/****************************************************************/

fclose( ofp );
return( i );
}

/********************************************************************/

void main( int argc, char *argv[] )

{
long size; /* size of each block file */
int i; /* resulting number of files */
char *str; /* pointer for finding extension */
char filename[257]; /* full file name of original */
char rootname[257]; /* file name less any exension */

/****************************************************************/
/* opening stuff */
/* I do ask that you please don't remove my name. */
/****************************************************************/

printf( "\nWIDGET - Version 1.0.0, Author: K. Scott Piel\n\n" );
printf( "Born of necessity and because I always wanted to make a\n");
printf( "widget. But you can't sell widgets so this program has been\n");
printf( "placed in the public domain. Enjoy, live long and prosper.\n\n");
printf( "Bytes Transfered: 0000000" );

/****************************************************************/
/* verify the command line arguments */
/****************************************************************/

if( argc < 3 ) {
usage();
exit( 0 );
}

/****************************************************************/
/* get the root part of the file name */
/****************************************************************/

strcpy( filename, argv[1] ); /* get the file name */
strcpy( rootname, filename ); /* copy it to the root name */
str = rootname; /* str points to name start */
while( *str ) str++; /* find the end of the name */

/****************************************************************/
/* find a period, start of string or path seperator */
/****************************************************************/

while( str != rootname && *str != '.' && *str != '\\' ) str--;
if( *str == '.' ) *str = '\0';

/****************************************************************/
/* call the appropriate file function */
/****************************************************************/

if( argv[2][0] == 'B' || argv[2][0] == 'b' ) {

/************************************************************/
/* verify that there is a third argument */
/************************************************************/

if( argc < 4 ) {
usage();
exit( 0 );
}

/************************************************************/
/* verify that each member of the third argument is a digit */
/************************************************************/

for( i = 0; argv[3][i]; i++ ) {
if( argv[3][i] < '0' || argv[3][i] > '9' ) {
usage();
exit( 0 );
}
}

/************************************************************/
/* convert the block size to a number */
/************************************************************/

size = 0L;
str = argv[3];
while( *str ) {
size += (long)(*str - '0');
size *= 10L;
str++;
}
size /= 10L;

/************************************************************/
/* break the file into it's component parts */
/************************************************************/

i = breakfile( filename, rootname, size );

} else if( argv[2][0] == 'J' || argv[2][0] == 'j' ) {

/************************************************************/
/* join the components back together */
/************************************************************/

i = joinfile( filename, rootname );

} else {

/************************************************************/
/* invalid mode argument */
/************************************************************/

usage();
exit( 0 );
}

/****************************************************************/
/* let the user know what we did */
/****************************************************************/

printf("\n\nTransfered %ld bytes using %d files.\n", count, i );
puts( "Program terminated normally.\n" );
exit( 0 );
}

/*** End Of File ************************************************************/