Back to index

lightning-sunbird  0.9+nobinonly
ifuncns.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Sean Su <ssu@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "ifuncns.h"
00041 #include "extra.h"
00042 
00043 BOOL SearchForUninstallKeys(char *szStringToMatch)
00044 {
00045   char      szBuf[MAX_BUF];
00046   char      szStringToMatchLowerCase[MAX_BUF];
00047   char      szBufKey[MAX_BUF];
00048   char      szSubKey[MAX_BUF];
00049   HKEY      hkHandle;
00050   BOOL      bFound;
00051   DWORD     dwIndex;
00052   DWORD     dwSubKeySize;
00053   DWORD     dwTotalSubKeys;
00054   DWORD     dwTotalValues;
00055   FILETIME  ftLastWriteFileTime;
00056   char      szWRMSUninstallKeyPath[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
00057   char      szWRMSUninstallName[] =  "UninstallString";
00058 
00059   lstrcpyn(szStringToMatchLowerCase, szStringToMatch, sizeof(szStringToMatchLowerCase));
00060   CharLower(szStringToMatchLowerCase);
00061 
00062   bFound = FALSE;
00063   if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, szWRMSUninstallKeyPath, 0, KEY_READ, &hkHandle) != ERROR_SUCCESS)
00064     return(bFound);
00065 
00066   dwTotalSubKeys = 0;
00067   dwTotalValues  = 0;
00068   RegQueryInfoKey(hkHandle, NULL, NULL, NULL, &dwTotalSubKeys, NULL, NULL, &dwTotalValues, NULL, NULL, NULL, NULL);
00069   for(dwIndex = 0; dwIndex < dwTotalSubKeys; dwIndex++)
00070   {
00071     dwSubKeySize = sizeof(szSubKey);
00072     if(RegEnumKeyEx(hkHandle, dwIndex, szSubKey, &dwSubKeySize, NULL, NULL, NULL, &ftLastWriteFileTime) == ERROR_SUCCESS)
00073     {
00074       wsprintf(szBufKey, "%s\\%s", szWRMSUninstallKeyPath, szSubKey);
00075       GetWinReg(HKEY_LOCAL_MACHINE, szBufKey, szWRMSUninstallName, szBuf, sizeof(szBuf));
00076       CharLower(szBuf);
00077       if(strstr(szBuf, szStringToMatchLowerCase) != NULL)
00078       {
00079         bFound = TRUE;
00080 
00081         /* found one subkey. break out of the for() loop */
00082         break;
00083       }
00084     }
00085   }
00086 
00087   RegCloseKey(hkHandle);
00088   return(bFound);
00089 }
00090 
00091 HRESULT FileMove(LPSTR szFrom, LPSTR szTo)
00092 {
00093   HANDLE          hFile;
00094   WIN32_FIND_DATA fdFile;
00095   char            szFromDir[MAX_BUF];
00096   char            szFromTemp[MAX_BUF];
00097   char            szToTemp[MAX_BUF];
00098   char            szBuf[MAX_BUF];
00099   BOOL            bFound;
00100 
00101   /* From file path exists and To file path does not exist */
00102   if((FileExists(szFrom)) && (!FileExists(szTo)))
00103   {
00104     MoveFile(szFrom, szTo);
00105     return(FO_SUCCESS);
00106   }
00107   /* From file path exists and To file path exists */
00108   if(FileExists(szFrom) && FileExists(szTo))
00109   {
00110     /* Since the To file path exists, assume it to be a directory and proceed.      */
00111     /* We don't care if it's a file.  If it is a file, then config.ini needs to be  */
00112     /* corrected to remove the file before attempting a MoveFile().                 */
00113     lstrcpy(szToTemp, szTo);
00114     AppendBackSlash(szToTemp, sizeof(szToTemp));
00115     ParsePath(szFrom, szBuf, MAX_BUF, PP_FILENAME_ONLY);
00116     lstrcat(szToTemp, szBuf);
00117     MoveFile(szFrom, szToTemp);
00118     return(FO_SUCCESS);
00119   }
00120 
00121   ParsePath(szFrom, szFromDir, MAX_BUF, PP_PATH_ONLY);
00122 
00123   if((hFile = FindFirstFile(szFrom, &fdFile)) == INVALID_HANDLE_VALUE)
00124     bFound = FALSE;
00125   else
00126     bFound = TRUE;
00127 
00128   while(bFound)
00129   {
00130     if((lstrcmpi(fdFile.cFileName, ".") != 0) && (lstrcmpi(fdFile.cFileName, "..") != 0))
00131     {
00132       /* create full path string including filename for source */
00133       lstrcpy(szFromTemp, szFromDir);
00134       AppendBackSlash(szFromTemp, sizeof(szFromTemp));
00135       lstrcat(szFromTemp, fdFile.cFileName);
00136 
00137       /* create full path string including filename for destination */
00138       lstrcpy(szToTemp, szTo);
00139       AppendBackSlash(szToTemp, sizeof(szToTemp));
00140       lstrcat(szToTemp, fdFile.cFileName);
00141 
00142       MoveFile(szFromTemp, szToTemp);
00143     }
00144 
00145     bFound = FindNextFile(hFile, &fdFile);
00146   }
00147 
00148   FindClose(hFile);
00149   return(FO_SUCCESS);
00150 }
00151 
00152 HRESULT FileCopy(LPSTR szFrom, LPSTR szTo, BOOL bFailIfExists)
00153 {
00154   HANDLE          hFile;
00155   WIN32_FIND_DATA fdFile;
00156   char            szFromDir[MAX_BUF];
00157   char            szFromTemp[MAX_BUF];
00158   char            szToTemp[MAX_BUF];
00159   char            szBuf[MAX_BUF];
00160   BOOL            bFound;
00161 
00162   if(FileExists(szFrom))
00163   {
00164     /* The file in the From file path exists */
00165     ParsePath(szFrom, szBuf, MAX_BUF, PP_FILENAME_ONLY);
00166     lstrcpy(szToTemp, szTo);
00167     AppendBackSlash(szToTemp, sizeof(szToTemp));
00168     lstrcat(szToTemp, szBuf);
00169     CopyFile(szFrom, szToTemp, bFailIfExists);
00170     return(FO_SUCCESS);
00171   }
00172 
00173   /* The file in the From file path does not exist.  Assume to contain wild args and */
00174   /* proceed acordingly.                                                             */
00175   ParsePath(szFrom, szFromDir, MAX_BUF, PP_PATH_ONLY);
00176 
00177   if((hFile = FindFirstFile(szFrom, &fdFile)) == INVALID_HANDLE_VALUE)
00178     bFound = FALSE;
00179   else
00180     bFound = TRUE;
00181 
00182   while(bFound)
00183   {
00184     if((lstrcmpi(fdFile.cFileName, ".") != 0) && (lstrcmpi(fdFile.cFileName, "..") != 0))
00185     {
00186       /* create full path string including filename for source */
00187       lstrcpy(szFromTemp, szFromDir);
00188       AppendBackSlash(szFromTemp, sizeof(szFromTemp));
00189       lstrcat(szFromTemp, fdFile.cFileName);
00190 
00191       /* create full path string including filename for destination */
00192       lstrcpy(szToTemp, szTo);
00193       AppendBackSlash(szToTemp, sizeof(szToTemp));
00194       lstrcat(szToTemp, fdFile.cFileName);
00195 
00196       CopyFile(szFromTemp, szToTemp, bFailIfExists);
00197     }
00198 
00199     bFound = FindNextFile(hFile, &fdFile);
00200   }
00201 
00202   FindClose(hFile);
00203   return(FO_SUCCESS);
00204 }
00205 
00206 HRESULT CreateDirectoriesAll(char* szPath)
00207 {
00208   int     i;
00209   int     iLen = lstrlen(szPath);
00210   char    szCreatePath[MAX_BUF];
00211   HRESULT hrResult;
00212 
00213   ZeroMemory(szCreatePath, MAX_BUF);
00214   memcpy(szCreatePath, szPath, iLen);
00215   for(i = 0; i < iLen; i++)
00216   {
00217     if((iLen > 1) &&
00218       ((i != 0) && ((szPath[i] == '\\') || (szPath[i] == '/'))) &&
00219       (!((szPath[0] == '\\') && (i == 1)) && !((szPath[1] == ':') && (i == 2))))
00220     {
00221       szCreatePath[i] = '\0';
00222       hrResult        = CreateDirectory(szCreatePath, NULL);
00223       szCreatePath[i] = szPath[i];
00224     }
00225   }
00226   return(hrResult);
00227 }
00228 
00229 HRESULT FileDelete(LPSTR szDestination)
00230 {
00231   HANDLE          hFile;
00232   WIN32_FIND_DATA fdFile;
00233   char            szBuf[MAX_BUF];
00234   char            szPathOnly[MAX_BUF];
00235   BOOL            bFound;
00236 
00237   if(FileExists(szDestination))
00238   {
00239     /* The file in the From file path exists */
00240     DeleteFile(szDestination);
00241     return(FO_SUCCESS);
00242   }
00243 
00244   /* The file in the From file path does not exist.  Assume to contain wild args and */
00245   /* proceed acordingly.                                                             */
00246   ParsePath(szDestination, szPathOnly, MAX_BUF, PP_PATH_ONLY);
00247 
00248   if((hFile = FindFirstFile(szDestination, &fdFile)) == INVALID_HANDLE_VALUE)
00249     bFound = FALSE;
00250   else
00251     bFound = TRUE;
00252 
00253   while(bFound)
00254   {
00255     if(!(fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
00256     {
00257       lstrcpy(szBuf, szPathOnly);
00258       AppendBackSlash(szBuf, sizeof(szBuf));
00259       lstrcat(szBuf, fdFile.cFileName);
00260 
00261       DeleteFile(szBuf);
00262     }
00263 
00264     bFound = FindNextFile(hFile, &fdFile);
00265   }
00266 
00267   FindClose(hFile);
00268   return(FO_SUCCESS);
00269 }
00270 
00271 HRESULT DirectoryRemove(LPSTR szDestination, BOOL bRemoveSubdirs)
00272 {
00273   HANDLE          hFile;
00274   WIN32_FIND_DATA fdFile;
00275   char            szDestTemp[MAX_BUF];
00276   BOOL            bFound;
00277 
00278   if(!FileExists(szDestination))
00279     return(FO_SUCCESS);
00280 
00281   if(bRemoveSubdirs == TRUE)
00282   {
00283     lstrcpy(szDestTemp, szDestination);
00284     AppendBackSlash(szDestTemp, sizeof(szDestTemp));
00285     lstrcat(szDestTemp, "*");
00286 
00287     bFound = TRUE;
00288     hFile = FindFirstFile(szDestTemp, &fdFile);
00289     while((hFile != INVALID_HANDLE_VALUE) && (bFound == TRUE))
00290     {
00291       if((lstrcmpi(fdFile.cFileName, ".") != 0) && (lstrcmpi(fdFile.cFileName, "..") != 0))
00292       {
00293         /* create full path */
00294         lstrcpy(szDestTemp, szDestination);
00295         AppendBackSlash(szDestTemp, sizeof(szDestTemp));
00296         lstrcat(szDestTemp, fdFile.cFileName);
00297 
00298         if(fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
00299         {
00300           DirectoryRemove(szDestTemp, bRemoveSubdirs);
00301         }
00302         else
00303         {
00304           DeleteFile(szDestTemp);
00305         }
00306       }
00307 
00308       bFound = FindNextFile(hFile, &fdFile);
00309     }
00310 
00311     FindClose(hFile);
00312   }
00313   
00314   RemoveDirectory(szDestination);
00315   return(FO_SUCCESS);
00316 }
00317 
00318 HKEY ParseRootKey(LPSTR szRootKey)
00319 {
00320   HKEY hkRootKey;
00321 
00322   if(lstrcmpi(szRootKey, "HKEY_CURRENT_CONFIG") == 0)
00323     hkRootKey = HKEY_CURRENT_CONFIG;
00324   else if(lstrcmpi(szRootKey, "HKEY_CURRENT_USER") == 0)
00325     hkRootKey = HKEY_CURRENT_USER;
00326   else if(lstrcmpi(szRootKey, "HKEY_LOCAL_MACHINE") == 0)
00327     hkRootKey = HKEY_LOCAL_MACHINE;
00328   else if(lstrcmpi(szRootKey, "HKEY_USERS") == 0)
00329     hkRootKey = HKEY_USERS;
00330   else if(lstrcmpi(szRootKey, "HKEY_PERFORMANCE_DATA") == 0)
00331     hkRootKey = HKEY_PERFORMANCE_DATA;
00332   else if(lstrcmpi(szRootKey, "HKEY_DYN_DATA") == 0)
00333     hkRootKey = HKEY_DYN_DATA;
00334   else /* HKEY_CLASSES_ROOT */
00335     hkRootKey = HKEY_CLASSES_ROOT;
00336 
00337   return(hkRootKey);
00338 }
00339 
00340 LPSTR GetStringRootKey(HKEY hkRootKey, LPSTR szString, DWORD dwStringSize)
00341 {
00342   if(hkRootKey == HKEY_CURRENT_CONFIG)
00343   {
00344     if(sizeof("HKEY_CURRENT_CONFIG") <= dwStringSize)
00345       lstrcpy(szString, "HKEY_CURRENT_CONFIG");
00346     else
00347       return(NULL);
00348   }
00349   else if(hkRootKey == HKEY_CURRENT_USER)
00350   {
00351     if(sizeof("HKEY_CURRENT_USER") <= dwStringSize)
00352       lstrcpy(szString, "HKEY_CURRENT_USER");
00353     else
00354       return(NULL);
00355   }
00356   else if(hkRootKey == HKEY_LOCAL_MACHINE)
00357   {
00358     if(sizeof("HKEY_LOCAL_MACHINE") <= dwStringSize)
00359       lstrcpy(szString, "HKEY_LOCAL_MACHINE");
00360     else
00361       return(NULL);
00362   }
00363   else if(hkRootKey == HKEY_USERS)
00364   {
00365     if(sizeof("HKEY_USERS") <= dwStringSize)
00366       lstrcpy(szString, "HKEY_USERS");
00367     else
00368       return(NULL);
00369   }
00370   else if(hkRootKey == HKEY_PERFORMANCE_DATA)
00371   {
00372     if(sizeof("HKEY_PERFORMANCE_DATA") <= dwStringSize)
00373       lstrcpy(szString, "HKEY_PERFORMANCE_DATA");
00374     else
00375       return(NULL);
00376   }
00377   else if(hkRootKey == HKEY_DYN_DATA)
00378   {
00379     if(sizeof("HKEY_DYN_DATA") <= dwStringSize)
00380       lstrcpy(szString, "HKEY_DYN_DATA");
00381     else
00382       return(NULL);
00383   }
00384   else
00385   {
00386     if(sizeof("HKEY_CLASSES_ROOT") <= dwStringSize)
00387       lstrcpy(szString, "HKEY_CLASSES_ROOT");
00388     else
00389       return(NULL);
00390   }
00391 
00392   return(szString);
00393 }
00394 
00395 BOOL WinRegKeyExists(HKEY hkRootKey, LPSTR szKey)
00396 {
00397   HKEY  hkResult;
00398   BOOL  bKeyExists = FALSE;
00399 
00400   if(RegOpenKeyEx(hkRootKey, szKey, 0, KEY_READ, &hkResult) == ERROR_SUCCESS)
00401   {
00402     bKeyExists = TRUE;
00403     RegCloseKey(hkResult);
00404   }
00405 
00406   return(bKeyExists);
00407 }
00408