Patch from Mike Kaplinskiy <mike.kaplinskiy@gmail.com> implementing trim, trimLeft...
[mozilla-central.git] / config / mangle.c
blobb4e9458113679fc029248accaf9c52df0e184426
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include <windows.h>
39 #include <stdio.h>
40 #include <stdlib.h>
42 HANDLE hMangleFile;
44 void Usage(void)
46 fprintf(stderr, "MANGLE: <file>\n");
49 BOOL MangleFile( const char *real_name, const char *mangle_name )
51 int len;
52 DWORD dwWritten;
53 char buffer[2048];
55 if( mangle_name && *mangle_name && strcmpi(real_name, mangle_name) ) {
56 printf("Mangle: renaming %s to %s\n", real_name, mangle_name);
58 if( ! MoveFile(real_name, "X_MANGLE.TMP") ) {
59 fprintf(stderr, "MANGLE: cannot rename %s to X_MANGLE.TMP\n",
60 real_name);
61 return FALSE;
64 if( ! MoveFile("X_MANGLE.TMP", mangle_name) ) {
65 MoveFile("X_MANGLE.TMP", real_name);
66 fprintf(stderr, "MANGLE: cannot rename X_MANGLE.TMP to %s\n",
67 mangle_name);
68 return FALSE;
71 len = sprintf(buffer, "mv %s %s\r\n", mangle_name, real_name);
73 if( (WriteFile( hMangleFile, buffer, len, &dwWritten, NULL ) == FALSE) ||
74 (dwWritten != len) ) {
75 fprintf(stderr, "MANGLE: error writing to UNMANGLE.BAT\n");
76 return FALSE;
79 return TRUE;
83 int main( int argc, char *argv[] )
85 WIN32_FIND_DATA find_data;
86 HANDLE hFoundFile;
88 if( argc != 1 ) {
89 Usage();
90 return 2;
94 hMangleFile = CreateFile("unmangle.bat", /* name */
95 GENERIC_READ|GENERIC_WRITE, /* access mode */
96 0, /* share mode */
97 NULL, /* security descriptor */
98 CREATE_NEW, /* how to create */
99 FILE_ATTRIBUTE_NORMAL, /* file attributes */
100 NULL ); /* template file */
102 if( hMangleFile == INVALID_HANDLE_VALUE ) {
103 if( GetLastError() == ERROR_FILE_EXISTS ) {
104 fprintf(stderr, "MANGLE: UNMANGLE.BAT already exists\n");
105 } else {
106 fprintf(stderr, "MANGLE: cannot open UNMANGLE.BAT\n");
108 return 1;
111 if( (hFoundFile = FindFirstFile("*.*", &find_data)) == INVALID_HANDLE_VALUE ) {
112 fprintf(stderr, "MANGLE: cannot read directory\n");
113 return 1;
116 do {
117 if( !MangleFile(find_data.cFileName, find_data.cAlternateFileName) ) {
118 fprintf(stderr, "MANGLE: cannot rename %s to %s\n",
119 find_data.cFileName, find_data.cAlternateFileName );
121 FindClose( hFoundFile );
122 CloseHandle( hMangleFile );
123 return 1;
125 } while( FindNextFile(hFoundFile, &find_data) );
126 FindClose( hFoundFile );
129 int len;
130 DWORD dwWritten;
131 char buffer[255];
133 len = sprintf(buffer, "del unmangle.bat\r\n");
134 WriteFile ( hMangleFile, buffer, len, &dwWritten, NULL );
136 CloseHandle( hMangleFile );
138 return 0;