⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file.c

📁 [随书类]Dos6.0源代码
💻 C
📖 第 1 页 / 共 4 页
字号:

   while ( pinfSectP ) {
      fartonear(szNearBuf,pinfSectP);
      if ( fnUpdateDevice(szNearBuf,RETURN_PRESENCE,szConfigLine) ) {
         if ( bRemoveFlag )
            fnUpdateDevice(szNearBuf,REMOUT_DEVICE,szConfigLine);
         RetVal = TRUE;
         break;
      }
      pinfSectP = infNextLine(pinfSectP);
   }

   return RetVal;
}

/* void fnGetFilePath(char* sDestBuff, char* sFileName);
 *
 * Function returns fully qualified path for either autoexec.bat or
 * config.sys. Pretty simple assumption here. Get list of fixed diskes,
 * assume first in list is boot drive ???
 *
 * ENTRY: sDestBuff  String pointer to buffer large enough to contain fully
 *                   qualified path to file.
 *
 *        sFileName  Pointer to file name to be path qualified.
 *
 * EXIT:  None
 *
 */
void FAR PASCAL fnGetFilePath(sDestBuff,sFileName)
char    *sDestBuff;
char    *sFileName;
{
   unsigned        Disks[26];

   /* If the installation destination is a remote drive, create new
      autoexec.bat and config.sys files in the destination directory. */

   if ( DosIsRemote((UPCASE(szSetupPath[0]) - 'A')) == REMOTE )
      strcpy(sDestBuff,szSetupPath); 
   else {                            
      GetFixedDisks(Disks);
      sDestBuff[0] = (char)(Disks[0] + 'A');
      sDestBuff[1] = ':';
      sDestBuff[2] = '\\';
      sDestBuff[3] = '\0';
   }
   if ( sFileName )
      catpath(sDestBuff,sFileName);
}

/* unsigned fnWriteFiles(szBuff,NewNameofOrig,BuffName);
 *
 * Function will write the contents of the buffer pointed to by BuffName.
 *
 * ENTRY: 
 *
 *
 */
unsigned fnWriteFiles(szBuff,NewNameofOrig,BuffName)
LPSTR    szBuff;
char     *NewNameofOrig;
char     *BuffName;
{
   char     szTmpBuffNew[MAXPATHLEN];
   char     szTmpBuffName[MAXPATHLEN];
   int      fhDst;

   fnGetFilePath(szTmpBuffNew,BuffName); // Qualify filename for buffer dest.
   if ( NewNameofOrig ) {
      fnGetFilePath(szTmpBuffName,NewNameofOrig); // Qualify filename for buff
      DosDelete(szTmpBuffName);
      DosRename(szTmpBuffNew,szTmpBuffName);      // Ren config.sys config.bak
   }
   _dos_setfileattr(szTmpBuffNew,_A_NORMAL);      // Make sure not read only.
   if (_dos_creat(szTmpBuffNew,_A_NORMAL,&fhDst) != 0) // Create file
      return CREATE_FAIL;
   if (fnWriteFile(szBuff,fhDst))           // Write new config.sys
      return WRITE_FAIL;
   return WRITE_SUCCESS;
}

/* unsigned fnGetInstalledSize(PSTR szConfigLine, int FieldNum);
 *
 * Function will return installed size of either ramdrive or smartdrive
 * given an entire device = line from config.sys.
 *
 * NOTES   : It seems that one of the few things you can depend on in a
 *           config.sys device = entry is that there will be a fully 
 *           qualified device driver name. This means that there will be
 *           a period (.) followed by three chars. (Thank God for this).
 *
 * ENTRY   : Near pointer to line from config.sys ( ramdrive or smartdrv ).
 *         : FieldNum = numeric field following device driver name to be
 *           returned.
 *         
 * RETURNS : Size of installed device as an unsigned value. A return value
 *           of zero indicates default size in use.
 *
 */
unsigned fnGetInstalledSize(szConfigLine,FieldNum)
PSTR     szConfigLine;
int      FieldNum;
{
   unsigned     i = 0;
   unsigned     uFieldCnt = 1;

   while ( szConfigLine[i] != '.' && szConfigLine[i] )
      ++i;

   ++i;    /* point to first char after file type period. */

   /* Now look for a numeric value in the field given as the second function
      argument. */

   while (!ISDIGIT(szConfigLine[i]) && szConfigLine[i] ) // find a digit.
      ++i;

   while ( uFieldCnt != FieldNum ) {
      while (ISDIGIT(szConfigLine[i]) && szConfigLine[i])  // Find next Field
         ++i;
      while (!ISDIGIT(szConfigLine[i]) && szConfigLine[i]) // find a digit.
         ++i;
      ++uFieldCnt;
   }

   return (atoi(szConfigLine + i));   // Convert to int and return value !

}

/* void fnConstructDeviceEntry(PSTR, PSTR, int, int);
 *
 * Function will create a device = entry line that is ready to be placed
 * into the config.sys file. The device will be ramdrive, smartdrv, or
 * himem depending on the device name given. The size of the device is
 * given in iActionValue, and the newly constructed line will be returned
 * in szConfigLine.
 *
 * ENTRY   : szConfigLine, buffer large enough to hold the config line. This
 *           buffer will contain the config line if it is presently in the
 *           config.sys file. (ie, were changing an entry).
 *
 *         : szDeviceName, the name of the device for which were
 *           constructing the new config line for.
 *
 *         : iActionValue, The size of the the ramdrive or smartdrive. If
 *           zero, default size will be used ( no size entry ). Ignored
 *           if himem is the device being operated on.
 *
 * RETURNS : None.
 *
 */

void fnConstructDeviceEntry(szConfigLine,szDeviceName,iActionValue,iMin)
PSTR     szConfigLine;
PSTR     szDeviceName;
int      iActionValue;
int      iMin;
{
   char         szPath[MAXPATHLEN];
   char         szInt_to_String[8];
   char         *p;
   int          i;

#ifndef  DOSONLY
   extern BOOL  bIsUpgrade;
   void far     wsBuildConfigLine( char *szPath, char *szDeviceName );

   if ( bIsUpgrade )
      wsBuildConfigLine(szConfigLine, szDeviceName );
   else
   {
      if ( !strcmpi( szDeviceName, HIMEM ) )
         fnGetFilePath( szPath, szDeviceName );
      else
      {
         ExpandFileName("0:",szPath);      // Get path to device driver.
         catpath( szPath, szDeviceName );  // Cat with device driver name.
      }
      strcpy(szConfigLine,pszDEVICE);     // Copy in device =
      i = strlen(szConfigLine);
      szConfigLine[i++] = EQUAL;
      szConfigLine[i] = NULL;
      strcat( szConfigLine,szPath );     // Copy in the fully qualified driver
     }
#else
      if ( !strcmpi( szDeviceName, HIMEM ) )
         fnGetFilePath( szPath, szDeviceName );
      else
      {
         ExpandFileName("0:",szPath);      // Get path to device driver.
         catpath( szPath, szDeviceName );  // Cat with device driver name.
      }
      strcpy(szConfigLine,pszDEVICE);     // Copy in device =
      i = strlen(szConfigLine);
      szConfigLine[i++] = EQUAL;
      szConfigLine[i] = NULL;
      strcat( szConfigLine,szPath );     // Copy in the fully qualified driver
#endif

   if (iActionValue && (strcmpi(szDeviceName,HIMEM)) &&
      (strcmpi(szDeviceName,EGASYS))) {
      i = strlen(szConfigLine);
      szConfigLine[i++] = SPACE;
      szConfigLine[i] = NULL;
      p = itoa(iActionValue,szInt_to_String,10);
      strcat(szConfigLine,szInt_to_String);
      if ( iMin ) {
         i = strlen(szConfigLine);
         szConfigLine[i++] = SPACE;
         szConfigLine[i] = NULL;
         p = itoa(iMin,szInt_to_String,10);
         strcat(szConfigLine,szInt_to_String);
      }
   }
   if (! (strcmpi(szDeviceName,RAMDRIVE)) )
      strcat(szConfigLine,EMM_SWITCH);

   if (! (strcmpi(szDeviceName,HIMEM)) && szHimemSwitch[0] ) {
      strcat(szConfigLine," /M:");
      strcat(szConfigLine,szHimemSwitch);
   }
}

/* LPSTR fnCreateFileBuffer(unsigned fType);
 *
 * This function is called in the cases where the user does not have either
 * an autoexec.bat or config.sys file. In these cases we create a buffer on
 * the far heap and build the appropiate file and return a long pointer to
 * the buffer.
 *
 * ENTRY: fType - This is a flag value specifing which file buffer to create
 *                must be either AUTOEXEC or CONFIG.
 *
 * EXIT:  LPSTR - Returns long pointer to newly created buffer.
 *
 */
LPSTR fnCreateFileBuffer(fType)
unsigned    fType;
{

   LPSTR          lpBuf;  /* far pointer to buffer. */
   LPSTR            pTo;  /* Used to keep pointer into new buffer. */
   unsigned         len;  /* used to calculate length of buff for autoexec */
   char   TmpWork1[128];  /* temp work buffers for string construction. */
   char   TmpWork2[128];

   if ( fType == CONFIG ) {
      /* First, create files = line */
      strcpy(TmpWork1,FILES" "NUM_FILES);
      /* Now, create buffers = line */
      strcpy(TmpWork2,BUFFERS" "NUM_FILES);
   }
   else {
      TmpWork1[0] = '\0';
      strcpy(TmpWork2,pszPATH);
      strcat(TmpWork2," ");
      strcat(TmpWork2,szSetupPath);
   }

   /* Now calculate length of buffer required for new file and place newly
      created strings into new buffer. Finaly return pointer to buffer     */

   len = (strlen(TmpWork1) + strlen(TmpWork2) + 8 );
   lpBuf = FALLOC(len);                              /* Allocate buffer */
   if (!lpBuf) 
      return NULL;  /* if allocation failes, return null pointer. */
   /* Now, write newly constructed strings into buffer ! */
   pTo = lpBuf;
   pTo = fnCopyBuf(TmpWork1,pTo,REMAINDER);
   *pTo = CR, ++pTo, *pTo = LF, ++pTo;       /* attach a cr,lf pair. */
   pTo = fnCopyBuf(TmpWork2,pTo,REMAINDER);
   *pTo = CR, ++pTo, *pTo = LF, ++pTo;       /* attach a cr,lf pair. */
   *pTo = '\0';                              /* terminate new buffer.    */
   return lpBuf;                             /* return pointer to buffer */
}

/* void fnModifyDeviceEntry(int, int, PSTR, PSTR);
 *
 *  Function will operate on a single line from config.sys This function
 *  is only called by fnProcessConfig. This function is given an option
 *  value, a pointer to the config.sys buffer, and the device name to be
 *  operated on. Depending on action value the following operations are
 *  possible.
 *
 *   NO_ACTION      : No action taken.
 *
 *   REMOVE_DEVICE  : Device = line will be removed from config.sys buffer.
 *
 *   default        : Device = line will be added to config.sys buffer. If
 *                    the action value is zero, the default size for that
 *                    device will be used. Otherwise the size given will
 *                    be used.
 *
 *   RETURN: None
 *	
 */ 
void fnModifyDeviceEntry(iActionValue, iMin, szDeviceName, szDeviceSect)
int          iActionValue;
int          iMin;
PSTR         szDeviceName;
PSTR         szDeviceSect;
{

   static char        szConfigLine[MAXPATHLEN];

   szConfigLine[0] = '\0';

   switch (iActionValue) {
      case NO_ACTION:
         break;
      case REMOVE_DEVICE:
         if ( szDeviceSect )
            fnCheckDevice(szDeviceSect,szConfigLine,YES_REMOVE);
         fnUpdateDevice(szDeviceName,REMOVE_DEVICE,NULL);
         gbSysMod = TRUE;
         break;
      default:   // Install device with given numerical size or default.
         if ( szDeviceSect )
            fnCheckDevice(szDeviceSect,szConfigLine,YES_REMOVE);
         fnUpdateDevice(szDeviceName,REMOVE_DEVICE,NULL);
         fnConstructDeviceEntry(szConfigLine,szDeviceName,iActionValue,iMin);
         if ( strcmpi(HIMEM,szDeviceName) )
            fnUpdateDevice(szConfigLine,ADD_DEVICE,szConfigLine);
         else
            fnUpdateDevice(szConfigLine,ADD_DEVICE_FIRST,szConfigLine);
         gbSysMod = TRUE;
   }
}

/* LPSTR fnLoadFile(szFile);
 *
 *  Reads the entire file into a far malloc'd buffer and returns a pointer
 *  to the buffer. End of lines are indicated by cr,lf pairs. EOF, or in
 *  this case, the end of the buffer is indacated by a single NULL byte.
 *
 *  ENTRY: szFile	- File name to load, null terminated string required.
 *	
 *
 *  RETURNS: Far pointer to loaded buffer or NULL on Failure.
 *
 */
LPSTR NEAR PASCAL fnLoadFile(szFile)
PSTR  szFile;
{
   LPSTR        lpBuf = NULL;
   WORD         fh = -1;
   char         szDst[MAXPATHLEN];
   unsigned     len;

   /* First off, Open the file */
   /* first try to open passed parameter as is */

   fh = FOPEN(szFile);

   if (fh == -1)
      goto error_close;

   /* Find size of file and allocate buffer in far heap */

   len = (WORD)FSEEK(fh,0L,SEEK_END); /* find File size in Bytes */
   FSEEK(fh,0L,SEEK_SET);

   if ( len >= 65000 )     /* limit on size of files we can load. */
      goto error_close;

   lpBuf = FALLOC(len+1);  /* Allocate buffer plus one 1 for 0 terminator */
   if (!lpBuf)
      goto error_close;

   /*	Read ALL of the file into memory, Including comments, ect. */
   
   if ( FREAD(fh,lpBuf,len) != len ) {
      FFREE(lpBuf);
      lpBuf = NULL;
   }
   else
      lpBuf[len] = 0;

error_close:

   FCLOSE(fh);
   return lpBuf;

}

/* BOOL NEAR PASCAL fnUpdateDevice(szDeviceName,Opt_Flag,szConfigLine);
 *
 *  Function will add, delete or determine presence of a device = driver
 *  or files = in the users config.sys file.
 *
 *  ENTRY: szDeviceName: Name of device you intend to operate on. ie 
 *                       SMARTDRV.SYS ect. In the case of ADD_DEVICE
 *                       must point to a complete Config.sys device = entry
 *                       or files = entry.
 *
 *                       Example:  device = c:\dev\vt52.sys /c /l
 *                       Example:  files  = 30
 *
 *                       In the case of REMOVE_DEVICE of RETURN_PRESENCE
 *                       should point to either a device driver name or
 *                       If operating on the files = line, must point to
 *                       string "files"
 *
 *         Opt_Flag    : One of four mutually exclusive options.
 *
 *         1.) ADD_DEVICE:      Adds a new device = line or files = line.
 *         3.) ADD_DEVICE_FIRST Adds new device = line as the FIRST device.
 *         3.) REMOVE_DEVICE:   Removes entire device = line or files = line.
 *         4.) RETURN_PRESENCE: Return BOOL as to presence of device = line
 *                              or files = line.
 *         5.) REMOUT_DEVICE:   Comment out the device line in question.
 *
 *        szConfigLine : In the case of RETURN_PRESENCE This must be a near
 *                       pointer to a buffer of sufficient size to hold the
 *                       requested line from config.sys (MAXCMDLINELEN).
 *                       
 *
 *  NOTES: When adding a device = line to config.sys, it will be placed into
 *         the files as the second device = line. When adding a files = line
 *         it will be made the first line in the file.
 *
 *  RETURNS: On add or remove return value indicates success or failure.
 *           On return_presence, return value indicates presence
 *           or lack of presence of config.sys line.
 *	
 *               
 */
BOOL NEAR PASCAL fnUpdateDevice(szDeviceName,Opt_Flag,szConfigLine)
PSTR      szDeviceName;
unsigned  Opt_Flag;
PSTR      szConfigLine;
{

   BOOL           bFiles = FALSE;
   LPSTR          lpFileOffset = lpConfigBuff;
   LPSTR          lpFileBuf = lpConfigBuff;
   LPSTR          pTo;
   LPSTR          lpTmpHold;
   unsigned       iCharCnt;
   char           szDevice[7];

   /* Initial seek to beginning of first line in config.sys buffer. */

   while ( ISWHITE(*lpFileOffset) ) 
      ++lpFileOffset;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -