Rev 2073 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*============================================================================** Copyright (C) 1998-2012 Vix Technology, All rights reserved**============================================================================**** Project/Product :** Filename : CopyFile.c** Author(s) : DDP**** Description :** The program mimics - but a lot faster a Jats Makefile fragment** The program will:**** Operate in two modes** CopyFile** DeleteFile**** CopyFile (5 args)** 1) Display the ----- Text dest** 2) Error if the source is not found** 3) Create target path if not alraedy existing** 4) Copy the source to the target - overwritting the target** 5) Set File mode** 6) Test for file existence**** DeleteFile (2 args)** 1) Display the ----- Text dest** 2) Delete the target file**** Make paths use '/'****** Information :** Compiler : ANSI C++** Target :*****==========================================================================*/#include <stdio.h>#include <windows.h>#define INVALID_FILE_ATTIBUTES ((DWORD) -1)VOID ErrorExit (LPTSTR lpszMessage, LPTSTR lpszMessage2);void createPaths ( char *path );/*----------------------------------------------------------------------------** FUNCTION : main**** DESCRIPTION : Main entry points****** INPUTS : Arguments are fixed** Text - Text to output** dest - Target Path** file - Source path** path - Target dir [Not used]** fmode - File mode**** RETURNS : 0 - All is good**----------------------------------------------------------------------------*/main( int argc, char *argv[] ){DWORD rv;char verbose = 0;char copyMode = 0;/*** Allow first argument to be a '-v'*/if ( argc > 1 && argv[1][0] == '-' && argv[1][1] == 'v'){int ii;for ( ii = 0; ii < argc ; ii++ ){if ( ii != 1 )fprintf(stderr, " -Arg%d:%s:\n", ii, argv[ii] );}fflush(stderr) ;verbose = 1;argc--;argv++;}/*** Determine mode of operation** Copy or Delete*/if ( argc == 6 ) {copyMode = 1;} else if ( argc == 3) {copyMode = 0;} else {ErrorExit("Incorrect argument count","");}/*** Display user text*/fprintf(stderr, "---- %s %s\n", argv[1], argv[2]);fflush(stderr) ;/*** Check that the source is a file*/if ( copyMode ){rv = GetFileAttributes( argv[3] );if ( rv == INVALID_FILE_ATTIBUTES ){// Need to be a better messageErrorExit("Error: Source File not found: ", argv[3]);}}/*** Remove the ReadOnly attribute on the dest file*/rv = GetFileAttributes( argv[2] );if ( rv != INVALID_FILE_ATTIBUTES ){if ( verbose )fprintf(stderr, "FileExists with attr: %ld\n", rv);if ( rv & FILE_ATTRIBUTE_READONLY ){rv &= ~FILE_ATTRIBUTE_READONLY;rv = SetFileAttributes( argv[2], rv );if ( rv == 0 ){ErrorExit("Error: Attempt to allow write access: ", argv[2]);}}if (! DeleteFile( argv[2] ) ){ErrorExit("Error: Deleting file: ", argv[2]);}}if ( copyMode ){/*** Create directories** Use the path to the target - not the provided directory** as the createPaths function will not create the last element*/createPaths( argv[2] );/*** Copy the file*/if ( ! CopyFile( argv[3], argv[2], FALSE ) ){rv = GetLastError();fprintf(stderr, "CopyFileLast Error: %ld\n", rv);ErrorExit("Error: Copy Error: ", argv[3]);}/*** Test for files existence*/rv = GetFileAttributes( argv[2] );if ( rv == INVALID_FILE_ATTIBUTES ){// Need to be a better messageErrorExit("Error: File not found after copy: ", argv[2]);}/*** Set the files attributes** Assume read-only*/rv |= FILE_ATTRIBUTE_READONLY;rv &= ~FILE_ATTRIBUTE_NORMAL;rv = SetFileAttributes( argv[2], rv );if ( rv == 0 ){ErrorExit("Error: Setting ReadOnly: ", argv[2]);}}return 0;}/*----------------------------------------------------------------------------** FUNCTION : createPaths**** DESCRIPTION : Create the path to the target****** INPUTS : path**** RETURNS : Will not return in error**----------------------------------------------------------------------------*/void createPaths ( char *path ){DWORD rv;char *ptr = path;while ( *ptr ){if ( *ptr == '\\' || *ptr == '/' ){*ptr = 0;//printf( "Create: %s\n", path );if ( ! CreateDirectory ( path, NULL )){rv = GetLastError();if ( rv != ERROR_ALREADY_EXISTS )ErrorExit("Error: Cound not create directories:", path);}*ptr = '\\';}ptr++;}}/*----------------------------------------------------------------------------** FUNCTION : ErrorExit**** DESCRIPTION : Error processing** Report an error and terminate process****** INPUTS : lpszMessage - Message to display**** RETURNS : Does't return** Will exit with bad code**----------------------------------------------------------------------------*/VOID ErrorExit (LPTSTR lpszMessage, LPTSTR lpszMessage2){fprintf(stderr, "%s%s\n", lpszMessage,lpszMessage2);fflush(stderr) ;ExitProcess(-1);}