📄 file.c
字号:
/*
* file.c Read a file into memory. For DosWin Setup. Also contains
* config.sys and autoexec.bat munging. Lots of stuff is far
* pascal because we need to call from windows code.
* MC
*
* Modification History:
*
* 3/24/89 Mike Colee Wrote it
*
*/
#include <dos.h>
#include <malloc.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "sulib.h"
#define PHILSMAXPATH 256
/* Globaly used pointers to non-translatable text strings. */
char *pszDEVICE = "device";
char *pszMOUSE = "mouse";
char *pszMSSYS = "mouse.sys";
char *pszHPSYS = "mousehp.sys";
char *pszADD_Y = "add_y";
char *pszSYSEXT = ".sys";
char *pszCOMEXT = ".com";
char *pszPATH = "PATH";
char *pszTEMP = "TEMP";
char *pszSET = "set";
/*
* Global flag indicating type of setup.
*/
BOOL bIsNetSetup = FALSE;
/* Global far pointers to buffers that will contain autoexec.bat and
config.sys contents. These can be modified by the DOS or windows
portion of setup in case the buffer needs to be expanded. */
LPSTR lpConfigBuff;
LPSTR lpAutoBuff;
BOOL gbAutoMod; /* becomes true if autoexec buffer is modified */
BOOL gbSysMod; /* becomes true if config buffer is modified */
PSTR pConfigWin = CONFIG_WIN;
PSTR pAutoWin = AUTOEXEC_WIN;
/* Another stupid global needed bucause of another hack to the config
munger. Some wierd machines will need a himem switch. This global
will either contain the switch string or will be NULL if no switch
is needed. */
extern char szHimemSwitch[];
BOOL gbBufValid = FALSE; /* Global flag used to detect first call to
fnProcessConfig from subsiquent call. */
/* Functions resolved here that are only used here !! (Local Prototypes ) */
BOOL fnEditPath(char*);
void fnModifyDeviceEntry(int, int, PSTR, PSTR);
unsigned fnWriteFiles(LPSTR, char*, char*);
BOOL fnPathline(LPSTR);
void fnTerminate(char*);
LPSTR fnCreateFileBuffer(unsigned);
char *fnMyStrPath(char*, char*);
BOOL fnCheckDevice(char*,char*,BOOL);
BOOL fnFileExists(char*);
BOOL fnMouseline(LPSTR, PSTR);
BOOL fnTempLine(LPSTR);
/* unsigned FAR PASCAL fnProcessFile(&iEga,&iHimem,&iSmartdrvMin,&iSmartdrvMAX,&iLim,&iRamdrive,Opt_Flag)
*
* iHimem, iSmartsrv, and iRamdrive are the values of their respective
* devices. iHimem will be (1) true if himem is present or is to be
* installed. iRamdrive and iSmartdrv are the sizes of the drivers to
* be installed or are the sizes of the currently installed devices.
*
* Function will operate on config.sys file according to function chosen
* with Opt_Flag. Opt_Flag options are:
*
* RETURN_PRAMS : Function returns size of smartdrive, ramdrive, and
* weather himem.sys is installed. These values are
* returned in pointer provided as func args. Return
* value indicates success or failure.
*
* SET_PRAMS : Function sets parameters for smartdrive, ramdrive
* and will add himem to config.sys. Parameters are
* set according to values given in func args. A null
* argument leaves that value un affected. Return value
* indicates success or failure. Success indicates that
* the newly modified buffer will need to be written.
*
* The SET_PRAMS option is also affected by the value
* of the given device parameter. If the value is
* zero the device entry will be un-affected. If a
* positive value is given, that will become the size
* of the installed devive. If a -1 value is given, the
* device will be removed from config.sys
*
* CHECK_COMPAT: Function will check config.sys for any of the device
* drivers listed in setup.inf as "incompatable". Any
* drivers found will be removed. This option will also
* make sure the files = spec is greater than or = to
* 30. Return value indicates true or false as to
* weather a config buffer will need to be written.
*
* WRITE_SYS : Function will rename the current config.sys to
* config.win then write out the newly modified buffer
* as config.sys. Return indicates success or failure.
*
* WRITE_BAK : Function will write out the newly modified buffer
* as config.win. Return indicates sucess or failure.
*
* WRITE_NONE : Throws out the modified buffer (read free).
*
* MUNGE_AUTO : Will add szSetupPath to the path statement in the
* users autoexec.bat file. also will set temp
* environment variable if necessary.
*
* ASSURE_OPEN : This is a NOP call used only to assure buffers
* for autoexec.bat and config.sys have been opened.
*
* ASSURE_MOUSE_POSITION: This call should be made to assure EGA.SYS will
* be installed before mouse.sys. sigh ...
*
*/
unsigned FAR PASCAL fnProcessFile(piEGA,piHimem,piSmartdrvMIN,piSmartdrvMAX,piLim,piRamdrive,Opt_Flag)
int *piEGA;
int *piHimem;
int *piSmartdrvMIN;
int *piSmartdrvMAX;
int *piLim;
int *piRamdrive;
unsigned Opt_Flag;
{
char szConfigPath[MAXPATHLEN]; /* String buffer to hold fully qualified
path to config.sys file. */
char szAutoexecPath[MAXPATHLEN]; /* String buffer to hold fully qualified
path to autoexec.bat file. */
unsigned AutoExecWork = 0; /* bit field to determine work needed */
unsigned fRetVal = 0;
unsigned fRetVal2 = 0;
int fhDst;
char szTmpPath[15];
char szConfigLine[MAXCMDLINELEN];
char szLookPath[256];
int n = NO_ACTION; /* do nothing to these entries */
BOOL bTmpRetVal = TRUE;
if (!piHimem) piHimem = &n; /* do nothing to these entries */
if (!piSmartdrvMIN) piSmartdrvMIN = &n; /* do nothing to these entries */
if (!piSmartdrvMAX) piSmartdrvMAX = &n; /* do nothing to these entries */
if (!piLim) piLim = &n; /* do nothing to these entries */
if (!piRamdrive) piRamdrive = &n; /* do nothing to these entries */
if (!piEGA) piEGA = &n; /* do nothing to these entries */
if (!gbBufValid) // If first call or buffer previously written
{
#ifndef DOSONLY
extern BOOL bIsUpgrade;
extern char *szTmpConfigPath, *szTmpAutoPath;
if ( !bIsUpgrade )
{
fnGetFilePath(szConfigPath,CONFIG_SYS);
fnGetFilePath(szAutoexecPath,AUTOEXEC_BAT);
}
else
{
strcpy( szConfigPath, szTmpConfigPath );
strcpy( szAutoexecPath, szTmpAutoPath );
}
#else
fnGetFilePath(szConfigPath,CONFIG_SYS);
fnGetFilePath(szAutoexecPath,AUTOEXEC_BAT);
#endif
if (! (lpConfigBuff = fnLoadFile(szConfigPath)) ) {
lpConfigBuff = fnCreateFileBuffer(CONFIG); // Open failure so create
if ( lpConfigBuff )
gbSysMod = TRUE;
else
return FALSE;
}
else
gbSysMod = FALSE; // Config.sys buffer not modified.
if (! (lpAutoBuff = fnLoadFile(szAutoexecPath)) ) {
lpAutoBuff = fnCreateFileBuffer(AUTOEXEC); // Open failure so create
if ( lpAutoBuff )
gbAutoMod = TRUE;
else
return FALSE;
}
else
gbAutoMod = FALSE; // Autoexec.bat buffer not modified.
gbBufValid = TRUE; // Ok, files loaded !
}
#ifndef DOSONLY
/* Ok, we have to check for any incompatable devices in config.sys and
also make sure number of file handles is >= 30. */
if ( Opt_Flag & CHECK_COMPAT ) {
int i = 0;
PINF pinfSectP;
BOOL bNR; // No Replace flag for files = line.
if (bNR = fnUpdateDevice(FILES,RETURN_PRESENCE,szConfigLine)) {
while ( !ISDIGIT(szConfigLine[i]) && !ISEOF(szConfigLine[i]) )
++i;
if ( atoi(szConfigLine + i) < 30 ) // File less than 30 ?
bNR = FALSE;
}
if ( !bNR ) {
fnUpdateDevice(FILES,REMOVE_DEVICE,szConfigLine);
strcpy(szTmpPath,FILES); // Yes, build files = 30 line.
strcat(szTmpPath,NUM_FILES);
if (bTmpRetVal &= fnUpdateDevice(szTmpPath,ADD_DEVICE,szConfigLine))
gbSysMod = TRUE;
}
pinfSectP = infFindSection(NULL,COMPATIBILITY);
while ( pinfSectP != NULL ) {
char buf[40];
fartonear(buf, pinfSectP);
if (fnUpdateDevice(buf,RETURN_PRESENCE,szConfigLine)) {
if (bTmpRetVal &= fnUpdateDevice(buf,REMOUT_DEVICE,szConfigLine))
gbSysMod = TRUE;
}
pinfSectP = infNextLine(pinfSectP);
}
if ( bTmpRetVal )
fRetVal |= CHECK_COMPAT_SUCCESS;
}
#endif // DOSONLY
if ( Opt_Flag & RETURN_PRAMS ) {
if (fnUpdateDevice(HIMEM,RETURN_PRESENCE,szConfigLine))
*piHimem = TRUE;
else
*piHimem = FALSE;
if (fnUpdateDevice(EGASYS,RETURN_PRESENCE,szConfigLine))
*piEGA = TRUE;
else
*piEGA = FALSE;
if (fnCheckDevice(CACHE_SECT,szConfigLine,NO_REMOVE)) {
*piSmartdrvMAX = fnGetInstalledSize(szConfigLine,1);
*piSmartdrvMIN = fnGetInstalledSize(szConfigLine,2);
}
else {
*piSmartdrvMAX = NO_ACTION; // no Smartdrive installed.
*piSmartdrvMIN = NO_ACTION; // no Smartdrive installed.
}
if (fnCheckDevice(VDISK_SECT,szConfigLine,NO_REMOVE))
*piRamdrive = fnGetInstalledSize(szConfigLine,1);
else
*piRamdrive = NO_ACTION; // no Ramdrive installed.
if (fnCheckDevice(LIM_SECT,szConfigLine,NO_REMOVE))
*piLim = fnGetInstalledSize(szConfigLine,1);
else
*piLim = NO_ACTION; // no Limulator installed.
fRetVal |= RET_PRAMS_SUCCESS;
}
if ( Opt_Flag & SET_PRAMS ) {
fnModifyDeviceEntry(*piSmartdrvMAX,*piSmartdrvMIN,SMARTDRV,CACHE_SECT);
fnModifyDeviceEntry(*piRamdrive,0,RAMDRIVE,VDISK_SECT);
fnModifyDeviceEntry(*piLim,0,LIMDRIVER,LIM_SECT);
fnModifyDeviceEntry(*piEGA,0,EGASYS,NULL);
fnModifyDeviceEntry(*piHimem,0,HIMEM,NULL);
fRetVal |= SET_PRAMS_SUCCESS;
}
/* Make sure at all costs that ega.sys is installed before mouse.sys */
if ( Opt_Flag & ASSURE_MOUSE_POSITION ) {
if ( fnUpdateDevice(MOUSE_SYS,RETURN_PRESENCE,szConfigLine) ) {
fnUpdateDevice(MOUSE_SYS,REMOVE_DEVICE,NULL);
fnUpdateDevice(szConfigLine,ADD_DEVICE,NULL);
gbSysMod = TRUE;
}
}
/* What we want to do here is rename original config.sys to config.bak
and write out modified buffer as config.sys */
if ( Opt_Flag & WRITE_SYS )
{
#ifndef DOSONLY
{
extern int bIsUpgrade;
if ( bIsUpgrade ) /* If Upgrade need to write */
{ /* to root as a unique name */
extern char *szAutoBat;
extern char *szConfSys;
extern char szWinAuto[];
extern char szWinConf[];
extern int far CreateUniqueName( char *szBuf, char *szFile );
static int SaveFile( PSTR szFile, LPSTR lpBuf );
CreateUniqueName( szWinAuto, szAutoBat );
fRetVal |= SaveFile( szWinAuto, lpAutoBuff );
CreateUniqueName( szWinConf, szConfSys );
fRetVal2 |= SaveFile( szWinConf, lpConfigBuff );
}
else
{
if ( gbSysMod )
fRetVal2 |= fnWriteFiles(lpConfigBuff,CONFIG_BAK,CONFIG_SYS);
if ( gbAutoMod )
fRetVal |= fnWriteFiles(lpAutoBuff,AUTOEXEC_BAK,AUTOEXEC_BAT);
}
}
#else
{
if ( gbSysMod )
fRetVal2 |= fnWriteFiles(lpConfigBuff,CONFIG_BAK,CONFIG_SYS);
if ( gbAutoMod )
fRetVal |= fnWriteFiles(lpAutoBuff,AUTOEXEC_BAK,AUTOEXEC_BAT);
}
#endif
if (( fRetVal & WRITE_SUCCESS) && (fRetVal2 & WRITE_SUCCESS) )
Opt_Flag |= WRITE_NONE;
else
{
if (!(fRetVal2 & WRITE_SUCCESS))
fRetVal = fRetVal2;
}
}
/* What we do here is write out the modified buffer as config.win */
if ( Opt_Flag & WRITE_BAK ) {
if ( gbSysMod )
fRetVal2 |= fnWriteFiles(lpConfigBuff,NULL,pConfigWin);
if ( gbAutoMod )
fRetVal |= fnWriteFiles(lpAutoBuff,NULL,pAutoWin);
if (( fRetVal & WRITE_SUCCESS) && (fRetVal2 & WRITE_SUCCESS) )
Opt_Flag |= WRITE_NONE;
else {
if (!(fRetVal2 & WRITE_SUCCESS))
fRetVal = fRetVal2;
}
}
/* All we need to do here is throw out the buffer. */
if ( Opt_Flag & WRITE_NONE ) {
FFREE(lpConfigBuff); // Free the config buffer
FFREE(lpAutoBuff); // Free the autoexec buffer
gbSysMod = gbAutoMod = gbBufValid = FALSE; // Invalidate buffer
fRetVal |= WRITE_SUCCESS;
}
/* The autoexec munging code is only used in the windows part of setup. */
#ifndef DOSONLY
/* Ok, let's add the installation path to autoexec.bat */
if ( Opt_Flag & MUNGE_AUTO ) {
if (! getenv(pszTEMP) ) // If no temp environment var, argg !...
AutoExecWork |= DO_TEMP;
if (! fnMyStrPath(getenv(pszPATH),szSetupPath) )
AutoExecWork |= DO_PATH;
if ( bIsNetSetup ) {
if (! fnMyStrPath(getenv(pszPATH),szDiskPath) )
AutoExecWork |= DO_PATH;
}
if ( fnModifyPath(AutoExecWork,NULL)) // Now munge away !!
gbAutoMod = TRUE;
}
#endif // DOSONLY
if (gbSysMod)
fRetVal |= CONFIG_DIRTY;
if (gbAutoMod)
fRetVal |= AUTOEXEC_DIRTY;
return fRetVal;
}
/* BOOL fnCheckDevice(char *Device_Sect,char *szConfigLine,BOOL bRemoveFlag);
*
* Function will check users config.sys file for all [type] device
* drivers listed in setup.inf under the [type] section. If one of
* these drivers is found the line from config.sys will be returned in
* the buffer provided. The device line in question may also be romoved
* from the config.sys file.
*
* ENTRY: Device_Sect - This argument specifies the device type to be
* searched for. One of: CACHE, LIM, RAMDRIVE.
*
* szConfigLine - Buffer of sufficent size to hold the line from
* the config.sys file.
*
* bRemoveFlag - Booleen flag set to TRUE if device in question
* should be removed from config.sys file.
*
* EXIT: Boolean as to success or failure of function. TRUE if device found.
* FALSE if device not found.
*
*/
BOOL fnCheckDevice(Device_Sect,szConfigLine,bRemoveFlag)
char *Device_Sect;
char *szConfigLine;
BOOL bRemoveFlag;
{
char szNearBuf[MAX_INF_LINE_LEN];
PINF pinfSectP;
BOOL RetVal = FALSE;
pinfSectP = infFindSection(NULL,Device_Sect);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -