home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / config / makecopy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.8 KB  |  168 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include <windows.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <direct.h>
  24. #include <sys/stat.h>
  25. #include <io.h>
  26. #include <fcntl.h>
  27.  
  28. static const char *prog;
  29.  
  30. void Usage(void)
  31. {
  32.     fprintf(stderr, "makecopy: <file> <dir-path>\n");
  33. }
  34.  
  35. void FlipSlashes(char *name)
  36. {
  37.     int i;
  38.  
  39.     /*
  40.     ** Flip any "unix style slashes" into "dos style backslashes" 
  41.     */
  42.     for( i=0; name[i]; i++ ) {
  43.         if( name[i] == '/' ) name[i] = '\\';
  44.     }
  45. }
  46.  
  47. int MakeDir( char *path )
  48. {
  49.     char *cp, *pstr;
  50.     struct stat sb;
  51.  
  52.     pstr = path;
  53.     while( cp = strchr(pstr, '\\') ) {
  54.         *cp = '\0';
  55.         
  56.         if( stat(path, &sb) == 0 && (sb.st_mode & _S_IFDIR) ) {
  57.             /* sub-directory already exists.... */
  58.         } else {
  59.             /* create the new sub-directory */
  60.             printf("+++ makecopy: creating directory %s\n", path);
  61.             if( mkdir(path) < 0 ) {
  62.                 return -1;
  63.             }
  64.         }
  65.         *cp = '\\';
  66.         pstr = cp+1;
  67.     }
  68. }
  69.  
  70. int CopyIfNecessary(char *oldFile, char *newFile)
  71. {
  72.     BY_HANDLE_FILE_INFORMATION hNewInfo;
  73.     BY_HANDLE_FILE_INFORMATION hOldInfo;
  74.  
  75.     HANDLE hFile;
  76.  
  77.     /* Try to open the destination file */
  78.     if ( (hFile = CreateFile(newFile, GENERIC_READ, FILE_SHARE_WRITE, NULL, 
  79.                     OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  80.                     NULL)) != INVALID_HANDLE_VALUE ) {
  81.         if (GetFileInformationByHandle(hFile, &hNewInfo) == FALSE) {
  82.             goto copy_file;
  83.         }
  84.         CloseHandle(hFile);
  85.  
  86.         /* Try to open the source file */
  87.         if ( (hFile = CreateFile(oldFile, GENERIC_READ, FILE_SHARE_WRITE, NULL, 
  88.                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  89.                         NULL)) != INVALID_HANDLE_VALUE ) {
  90.             if (GetFileInformationByHandle(hFile, &hOldInfo) == FALSE) {
  91.                 goto copy_file;
  92.             }
  93.         }
  94.         CloseHandle(hFile);
  95.  
  96.         /*
  97.         ** If both the source and destination were created at the same time
  98.         ** and have the same size then do not copy...
  99.         */
  100.         if ((hOldInfo.ftLastWriteTime.dwLowDateTime == hNewInfo.ftLastWriteTime.dwLowDateTime) &&
  101.             (hOldInfo.ftLastWriteTime.dwHighDateTime == hNewInfo.ftLastWriteTime.dwHighDateTime) &&
  102.             (hOldInfo.nFileSizeLow == hNewInfo.nFileSizeLow) &&
  103.             (hOldInfo.nFileSizeHigh == hNewInfo.nFileSizeHigh)) {
  104.             return 0;
  105.         }
  106.     }
  107.  
  108. copy_file:
  109.     if( ! CopyFile(oldFile, newFile, FALSE) ) {
  110.         return 1;
  111.     }
  112.     return 0;
  113. }
  114.  
  115. int main( int argc, char *argv[] ) 
  116. {
  117.     char old_path[4096];
  118.     char new_path[4096];
  119.     char *oldFileName; /* points to where file name starts in old_path */
  120.     char *newFileName; /* points to where file name starts in new_path */
  121.     WIN32_FIND_DATA findFileData;
  122.     HANDLE hFindFile;
  123.     int rv;
  124.  
  125.     if( argc != 3 ) {
  126.         Usage();
  127.         return 2;
  128.     }
  129.  
  130.     strcpy(old_path, argv[1]);
  131.     FlipSlashes(old_path);
  132.     oldFileName = strrchr(old_path, '\\');
  133.     if (oldFileName) {
  134.         oldFileName++;
  135.     } else {
  136.         oldFileName = old_path;
  137.     }
  138.  
  139.     sprintf(new_path, "%s\\", argv[2]);
  140.     FlipSlashes(new_path);
  141.     newFileName = new_path + strlen(new_path);
  142.  
  143.     if( MakeDir(new_path) < 0 ) {
  144.         fprintf(stderr, "\n+++ makecopy: unable to create directory %s\n", new_path);
  145.         return 1;
  146.     }
  147.  
  148.     hFindFile = FindFirstFile(old_path, &findFileData);
  149.     if (hFindFile == INVALID_HANDLE_VALUE) {
  150.         fprintf(stderr, "\n+++ makecopy: no such file: %s\n", argv[1]);
  151.         return 1;
  152.     }
  153.  
  154.     printf("+++ makecopy: Installing %s into directory %s\n", argv[1], argv[2]);
  155.  
  156.     do {
  157.         strcpy(oldFileName, findFileData.cFileName);
  158.         strcpy(newFileName, findFileData.cFileName);
  159.         rv = CopyIfNecessary(old_path, new_path);
  160.         if (rv != 0) {
  161.             break;
  162.         }
  163.     } while (FindNextFile(hFindFile, &findFileData) != 0);
  164.  
  165.     FindClose(hFindFile);
  166.     return rv;
  167. }
  168.