home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / config / mangle.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.8 KB  |  121 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.  
  23. HANDLE hMangleFile;
  24.  
  25. void Usage(void)
  26. {
  27.     fprintf(stderr, "MANGLE: <file>\n");
  28. }
  29.  
  30. BOOL MangleFile( const char *real_name, const char *mangle_name ) 
  31. {
  32.     int len;
  33.     DWORD dwWritten;
  34.     char buffer[2048];
  35.  
  36.     if( mangle_name && *mangle_name && strcmpi(real_name, mangle_name) ) {
  37.         printf("Mangle: renaming %s to %s\n", real_name, mangle_name);
  38.  
  39.         if( ! MoveFile(real_name, "X_MANGLE.TMP") ) {
  40.             fprintf(stderr, "MANGLE: cannot rename %s to X_MANGLE.TMP\n", 
  41.                     real_name);
  42.                 return FALSE;
  43.         }
  44.  
  45.         if( ! MoveFile("X_MANGLE.TMP", mangle_name) ) {
  46.             MoveFile("X_MANGLE.TMP", real_name);
  47.             fprintf(stderr, "MANGLE: cannot rename X_MANGLE.TMP to %s\n", 
  48.                     mangle_name);
  49.             return FALSE;
  50.         }
  51.  
  52.         len = sprintf(buffer, "mv %s %s\r\n", mangle_name, real_name);
  53.  
  54.         if( (WriteFile( hMangleFile, buffer, len, &dwWritten, NULL ) == FALSE) ||
  55.             (dwWritten != len) ) {
  56.             fprintf(stderr, "MANGLE: error writing to UNMANGLE.BAT\n");
  57.             return FALSE;
  58.         }
  59.     }
  60.     return TRUE;
  61. }
  62.  
  63.  
  64. int main( int argc, char *argv[] ) 
  65. {
  66.     WIN32_FIND_DATA find_data;
  67.     HANDLE hFoundFile;
  68.  
  69.     if( argc != 1 ) {
  70.         Usage();
  71.         return 2;
  72.     }
  73.  
  74.  
  75.     hMangleFile = CreateFile("unmangle.bat",    /* name                */
  76.                     GENERIC_READ|GENERIC_WRITE, /* access mode         */
  77.                     0,                          /* share mode          */
  78.                     NULL,                       /* security descriptor */
  79.                     CREATE_NEW,                 /* how to create       */
  80.                     FILE_ATTRIBUTE_NORMAL,      /* file attributes     */
  81.                     NULL );                     /* template file       */
  82.  
  83.     if( hMangleFile == INVALID_HANDLE_VALUE ) {
  84.         if( GetLastError() == ERROR_FILE_EXISTS ) {
  85.             fprintf(stderr, "MANGLE: UNMANGLE.BAT already exists\n");
  86.         } else {
  87.             fprintf(stderr, "MANGLE: cannot open UNMANGLE.BAT\n");
  88.         }
  89.         return 1;
  90.     }
  91.  
  92.     if( (hFoundFile = FindFirstFile("*.*", &find_data)) == INVALID_HANDLE_VALUE ) {
  93.         fprintf(stderr, "MANGLE: cannot read directory\n");
  94.         return 1;
  95.     }
  96.  
  97.     do {
  98.         if( !MangleFile(find_data.cFileName, find_data.cAlternateFileName) ) {
  99.             fprintf(stderr, "MANGLE: cannot rename %s to %s\n",
  100.                     find_data.cFileName, find_data.cAlternateFileName );
  101.     
  102.             FindClose( hFoundFile );
  103.             CloseHandle( hMangleFile );
  104.             return 1;
  105.         }
  106.     } while( FindNextFile(hFoundFile, &find_data) );
  107.     FindClose( hFoundFile );
  108.  
  109.     {
  110.         int len;
  111.         DWORD dwWritten;
  112.         char buffer[255];
  113.  
  114.         len = sprintf(buffer, "del unmangle.bat\r\n");
  115.         WriteFile  ( hMangleFile, buffer, len, &dwWritten, NULL );
  116.     }
  117.     CloseHandle( hMangleFile );
  118.  
  119.     return 0;
  120. }
  121.