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 "uninstall.h"
00041 #include "extra.h"
00042 #include "ifuncns.h"
00043 
00044 BOOL SearchForUninstallKeys(char *szStringToMatch)
00045 {
00046 #ifdef OLDCODE
00047   char      szBuf[MAX_BUF];
00048   char      szStringToMatchLowerCase[MAX_BUF];
00049   char      szBufKey[MAX_BUF];
00050   char      szSubKey[MAX_BUF];
00051   HKEY      hkHandle;
00052   BOOL      bFound;
00053   DWORD     dwIndex;
00054   DWORD     dwSubKeySize;
00055   DWORD     dwTotalSubKeys;
00056   DWORD     dwTotalValues;
00057   FILETIME  ftLastWriteFileTime;
00058   char      szWRMSUninstallKeyPath[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
00059   char      szWRMSUninstallName[] =  "UninstallString";
00060 
00061   strcpyn(szStringToMatchLowerCase, szStringToMatch, sizeof(szStringToMatchLowerCase));
00062   CharLower(szStringToMatchLowerCase);
00063 
00064   bFound = FALSE;
00065   if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, szWRMSUninstallKeyPath, 0, KEY_READ, &hkHandle) != ERROR_SUCCESS)
00066     return(bFound);
00067 
00068   dwTotalSubKeys = 0;
00069   dwTotalValues  = 0;
00070   RegQueryInfoKey(hkHandle, NULL, NULL, NULL, &dwTotalSubKeys, NULL, NULL, &dwTotalValues, NULL, NULL, NULL, NULL);
00071   for(dwIndex = 0; dwIndex < dwTotalSubKeys; dwIndex++)
00072   {
00073     dwSubKeySize = sizeof(szSubKey);
00074     if(RegEnumKeyEx(hkHandle, dwIndex, szSubKey, &dwSubKeySize, NULL, NULL, NULL, &ftLastWriteFileTime) == ERROR_SUCCESS)
00075     {
00076       wsprintf(szBufKey, "%s\\%s", szWRMSUninstallKeyPath, szSubKey);
00077       GetWinReg(HKEY_LOCAL_MACHINE, szBufKey, szWRMSUninstallName, szBuf, sizeof(szBuf));
00078       CharLower(szBuf);
00079       if(strstr(szBuf, szStringToMatchLowerCase) != NULL)
00080       {
00081         bFound = TRUE;
00082 
00083         /* found one subkey. break out of the for() loop */
00084         break;
00085       }
00086     }
00087   }
00088 
00089   RegCloseKey(hkHandle);
00090   return(bFound);
00091 #endif
00092 }
00093 
00094 HRESULT FileMove(PSZ szFrom, PSZ szTo)
00095 {
00096   HDIR            hFile;
00097   FILEFINDBUF3    fdFile;
00098   ULONG           ulFindCount;
00099   ULONG           ulAttrs;
00100   char            szFromDir[MAX_BUF];
00101   char            szFromTemp[MAX_BUF];
00102   char            szToTemp[MAX_BUF];
00103   char            szBuf[MAX_BUF];
00104   BOOL            bFound;
00105 
00106   /* From file path exists and To file path does not exist */
00107   if((FileExists(szFrom)) && (!FileExists(szTo)))
00108   {
00109     
00110     /* @MAK - need to handle OS/2 case where they are not the same drive*/
00111     DosMove(szFrom, szTo);
00112 
00113     return(FO_SUCCESS);
00114   }
00115   /* From file path exists and To file path exists */
00116   if(FileExists(szFrom) && FileExists(szTo))
00117   {
00118     /* Since the To file path exists, assume it to be a directory and proceed.      */
00119     /* We don't care if it's a file.  If it is a file, then config.ini needs to be  */
00120     /* corrected to remove the file before attempting a MoveFile().                 */
00121     strcpy(szToTemp, szTo);
00122     AppendBackSlash(szToTemp, sizeof(szToTemp));
00123     ParsePath(szFrom, szBuf, MAX_BUF, PP_FILENAME_ONLY);
00124     strcat(szToTemp, szBuf);
00125     DosMove(szFrom, szToTemp);
00126     return(FO_SUCCESS);
00127   }
00128 
00129   ParsePath(szFrom, szFromDir, MAX_BUF, PP_PATH_ONLY);
00130 
00131   strcat(szFrom, "*.*");
00132   ulFindCount = 1;
00133   hFile = HDIR_CREATE;
00134   ulAttrs = FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY | FILE_ARCHIVED;
00135   if((DosFindFirst(szFrom, &hFile, ulAttrs, &fdFile, sizeof(fdFile), &ulFindCount, FIL_STANDARD)) != NO_ERROR)
00136     bFound = FALSE;
00137   else
00138     bFound = TRUE;
00139 
00140   while(bFound)
00141   {
00142     if((stricmp(fdFile.achName, ".") != 0) && (stricmp(fdFile.achName, "..") != 0))
00143     {
00144       /* create full path string including filename for source */
00145       strcpy(szFromTemp, szFromDir);
00146       AppendBackSlash(szFromTemp, sizeof(szFromTemp));
00147       strcat(szFromTemp, fdFile.achName);
00148 
00149       /* create full path string including filename for destination */
00150       strcpy(szToTemp, szTo);
00151       AppendBackSlash(szToTemp, sizeof(szToTemp));
00152       strcat(szToTemp, fdFile.achName);
00153 
00154       DosMove(szFromTemp, szToTemp);
00155     }
00156 
00157     ulFindCount = 1;
00158     if (DosFindNext(hFile, &fdFile, sizeof(fdFile), &ulFindCount) == NO_ERROR) {
00159       bFound = TRUE;
00160     } else {
00161       bFound = FALSE;
00162     }
00163   }
00164 
00165   DosFindClose(hFile);
00166   return(FO_SUCCESS);
00167 }
00168 
00169 HRESULT FileCopy(PSZ szFrom, PSZ szTo, BOOL bFailIfExists)
00170 {
00171   HDIR            hFile;
00172   FILEFINDBUF3    fdFile;
00173   ULONG           ulFindCount;
00174   ULONG           ulAttrs;
00175   char            szFromDir[MAX_BUF];
00176   char            szFromTemp[MAX_BUF];
00177   char            szToTemp[MAX_BUF];
00178   char            szBuf[MAX_BUF];
00179   BOOL            bFound;
00180 
00181   if(FileExists(szFrom))
00182   {
00183     /* The file in the From file path exists */
00184     ParsePath(szFrom, szBuf, MAX_BUF, PP_FILENAME_ONLY);
00185     strcpy(szToTemp, szTo);
00186     AppendBackSlash(szToTemp, sizeof(szToTemp));
00187     strcat(szToTemp, szBuf);
00188     if (bFailIfExists) {
00189       DosCopy(szFrom, szToTemp, 0);
00190     } else {
00191       DosCopy(szFrom, szToTemp, DCPY_EXISTING);
00192     }
00193 
00194     return(FO_SUCCESS);
00195   }
00196 
00197   /* The file in the From file path does not exist.  Assume to contain wild args and */
00198   /* proceed acordingly.                                                             */
00199   ParsePath(szFrom, szFromDir, MAX_BUF, PP_PATH_ONLY);
00200 
00201   ulFindCount = 1;
00202   hFile = HDIR_CREATE;
00203   ulAttrs = FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY | FILE_ARCHIVED;
00204   if((DosFindFirst(szFrom, &hFile, ulAttrs, &fdFile, sizeof(fdFile), &ulFindCount, FIL_STANDARD)) != NO_ERROR)
00205     bFound = FALSE;
00206   else
00207     bFound = TRUE;
00208 
00209   while(bFound)
00210   {
00211     if((stricmp(fdFile.achName, ".") != 0) && (stricmp(fdFile.achName, "..") != 0))
00212     {
00213       /* create full path string including filename for source */
00214       strcpy(szFromTemp, szFromDir);
00215       AppendBackSlash(szFromTemp, sizeof(szFromTemp));
00216       strcat(szFromTemp, fdFile.achName);
00217 
00218       /* create full path string including filename for destination */
00219       strcpy(szToTemp, szTo);
00220       AppendBackSlash(szToTemp, sizeof(szToTemp));
00221       strcat(szToTemp, fdFile.achName);
00222 
00223       if (bFailIfExists) {
00224         DosCopy(szFromTemp, szToTemp, 0);
00225       } else {
00226         DosCopy(szFromTemp, szToTemp, DCPY_EXISTING);
00227       }
00228     }
00229 
00230     ulFindCount = 1;
00231     if (DosFindNext(hFile, &fdFile, sizeof(fdFile), &ulFindCount) == NO_ERROR) {
00232       bFound = TRUE;
00233     } else {
00234       bFound = FALSE;
00235     }
00236   }
00237 
00238   DosFindClose(hFile);
00239   return(FO_SUCCESS);
00240 }
00241 
00242 HRESULT CreateDirectoriesAll(char* szPath)
00243 {
00244   int     i;
00245   int     iLen = strlen(szPath);
00246   char    szCreatePath[MAX_BUF];
00247   HRESULT hrResult = 0;
00248 
00249   memset(szCreatePath, 0, MAX_BUF);
00250   memcpy(szCreatePath, szPath, iLen);
00251   for(i = 0; i < iLen; i++)
00252   {
00253     if((iLen > 1) &&
00254       ((i != 0) && ((szPath[i] == '\\') || (szPath[i] == '/'))) &&
00255       (!((szPath[0] == '\\') && (i == 1)) && !((szPath[1] == ':') && (i == 2))))
00256     {
00257       szCreatePath[i] = '\0';
00258       if(FileExists(szCreatePath) == FALSE)
00259       {
00260         APIRET rc = DosCreateDir(szCreatePath, NULL);  
00261         if (rc == NO_ERROR) {
00262           hrResult = 1;
00263         }
00264       }
00265       szCreatePath[i] = szPath[i];
00266     }
00267   }
00268   return(hrResult);
00269 }
00270 
00271 HRESULT FileDelete(PSZ szDestination)
00272 {
00273   HDIR            hFile;
00274   FILEFINDBUF3    fdFile;
00275   ULONG           ulFindCount;
00276   ULONG           ulAttrs;
00277   char            szBuf[MAX_BUF];
00278   char            szPathOnly[MAX_BUF];
00279   BOOL            bFound;
00280 
00281   if(FileExists(szDestination))
00282   {
00283     /* The file in the From file path exists */
00284     DosDelete(szDestination);
00285     return(FO_SUCCESS);
00286   }
00287 
00288   /* The file in the From file path does not exist.  Assume to contain wild args and */
00289   /* proceed acordingly.                                                             */
00290   ParsePath(szDestination, szPathOnly, MAX_BUF, PP_PATH_ONLY);
00291 
00292   ulFindCount = 1;
00293   hFile = HDIR_CREATE;
00294   ulAttrs = FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY | FILE_ARCHIVED;
00295   if((DosFindFirst(szDestination, &hFile, ulAttrs, &fdFile, sizeof(fdFile), &ulFindCount, FIL_STANDARD)) != NO_ERROR)
00296     bFound = FALSE;
00297   else
00298     bFound = TRUE;
00299 
00300   while(bFound)
00301   {
00302     if(!(fdFile.attrFile & FILE_DIRECTORY))
00303     {
00304       strcpy(szBuf, szPathOnly);
00305       AppendBackSlash(szBuf, sizeof(szBuf));
00306       strcat(szBuf, fdFile.achName);
00307 
00308       DosDelete(szBuf);
00309     }
00310 
00311     ulFindCount = 1;
00312     if (DosFindNext(hFile, &fdFile, sizeof(fdFile), &ulFindCount) == NO_ERROR) {
00313       bFound = TRUE;
00314     } else {
00315       bFound = FALSE;
00316     }
00317   }
00318 
00319   DosFindClose(hFile);
00320   return(FO_SUCCESS);
00321 }
00322 
00323 HRESULT DirectoryRemove(PSZ szDestination, BOOL bRemoveSubdirs)
00324 {
00325   HDIR            hFile;
00326   FILEFINDBUF3    fdFile;
00327   ULONG           ulFindCount;
00328   ULONG           ulAttrs;
00329   char            szDestTemp[MAX_BUF];
00330   BOOL            bFound;
00331 
00332   if(!FileExists(szDestination))
00333     return(FO_SUCCESS);
00334 
00335   if(bRemoveSubdirs == TRUE)
00336   {
00337     strcpy(szDestTemp, szDestination);
00338     AppendBackSlash(szDestTemp, sizeof(szDestTemp));
00339     strcat(szDestTemp, "*");
00340 
00341     ulFindCount = 1;
00342     hFile = HDIR_CREATE;
00343     ulAttrs = FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY | FILE_ARCHIVED;
00344     if((DosFindFirst(szDestTemp, &hFile, ulAttrs, &fdFile, sizeof(fdFile), &ulFindCount, FIL_STANDARD)) != NO_ERROR)
00345       bFound = FALSE;
00346     else
00347       bFound = TRUE;
00348     while(bFound == TRUE)
00349     {
00350       if((stricmp(fdFile.achName, ".") != 0) && (stricmp(fdFile.achName, "..") != 0))
00351       {
00352         /* create full path */
00353         strcpy(szDestTemp, szDestination);
00354         AppendBackSlash(szDestTemp, sizeof(szDestTemp));
00355         strcat(szDestTemp, fdFile.achName);
00356 
00357         if(fdFile.attrFile & FILE_DIRECTORY)
00358         {
00359           DirectoryRemove(szDestTemp, bRemoveSubdirs);
00360         }
00361         else
00362         {
00363           DosDelete(szDestTemp);
00364         }
00365       }
00366 
00367       ulFindCount = 1;
00368       if (DosFindNext(hFile, &fdFile, sizeof(fdFile), &ulFindCount) == NO_ERROR) {
00369         bFound = TRUE;
00370       } else {
00371         bFound = FALSE;
00372       }
00373     }
00374 
00375     DosFindClose(hFile);
00376   }
00377   
00378   DosDeleteDir(szDestination);
00379   return(FO_SUCCESS);
00380 }
00381