home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / slfinsta.zip / uninstal.c < prev    next >
C/C++ Source or Header  |  2000-03-26  |  11KB  |  333 lines

  1. /* $Id: uninstal.c,v 1.1 2000/03/27 04:53:00 ktk Exp $ */
  2.  
  3. /****************************************************************************
  4. *
  5. *                         SciTech Display Doctor
  6. *
  7. *               Copyright (C) 1991-1998 SciTech Software, Inc.
  8. *                            All rights reserved.             
  9. *
  10. *  ======================================================================
  11. *  |REMOVAL OR MODIFICATION OF THIS HEADER IS STRICTLY PROHIBITED BY LAW|
  12. *  |                                                                    |
  13. *  |This copyrighted computer code is a proprietary trade secret of     |
  14. *  |SciTech Software, Inc., located at 505 Wall Street, Chico, CA 95928 |
  15. *  |USA (www.scitechsoft.com).  ANY UNAUTHORIZED POSSESSION, USE,       |
  16. *  |VIEWING, COPYING, MODIFICATION OR DISSEMINATION OF THIS CODE IS     |
  17. *  |STRICTLY PROHIBITED BY LAW.  Unless you have current, express       |
  18. *  |written authorization from SciTech to possess or use this code, you |
  19. *  |may be subject to civil and/or criminal penalties.                  |
  20. *  |                                                                    |
  21. *  |If you received this code in error or you would like to report      |
  22. *  |improper use, please immediately contact SciTech Software, Inc. at  |
  23. *  |530-894-8400.                                                       |
  24. *  |                                                                    |
  25. *  |REMOVAL OR MODIFICATION OF THIS HEADER IS STRICTLY PROHIBITED BY LAW|
  26. *  ======================================================================
  27. *
  28. * Language:        ANSI C
  29. * Environment:    OS/2 Warp
  30. *
  31. * Description:    Program to uninstall SciTech Display Doctor components.
  32. *
  33. ****************************************************************************/
  34.  
  35. #define    INCL_WIN
  36. #define    INCL_WINWORKPLACE
  37. #include <os2.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include "pmapi.h"
  42.  
  43. /*--------------------------- Global variables ----------------------------*/
  44.  
  45. const char *szUninstallTitle    = "Uninstall SDD/2?";
  46. const char *szUninstall            = "Are you sure you wish to remove SciTech Display Doctor, and all it's components from your system?";
  47. const char *szRebootTitle        = "Please reboot!";
  48. const char *szReboot            = "In order to complete the removal of SciTech Display Doctor, you must reboot your system.";
  49. const char *szSDDHELP            = "sddhelp.sys";
  50. const char *szNoDDC                = "SET VCFG_NO_DDC=TRUE";
  51. const char *szGREXT                = "SET GREEXT=SDDGREXT";
  52. char szUNINSTALL[]                = "X:\\sdd\\uninstal.exe";
  53. char szSDDDir[]                    = "X:\\sdd";
  54. char szSDDHELP_SYS[]            = "X:\\os2\\sddhelp.sys";
  55. char szSVGADATA_PMI[]            = "X:\\os2\\svgadata.pmi";
  56. char szPRIVATE_DIF[]            = "X:\\os2\\private.dif";
  57. char szVIDEO_CFG[]                = "X:\\os2\\video.cfg";
  58. char szSDDGRADD_DLL[]            = "X:\\os2\\dll\\sddgradd.dll";
  59. char szSDDGREXT_DLL[]            = "X:\\os2\\dll\\sddgrext.dll";
  60. char szSDDPMI_DLL[]                = "X:\\os2\\dll\\sddpmi.dll";
  61. char szSDDCFG_DLL[]                = "X:\\os2\\dll\\sddcfg.dll";
  62. char szSDDCFGI_DLL[]            = "X:\\os2\\dll\\sddcfgi.dll";
  63. char szConfigSys[]                = "X:\\config.sys";
  64. char szSETVGA[]                    = "X:\\os2\\setvga.cmd";
  65.  
  66. /*-------------------------- Implementation -------------------------------*/
  67.  
  68. /* Undocumented functions */
  69. APIRET APIENTRY DosReplaceModule(PSZ pszOldModule,PSZ pszNewModule,PSZ pszBackupModule);
  70.  
  71. /****************************************************************************
  72. PARAMETERS:
  73. str    - String to set boot drive for
  74.  
  75. RETURNS:
  76. String with the boot drive letter updated
  77.  
  78. REMARKS:
  79. Sets the boot drive letter for the string, and returns the updated string.
  80. ****************************************************************************/
  81. char *SetBootDrive(
  82.     char *str)
  83. {
  84.     str[0] = PM_getBootDrive();
  85.     return str;
  86. }
  87.  
  88. /****************************************************************************
  89. PARAMETERS:
  90. buf    - String to search
  91. str    - String to search for
  92.  
  93. RETURNS:
  94. true if buf contains str, false if not.
  95.  
  96. REMARKS:
  97. Scans the string 'buf' looking for the string 'str'.
  98. ****************************************************************************/
  99. static ibool ContainsString(
  100.     const char *buf,
  101.     const char *str)
  102. {
  103.     int         len = strlen(str);
  104.     const char    *p = buf;
  105.  
  106.     while (*p) {
  107.         if (strnicmp(p, str, len) == 0)
  108.             return true;
  109.         p++;
  110.         }
  111.     return false;
  112. }
  113.  
  114. /****************************************************************************
  115. PARAMETERS:
  116. sourceName    - Name for source file
  117. destName    - Name for destination file
  118.  
  119. RETURNS:
  120. true on success, false on failure.
  121.  
  122. REMARKS:
  123. Copies the source file to the destination file.
  124. ****************************************************************************/
  125. static int CopyBinaryFile(
  126.     char *sourceName,
  127.     char *destName)
  128. {
  129.     char    *data;
  130.     FILE    *in,*out;
  131.     size_t    size,blocksize,sizeleft,i,numblocks;
  132.  
  133.     /* Open the source and destination files */
  134.     if ((in = fopen(sourceName, "rb")) == NULL)
  135.         return false;
  136.     if ((out = fopen(destName, "wb")) == NULL)
  137.         return false;
  138.     if ((data = (char*)malloc(blocksize = 0x7FFFU)) == NULL)
  139.         return false;
  140.  
  141.     /* Get the size of the source file */
  142.     fseek(in, 0, SEEK_END);
  143.     size = ftell(in);
  144.     fseek(in, 0, SEEK_SET);
  145.  
  146.     /* Now copy the contents of the file 32k at a time */
  147.     if (size > 0) {
  148.         if (size < blocksize)
  149.             blocksize = size;
  150.         numblocks = size / blocksize;
  151.         sizeleft = size;
  152.         for (i = 0; i < numblocks; i++) {
  153.             if (fread(data, 1, blocksize, in) != blocksize)
  154.                 return false;
  155.             if (fwrite(data, 1, blocksize, out) != blocksize)
  156.                 return false;
  157.             sizeleft -= blocksize;
  158.             }
  159.         if (sizeleft > 0) {
  160.             if (fread(data, 1, sizeleft, in) != sizeleft)
  161.                 return false;
  162.             if (fwrite(data, 1, sizeleft, out) != sizeleft)
  163.                 return false;
  164.             }
  165.         }
  166.  
  167.     free(data);
  168.     fclose(in);
  169.     fclose(out);
  170.     return true;
  171. }
  172.  
  173. /****************************************************************************
  174. PARAMETERS:
  175. filename    - Filename of config file to update
  176. searchKey    - Key string to search for in line
  177.  
  178. RETURNS:
  179. true on success, false on failure.
  180.  
  181. REMARKS:
  182. Searches for the specified search key in the configuration file and deletes
  183. all lines that contains this key.
  184. ****************************************************************************/
  185. static ibool DeleteLine(
  186.     char *filename,
  187.     char *searchKey)
  188. {
  189.     char    buf[255],tempName[_MAX_PATH];
  190.     FILE    *oldfile,*newfile;
  191.  
  192.     oldfile = fopen(filename, "r");
  193.     tmpnam(tempName);
  194.     newfile = fopen(tempName, "w");
  195.     if (newfile == NULL || oldfile == NULL)
  196.         return false;
  197.  
  198.     // Find the starting line in the input file given search keys
  199.     while (fgets(buf, 255, oldfile)) {
  200.         if (ContainsString(buf, searchKey))
  201.             continue;
  202.         fputs(buf, newfile);
  203.         }
  204.     fclose(newfile);
  205.     fclose(oldfile);
  206.     if (!CopyBinaryFile(tempName, filename))
  207.         return false;
  208.     DosDelete(tempName);
  209.     return true;
  210. }
  211.  
  212. #define    CLEAR_MASK    (PM_FILE_READONLY | PM_FILE_HIDDEN | PM_FILE_SYSTEM)
  213.  
  214. /****************************************************************************
  215. PARAMETERS:
  216. path         - path of directory to delete
  217. fileMask    - Mask for files to remove
  218.  
  219. REMARKS:
  220. Deletes all files and subdirectories in a directory, by recursively removing
  221. all sub-directories.
  222. ****************************************************************************/
  223. static ibool removeFiles(
  224.     const char *path,
  225.     const char *fileMask)
  226. {
  227.     PM_findData    findData;
  228.     void        *hfile;
  229.     int         done;
  230.     char         files[PM_MAX_PATH];
  231.     char         name[PM_MAX_PATH];
  232.  
  233.     findData.dwSize = sizeof(findData);
  234.     strcpy(files,path);
  235.     if (strlen(path) > 0)
  236.         PM_backslash(files);
  237.     if (strcmp(fileMask,"*") == 0)
  238.         strcat(files,"*.*");
  239.     else
  240.         strcat(files,fileMask);
  241.     done = (hfile = PM_findFirstFile(files,&findData)) == PM_FILE_INVALID;
  242.     while (!done) {
  243.         if ((strcmp(findData.name,".") != 0) && (strcmp(findData.name,"..") != 0)) {
  244.             strcpy(name,path);
  245.             if (strlen(path) > 0)
  246.                 PM_backslash(name);
  247.             strcat(name,findData.name);
  248.             if (findData.attrib & PM_FILE_DIRECTORY) {
  249.                 /* This is a directory, so recursively delete it also */
  250.                 PM_findClose(hfile);
  251.                 if (stricmp(findData.name,fileMask) == 0) {
  252.                     if (!removeFiles(name,"*"))
  253.                         return false;
  254.                     }
  255.                 else if (!removeFiles(name,fileMask))
  256.                     return false;
  257.                 if (!PM_rmdir(name))
  258.                     return false;
  259.                 done = (hfile = PM_findFirstFile(files,&findData)) == PM_FILE_INVALID;
  260.                 continue;
  261.                 }
  262.             if (findData.attrib & CLEAR_MASK)
  263.                 PM_setFileAttr(name,PM_FILE_NORMAL);
  264.             remove(name);
  265.             }
  266.         done = !PM_findNextFile(hfile,&findData);
  267.         }
  268.     if (hfile != PM_FILE_INVALID)
  269.         PM_findClose(hfile);
  270.     return true;
  271. }
  272.  
  273. /****************************************************************************
  274. PARAMETERS:
  275. filename    - Name of the file to delete
  276.  
  277. REMARKS:
  278. Removed a module that is potentially in use. We use DosReplaceModule to
  279. replace the module while it is in use, and then the file is closed. After
  280. that we can simply delete the file.
  281. ****************************************************************************/
  282. void RemoveModule(
  283.     char *filename)
  284. {
  285.     DosReplaceModule(SetBootDrive(filename),NULL,NULL);
  286.     DosDelete(filename);
  287. }
  288.  
  289. int main(int argc,char *argv[])
  290. {
  291.     HAB    habAnchor;
  292.     HMQ    hmqQueue;
  293.  
  294.     habAnchor = WinInitialize(0);
  295.     hmqQueue = WinCreateMsgQueue(habAnchor,0);
  296.     if (WinMessageBox(HWND_DESKTOP,0,(PSZ)szUninstall,(PSZ)szUninstallTitle,0,MB_OKCANCEL | MB_ICONQUESTION | MB_DEFBUTTON2 | MB_MOVEABLE) != MBID_CANCEL) {
  297.         // Remove all Nucleus files
  298.         removeFiles(PM_getNucleusPath(),"*.*");
  299.  
  300.         // Remove all SDD files
  301.         DosReplaceModule(SetBootDrive(szUNINSTALL),NULL,NULL);
  302.         removeFiles(SetBootDrive(szSDDDir),"*.*");
  303.  
  304.         // Remove all SDD driver files and DLL's
  305.         DosDelete(SetBootDrive(szSDDHELP_SYS));
  306.         DosDelete(SetBootDrive(szSVGADATA_PMI));
  307.         DosDelete(SetBootDrive(szPRIVATE_DIF));
  308.         DosDelete(SetBootDrive(szVIDEO_CFG));
  309.         RemoveModule(szSDDGRADD_DLL);
  310.         RemoveModule(szSDDGREXT_DLL);
  311.         RemoveModule(szSDDPMI_DLL);
  312.         RemoveModule(szSDDCFG_DLL);
  313.         RemoveModule(szSDDCFGI_DLL);
  314.  
  315.         // Remove the Workplace Shell objects
  316.         WinDestroyObject(WinQueryObject("<SCITECH_DESKTOP>"));
  317.  
  318.         // Revert to VGA mode
  319.         system(SetBootDrive(szSETVGA));
  320.  
  321.         // Remove entries from CONFIG.SYS file
  322.         DeleteLine(SetBootDrive(szConfigSys),(PSZ)szSDDHELP);
  323.         DeleteLine(SetBootDrive(szConfigSys),(PSZ)szNoDDC);
  324.         DeleteLine(SetBootDrive(szConfigSys),(PSZ)szGREXT);
  325.  
  326.         // Reboot the system
  327.         WinMessageBox(HWND_DESKTOP,0,(PSZ)szReboot,(PSZ)szRebootTitle,0,MB_OK | MB_INFORMATION | MB_DEFBUTTON1 | MB_MOVEABLE);
  328.         }
  329.     WinDestroyMsgQueue(hmqQueue);
  330.     WinTerminate(habAnchor);
  331.     return 0;
  332. }
  333.