netscape-revival
config/makecopy.c
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <direct.h>
#include <sys/stat.h>
#include <io.h>
#include <fcntl.h>
static const char *prog;
void Usage(void)
{
fprintf(stderr, "MAKECOPY: <file> <dir-path>\n");
}
void FlipSlashes(char *name)
{
int i;
/*
** Flip any "unix style slashes" into "dos style backslashes"
*/
for( i=0; name[i]; i++ ) {
if( name[i] == '/' ) name[i] = '\\';
}
}
void GetPathName(char *file, char *new_path)
{
int i;
i = strlen(file);
for( i=strlen(file); i && file[i] != '\\'; i--);
strncpy(new_path, file, i);
if( new_path[i] != '\\' ) {
new_path[i++] = '\\';
}
new_path[i] = '\0';
}
int MakeDir( char *path )
{
char *cp, *pstr;
struct stat sb;
pstr = path;
while( cp = strchr(pstr, '\\') ) {
*cp = '\0';
if( stat(path, &sb) == 0 && (sb.st_mode & _S_IFDIR) ) {
/* sub-directory already exists.... */
} else {
/* create the new sub-directory */
printf("+++ MAKECOPY: creating directory %s\n", path);
if( mkdir(path) < 0 ) {
return -1;
}
}
*cp = '\\';
pstr = cp+1;
}
}
int CopyIfNecessary(char *oldFile, char *newFile, char *path)
{
BY_HANDLE_FILE_INFORMATION hNewInfo;
BY_HANDLE_FILE_INFORMATION hOldInfo;
HANDLE hFile;
// Try to open the destination file
if ( (hFile = CreateFile(newFile, GENERIC_READ, FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL)) != INVALID_HANDLE_VALUE ) {
if (GetFileInformationByHandle(hFile, &hNewInfo) == FALSE) {
goto copy_file;
}
CloseHandle(hFile);
// Try to open the source file
if ( (hFile = CreateFile(oldFile, GENERIC_READ, FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL)) != INVALID_HANDLE_VALUE ) {
if (GetFileInformationByHandle(hFile, &hOldInfo) == FALSE) {
goto copy_file;
}
}
CloseHandle(hFile);
//
// If both the source and destination were created at the same time
// and have the same size then do not copy...
//
if ((hOldInfo.ftLastWriteTime.dwLowDateTime == hNewInfo.ftLastWriteTime.dwLowDateTime) &&
(hOldInfo.ftLastWriteTime.dwHighDateTime == hNewInfo.ftLastWriteTime.dwHighDateTime) &&
(hOldInfo.nFileSizeLow == hNewInfo.nFileSizeLow) &&
(hOldInfo.nFileSizeHigh == hNewInfo.nFileSizeHigh)) {
return 0;
}
}
copy_file:
printf("+++ MAKECOPY: Installing %s into directory %s", oldFile, path);
if( ! CopyFile(oldFile, newFile, FALSE) ) {
return 1;
}
return 0;
}
static char new_file[4096];
static char new_path[4096];
int main( int argc, char *argv[] )
{
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
if( argc != 3 ) {
Usage();
return 2;
}
_splitpath(argv[1], NULL, NULL, fname, ext);
sprintf(new_file, "%s\\%s%s", argv[2], fname, ext);
FlipSlashes(new_file);
sprintf(new_path, "%s\\", argv[2]);
FlipSlashes(new_path);
if( MakeDir(new_path) < 0 ) {
fprintf(stderr, "\n+++ MAKECOPY: unable to create directory %s\n", new_path);
return 1;
}
return CopyIfNecessary(argv[1], new_file, new_path);
}