home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Today - The Disc! 17 / cdrt17.iso / pc / fish / win32s / iniupd / iniupd.c next >
Text File  |  1994-11-11  |  10KB  |  351 lines

  1. #define MAXBUF 256
  2. #define MAXPATH 64
  3.  
  4. #include <windows.h>
  5. #include <assert.h>
  6. #include <dos.h>
  7. #include <io.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <sys\stat.h>
  11.  
  12. #include "iniupd.h"
  13.  
  14. typedef struct {
  15.     BYTE bMajor;
  16.     BYTE bMinor;
  17.     WORD wBuildNumber;
  18.     BOOL fDebug;
  19. } WIN32SINFO, far * LPWIN32SINFO;
  20.  
  21. #define FALSE 0
  22. #define TRUE 1
  23.  
  24. FILE *fpSysIni;
  25. FILE *fpSysPre;
  26. char *psz1;
  27. char rgchBuf[MAXBUF];
  28. char szTmpBuf[MAXBUF];
  29. char szSysIni[MAXBUF];
  30. char szSysOld[MAXBUF];
  31. char szTempFile[MAXBUF];
  32. char *szWinPath;
  33. char *szVxdPath;
  34.  
  35. /****************************************************************************
  36.    FUNCTION: LibMain(HANDLE, WORD, WORD, LPSTR)
  37.  
  38.    PURPOSE:  Is called by LibEntry.  LibEntry is called by Windows when
  39.          the DLL is loaded.  The LibEntry routine is provided in
  40.          the LIBENTRY.OBJ in the SDK Link Libraries disk.  (The
  41.          source LIBENTRY.ASM is also provided.)
  42.  
  43.          LibEntry initializes the DLL's heap, if a HEAPSIZE value is
  44.          specified in the DLL's DEF file.  Then LibEntry calls
  45.          LibMain.  The LibMain function below satisfies that call.
  46.     
  47.          The LibMain function should perform additional initialization
  48.          tasks required by the DLL.  In this example, no initialization
  49.          tasks are required.  LibMain should return a value of 1 if
  50.          the initialization is successful.
  51.     
  52. *******************************************************************************/
  53. int FAR PASCAL LibMain(hModule, wDataSeg, cbHeapSize, lpszCmdLine)
  54. HANDLE  hModule;
  55. WORD    wDataSeg;
  56. WORD    cbHeapSize;
  57. LPSTR   lpszCmdLine;
  58. {
  59.     return 1;
  60. }
  61.  
  62. // Determine whether .ini line contains specified bracketed section name
  63. BOOL findsection(char *szStr1, char *szStr2, int count )
  64. {
  65.     int i, j, k;
  66.     BOOL bFound;
  67.  
  68.     // Find section bracket, skip over white space
  69.     for (i=0; i<lstrlen(szStr1); i++) {
  70.         if (szStr1[i] == ';' )
  71.         return FALSE;  // Ignore comment lines
  72.         if (szStr1[i] == '[' )
  73.         break;
  74.     }
  75.     // String does not contain bracketed [] section name
  76.     if (i >= lstrlen(szStr1) )
  77.         return FALSE;
  78.  
  79.     // Determine if substring section is present on .ini line
  80.     for (j = i; j <= (lstrlen(szStr1)-count); j++)
  81.     {
  82.         for (k=0, bFound = TRUE; bFound && (k<count); k++ ) {
  83.         if (toupper(szStr1[j+k]) != toupper(szStr2[k]))
  84.             bFound = FALSE;
  85.         }
  86.         if (bFound)
  87.         return TRUE;
  88.     }
  89.     return FALSE;
  90. }
  91.  
  92. // Determine whether .ini line contains reference to W32S.386 VxD
  93. BOOL findsubstring(char *szStr1, char *szStr2, int count )
  94. {
  95.     int i, j, k;
  96.     BOOL bFound;
  97.  
  98.     // Make sure not a comment line.  If '=' found, search driver reference
  99.     for (i=0; i<lstrlen(szStr1); i++) {
  100.         if (szStr1[i] == ';' )
  101.         return FALSE;  // Ignore comment lines
  102.         if (szStr1[i] == '=' )
  103.         break;
  104.     }
  105.     // String does not contain bracketed 'device=' or 'drivers='
  106.     if (i >= lstrlen(szStr1) )
  107.         return FALSE;
  108.  
  109.     // Determine if substring is present on .ini line
  110.     for (j = 0; j <= (lstrlen(szStr1)-count); j++)
  111.     {
  112.         for (k=0, bFound = TRUE; bFound && (k<count); k++ ) {
  113.         if (toupper(szStr1[j+k]) != toupper(szStr2[k]))
  114.             bFound = FALSE;
  115.         }
  116.         if (bFound)
  117.         return TRUE;
  118.     }
  119.     return FALSE;
  120. }
  121.  
  122. /****************************************************************************
  123.  
  124.    FUNCTION: VOID strcpyf2n(char FAR *in, char *out )
  125.  
  126.    PURPOSE:  strcpyf2n copyies the contents of the far string in to the
  127.          near string out. Memory must be allocated for out for the
  128.          full string length including the terminatined '\0'
  129.     
  130. *******************************************************************************/
  131.  
  132. VOID strcpyf2n (char FAR *in, char *out)
  133. {
  134.     while (*out++ = *in++);
  135. }
  136.  
  137. /****************************************************************************
  138.     FUNCTION:  WEP(int)
  139.  
  140.     PURPOSE:  Performs cleanup tasks when the DLL is unloaded.  WEP() is
  141.           called automatically by Windows when the DLL is unloaded (no
  142.           remaining tasks still have the DLL loaded).  It is strongly
  143.           recommended that a DLL have a WEP() function, even if it does
  144.           nothing but returns success (1), as in this example.
  145.  
  146. *******************************************************************************/
  147. int FAR PASCAL WEP (bSystemExit)
  148. int bSystemExit;
  149. {
  150.     return(1);
  151. }
  152.  
  153.  
  154.  
  155. /****************************************************************************
  156. ****************************************************************************/
  157.  
  158. BOOL FAR PASCAL MakeSystemIni(LPSTR lpszWinPath, LPSTR lpszVxdPath)
  159. {
  160. unsigned char fDevAdded = FALSE;
  161.  
  162.     szWinPath = (char *) malloc (lstrlen(lpszWinPath)+1);
  163.     szVxdPath = (char *) malloc (lstrlen(lpszVxdPath)+1);
  164.  
  165.     strcpyf2n(lpszWinPath, szWinPath);
  166.     strcpyf2n(lpszVxdPath, szVxdPath);
  167.  
  168.     szSysIni[0] = '\0';
  169.     lstrcat(lstrcpy(szSysIni, szWinPath), "SYSTEM.INI");
  170.  
  171.     szTempFile[0] = '\0';
  172.     lstrcat(lstrcpy(szTempFile, szWinPath), "$win32s$.tmp" );
  173.  
  174.     if ((fpSysIni = fopen(szSysIni, "r")) == NULL)
  175.     {
  176.     wsprintf(szTmpBuf, "%s cannot be opened for read from %s.  Setup is unable to make modifications.",
  177.       (LPSTR)szSysIni, (LPSTR)szWinPath);
  178.     MessageBox((HWND)NULL, szTmpBuf, "Setup Message", MB_OK | MB_ICONEXCLAMATION);
  179.     return(FALSE);
  180.     }
  181.  
  182.     lstrcat(lstrcpy(szSysOld, szWinPath), "SYSTEM.OLD");
  183.     if ((fpSysPre = fopen(szSysOld, "wt")) == NULL)
  184.     {
  185.     wsprintf(szTmpBuf, "%s cannot be opened for write.  Setup is unable to make modifications.",
  186.       (LPSTR)szSysOld);
  187.     MessageBox((HWND)NULL, szTmpBuf, "Setup Message", MB_OK | MB_ICONEXCLAMATION);
  188.     fclose(fpSysIni);
  189.     return(FALSE);
  190.     }
  191.  
  192.     while (fgets(rgchBuf, MAXBUF, fpSysIni))
  193.     {
  194.     fputs(rgchBuf, fpSysPre);
  195.     if (findsection(rgchBuf, "[386Enh]", 8)==TRUE)
  196.     {
  197.         // only add to the [386Enh] section.
  198.         if (fDevAdded == FALSE)
  199.         {
  200.         fputs("device=", fpSysPre);
  201.         fputs(szVxdPath, fpSysPre);
  202.         fputc('\n', fpSysPre);
  203.         fDevAdded = TRUE;
  204.         }
  205.         do
  206.         {
  207.         if (!(psz1 = fgets(rgchBuf, MAXBUF, fpSysIni)))
  208.             break;
  209.                 // If reinstall, remove duplicate device=<path>\VxD lines
  210.         if (!findsubstring(rgchBuf, "\\win32s\\w32s.386", 16))
  211.             fputs(rgchBuf, fpSysPre);
  212.         } while (*psz1 != '[');
  213.     }
  214.  
  215.         // Add device=winmm16.dll to [Boot] section
  216.         if (findsection(rgchBuf, "[Boot]", 6)==TRUE)
  217.     {
  218.         do
  219.         {
  220.         if (!(psz1 = fgets(rgchBuf, MAXBUF, fpSysIni)))
  221.             break;
  222.         // If reinstall, do not duplicate drivers=winmm16.dll info
  223.         if (findsubstring(rgchBuf, "drivers=", 8))
  224.             if (!findsubstring(rgchBuf, "winmm16.dll", 11)) {
  225.             rgchBuf[lstrlen(rgchBuf)-1] = '\0'; // Remove CR
  226.             lstrcat( rgchBuf, " winmm16.dll\n" );
  227.             }
  228.         fputs(rgchBuf, fpSysPre);
  229.         } while (*psz1 != '[');
  230.     }
  231.     }
  232.  
  233.     fclose(fpSysIni);
  234.     fclose(fpSysPre);
  235.  
  236.     // Rename o' rama
  237.     rename( szSysIni, szTempFile );
  238.     rename( szSysOld, szSysIni );
  239.     rename( szTempFile, szSysOld );
  240.  
  241.     return(TRUE);
  242. }
  243. /*
  244. */
  245.  
  246. BOOL FAR PASCAL PagingEnabled( void )
  247. {
  248.     // Win32s requires Windows to be paging enabled.
  249.     // This is enabled by default during standard 386 installation,
  250.     // but can be configured via the Control Panel 386 Enhanced icon.
  251.     if ( GetWinFlags() & WF_PAGING )
  252.     return TRUE;
  253.     else
  254.     return FALSE;
  255. }
  256. /*
  257. */
  258.  
  259. BOOL FAR PASCAL ShareEnabled( void )
  260. {
  261.     // Win32s requires file sharing and locking support
  262.     // This is enabled by default in WfW, but must be specified via SHARE.EXE
  263.     // on MS-DOS.  This routine determines wether file locking is enabled.
  264.     BOOL bShare = FALSE;
  265.     HFILE hf1, hf2;
  266.  
  267.     GetWindowsDirectory( rgchBuf, MAXBUF );
  268.     if (rgchBuf[lstrlen( rgchBuf ) - 1] != '\\')
  269.     lstrcat( rgchBuf, "\\" );
  270.     lstrcat( rgchBuf, "SYSTEM.INI" );
  271.  
  272.     if (HFILE_ERROR == (hf1 = _lopen( rgchBuf, READ | OF_SHARE_EXCLUSIVE) )) {
  273.     bShare = TRUE; // Error path: System.ini already locked (share assumed not loaded)
  274.     } else if (HFILE_ERROR == (hf2 = _lopen( rgchBuf, READ | OF_SHARE_EXCLUSIVE) ))
  275.     bShare = TRUE;
  276.  
  277.     if (hf1 != HFILE_ERROR)
  278.     _lclose( hf1 );
  279.  
  280.     if (hf2 != HFILE_ERROR)
  281.     _lclose( hf2 );
  282.  
  283.     return bShare;
  284. }
  285. /*
  286. */
  287.  
  288. // Indicates whether Win32s is installed and version number
  289. // if Win32s is loaded and VxD is functional.
  290. BOOL FAR PASCAL IsWin32sLoaded( LPSTR szVersion ) {
  291.  
  292. BOOL            fWin32sLoaded = FALSE;
  293. FARPROC            pfnInfo;
  294. HANDLE            hWin32sys;
  295. WIN32SINFO        Info;
  296.  
  297. hWin32sys = LoadLibrary( "W32SYS.DLL" );
  298. if(hWin32sys > HINSTANCE_ERROR) {
  299.     pfnInfo = GetProcAddress(hWin32sys, "GETWIN32SINFO");
  300.     if(pfnInfo) {
  301.     // Win32s version 1.1 is installed
  302.     if(!(*pfnInfo)((LPWIN32SINFO) &Info)) {
  303.         fWin32sLoaded = TRUE;
  304.         wsprintf( szVersion, "%d.%d.%d.0",
  305.                   Info.bMajor, Info.bMinor, Info.wBuildNumber );
  306.     } else
  307.         fWin32sLoaded = FALSE; //Win32s VxD not loaded.
  308.     } else {
  309.     // Win32s version 1.0 is installed.
  310.     fWin32sLoaded = TRUE;
  311.     lstrcpy( szVersion, "1.0.0.0" );
  312.     }
  313.     FreeLibrary( hWin32sys );
  314. } else // Win32s not installed.
  315.     fWin32sLoaded = FALSE;
  316.  
  317. return fWin32sLoaded;
  318. }
  319. /*
  320. */
  321.  
  322. // This function tests to see if Win16 application is running on Windows
  323. // NT. The Win32s Setup program uses this information to assure that
  324. // Win32s DLLs and VxD are not installed on Windows NT.
  325. BOOL FAR PASCAL OnWindowsNT( VOID )
  326. {
  327.     // Returns TRUE if on Windows NT, otherwise FALSE
  328.     return (BOOL)(GetWinFlags() & 0x00004000);
  329. }
  330.  
  331. // This function tests to see that there is no active Win32s application
  332. // currently in the system. Various problems may occur otherwise.
  333. BOOL FAR PASCAL IsRunningApp(void)
  334. {
  335.     extern WORD FAR PASCAL Win32sGetRunningApp(LPSTR, WORD, LPWORD);
  336.  
  337.     return(Win32sGetRunningApp(NULL, 0, NULL) != 0);
  338. }
  339.  
  340. char szLocalFileName[240];
  341.  
  342. int FAR PASCAL SetFileAttrib(LPSTR lpszFileName, int attrib)
  343. {
  344.  
  345.     lstrcpy(szLocalFileName, lpszFileName);
  346.     return _chmod(szLocalFileName, attrib);
  347. }
  348.  
  349. /*
  350. */
  351.