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

📄 file.c

📁 [随书类]Dos6.0源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
   if ( ISEOF(*lpFileOffset) )
      return FALSE;

   if ( (Opt_Flag == ADD_DEVICE) || (Opt_Flag == ADD_DEVICE_FIRST) ) {

      /* Allocate new buff large enough for old + new device + CR,LF,NULL
         + another possible CR,LF,NULL */

      pTo = FALLOC(fnLstrlen(lpFileBuf) + strlen(szDeviceName) + 6);
      if (!pTo)
         return FALSE;

      lpConfigBuff = pTo;               // Save pointer to head of new buff.
      if (strnicmp(szDeviceName,FILES,strlen(FILES))) {
         // While not to device section yet, copy lines to new buffer.
         while (strncmpinf(pszDEVICE,lpFileOffset,6) && !ISEOF(*lpFileOffset)) {
            pTo = fnCopyBuf(lpFileOffset,pTo,fnLinelenl(lpFileOffset));
            lpFileOffset = fnNextConfigLine(lpFileOffset);
         }
         if ( Opt_Flag != ADD_DEVICE_FIRST ) {
            while ( !ISEOF(*lpFileOffset) ) {
               pTo = fnCopyBuf(lpFileOffset,pTo,fnLinelenl(lpFileOffset));
               lpFileOffset = fnNextConfigLine(lpFileOffset);
            }
         }
      }
      // Put new device line in.
      if ( ISEOF(*lpFileOffset) && !ISCRLF(*(lpFileOffset-1)) ) {
         *pTo = CR,++pTo;
         *pTo = LF,++pTo;
      }
      pTo = fnCopyBuf((LPSTR)szDeviceName,pTo,strlen(szDeviceName)); 
      *pTo = CR,++pTo;
      *pTo = LF,++pTo;
      fnCopyBuf(lpFileOffset,pTo,REMAINDER); // attach remainder of buff.
      FFREE(lpFileBuf);                      // Free the old buffer.
      return TRUE;                           // Return success !
   }
   else { 
      // We need to either remove or verify presence of a device.
      pTo = lpFileOffset;
      if (!strcmpi(szDeviceName,FILES)) {
         strcpy(szDevice,FILES);
         bFiles = TRUE;
      }
      else 
         strcpy(szDevice,pszDEVICE);

      /* Now, while we have not hit the end of the buffer ... */
      while (!(ISEOF(*lpFileOffset))) {
         pTo = lpFileOffset;
         if ( !strncmpinf(szDevice,lpFileOffset,strlen(szDevice)) ) {
            while ( *lpFileOffset != EQUAL && !ISEOL(*(lpFileOffset+1)) )
               ++lpFileOffset;
            ++lpFileOffset;
            while ( ISFILL(*lpFileOffset) && *lpFileOffset )
               ++lpFileOffset;
            while ( !ISWHITE(*lpFileOffset) && *lpFileOffset )
               ++lpFileOffset;
            --lpFileOffset;
            iCharCnt = 0;
            while ( !DEVICESEP(*lpFileOffset) && (lpFileOffset > pTo) )
               --lpFileOffset, ++iCharCnt;
            ++lpFileOffset;
            if ( bFiles || !strncmpinf(szDeviceName,lpFileOffset,iCharCnt) &&
               iCharCnt == strlen(szDeviceName) ) {
               switch(Opt_Flag) {        // !!! we found the device !!!.
                  case RETURN_PRESENCE:
                     fnCopyBuf(pTo,(LPSTR)szConfigLine,(fnLinelenl(pTo)));
                     fnTerminate(szConfigLine);
                     return TRUE;
                  case REMOVE_DEVICE:
                     lpFileOffset = fnNextConfigLine(lpFileOffset);
                     fnCopyBuf(lpFileOffset,pTo,REMAINDER);
                     return TRUE;
                  case REMOUT_DEVICE:
                     *pTo = CR,++pTo;
                     *pTo = LF,++pTo;
                     lpFileOffset = fnNextConfigLine(lpFileOffset);
                     fnCopyBuf(lpFileOffset,pTo,REMAINDER);
                     return TRUE;
               }
            }
         }
         lpFileOffset = fnNextConfigLine(lpFileOffset);
      }
      return FALSE;
   }
}

/* INT NEAR PASCAL strncmpinf(PSTR pch1, LPSTR pch2, int n);
 *
 * Function compares N chars of strings without regaurd to case. I needed
 * this so that I could compare a string in near mem with a string in far
 * mem.
 *
 * ENTRY: Near string pointer, far string pointer, number of chars to comp.
 *
 * RETURNS: 0 if compare == ok. non zero if strings do not compare.
 *
 */
int NEAR PASCAL strncmpinf(pch1, pch2, n)
PSTR      pch1;
LPSTR     pch2;
int       n;
{
   while (*pch1 && --n > 0 && toupper(*pch1) == toupper(*pch2))
      *pch1++,*pch2++;
   return toupper(*pch1) != toupper(*pch2);
}

/* LPSTR NEAR PASCAL fnCopyBuff(LPSTR pFrom, LPSTR pTo, unsigned iCnt);
 *
 * Function moves the remaining contents of a text buffer from one location
 * within the buffer to a new location within the buffer. This is used to
 * either remove or make room for an entry in the file buffer.
 *
 * ENTRY: pointers To and From designate where the remaining protion of the
 *        buffer will be moved to. The new buffer will be NULL terminated.
 *
 * EXIT:  Returns pointer to next available char position in the buffer.
 *
 */
LPSTR fnCopyBuf(pFrom,pTo,iCnt)
LPSTR     pFrom;
LPSTR     pTo;
unsigned  iCnt;
{
   // While not End of buffer or end of count.

   while ( *pFrom != 0 && iCnt != 0 ) {
      *pTo = *pFrom;                          // Do the move.
      ++pTo;
      ++pFrom;          // Increment buffer poointers.
      --iCnt;           // Decrement count.
   }
   if ( *pFrom == '\0' )
      *pTo = *pFrom;      // Terminate newly expanded or contracted buffer.
   return pTo;
}

/* unsigned fnLstrlen(LPSTR)
 *
 * Returns length of string not including null terminating
 * char (far pointer version).
 *
 * ENTRY:    LPSTR to buffer
 * EXIT:     length of string.
 * WARNING:
 * EFFECTS:  No global data effected.
 *
 */
unsigned fnLstrlen(lpBuf)
LPSTR  lpBuf;
{

   unsigned i = 0;

   while ( *lpBuf++ != '\0' )
      ++i;
   return i;

}

/* int fnLinelenl(LPSTR)
 *
 * Returns length of buffer line up to and including any LF and CR chars.
 *
 * (far pointer version).
 *
 * ENTRY:    LPSTR to buffer
 * EXIT:     length of line.
 * WARNING:
 * EFFECTS:  No global data effected.
 *
 */
int fnLinelenl(lpBuf)
LPSTR  lpBuf;
{

   unsigned i = 0;

   while (!ISEOL(*lpBuf))
      ++i,++lpBuf;

   while ( ISCRLF(*lpBuf) )
      ++i,++lpBuf;

   return i;

}

/* LPSTR fnNextConfigLine(LPSTR lpFileOffset);
 *
 * Advances Far pointer into config.sys files to the first non-white char
 * of the next line in the buffer containing the file. Will return null
 * in the EOF case.
 *
 * ENTRY: Far pointer into buffer holding file.
 *
 * EXIT:  Far pointer into buffer holding file. NULL on EOF.
 *
 */
LPSTR fnNextConfigLine(lpFileOffset)
LPSTR    lpFileOffset;
{

   while ( !ISEOL(*lpFileOffset) )   //seek end of line.
      ++lpFileOffset;
 
   /* seek to beginning of next line, or end of buffer. */
 
   while ( ISWHITE(*lpFileOffset) )
      ++lpFileOffset;

   return lpFileOffset;
}

/* int fnWriteFile(LPSTR szBuffer, int fh);
 *
 *  Function will determine length of buffer, write buffer out to file
 *  handle provided. Close the files, return condition of opertion.
 *
 *
 *
 */
int NEAR PASCAL fnWriteFile(szBuffer,fh)
LPSTR    szBuffer;
int      fh;
{

   unsigned   len;
   int        condition;

   len = fnLstrlen(szBuffer);
   if ( FWRITE(fh,szBuffer,len) != len )   /* Write Error ! */
      condition = FERROR();
   else
      condition = ERROR_OK;

   FCLOSE(fh);
   return condition;
}

/* void fnTerminate(char* Buffer);
 *
 * Function seeks to the end of the buffer
 * appended and NULL teriminates the buffer.
 *
 * ENTRY: buffer - Pointer to buffer containing path.
 *
 * EXIT:  None
 *
 */
void fnTerminate(Buffer)
char      *Buffer;
{
   unsigned     Index;

   Index = fnLinelenl(Buffer);
   --Index;

   while(ISWHITE(Buffer[Index]))  //Back up to some meat.
      --Index; 

   Buffer[Index+1] = '\0';
}

/* BOOL fnMystrstr(char *szSrcStr, char *szSearchStr);
 *
 * Function will return BOOL value as to weather the Search string exists
 * any where within the source string. The difference between this func
 * the C run time func is that this one is simpler and is also not case
 * sensitive.
 *
 * ENTRY: szSrcStr    - Char buffer to be searched.
 *
 *        szSearchStr - String that will be searched for.
 *
 * EXIT:  BOOL value as to weather or not string was found.
 *
 *
 * WARNING: Source and search strings MUST be null terminated.
 *          
 *
 */
BOOL fnMystrstr(szSrcStr, szSearchStr)
char       *szSrcStr;
char       *szSearchStr;
{
   unsigned      len;             // Get length of search string.

   len = strlen(szSearchStr);

   while ( !ISEOL(*szSrcStr) ) {
      if ( ! strnicmp(szSrcStr,szSearchStr,len))
         return TRUE;
      ++szSrcStr;
   }
   return FALSE;

}

#ifndef DOSONLY

/* BOOL fnModifyPath(unsigned WorkNeeded, PSTR szMouseName);
 *
 * This function can add the windows installation path to the users path
 * statement in autoexec.bat This function will also add a temp environment
 * variable to the users autoexec.bat file if specified.
 *
 * ENTRY: WorkNeeded - bit field that will specify any combination of the
 *        following options:
 *
 *        DO_PATH  : This option tells the func to munge the users path.
 *
 *        DO_MOUSE : This option will search for a mouse driver installation
 *                   and replace it with a new installation line which will
 *                   install the new mouse driver from the windows
 *                   installation directory.
 *
 * EXIT : Function returns a boolean as to the success, or failure.
 *
 */
BOOL NEAR PASCAL fnModifyPath(WorkNeeded,szMouseName)
unsigned     WorkNeeded;
PSTR         szMouseName;
{
   LPSTR          lpFileOffset = lpAutoBuff;
   LPSTR          pHead = lpAutoBuff;
   LPSTR          pTo;
   char           TmpWorkSpace[PHILSMAXPATH]; // large enough for path construction.
   char           *szSearch;         // used to search through a string.
   unsigned       len;               // used to calc size of new buffer.
   BOOL           bNew      = FALSE;
   BOOL           bMouseFnd = TRUE;
   BOOL           bPathFnd  = TRUE;
   BOOL           bTempFnd  = TRUE;
   BOOL           bBufMod   = FALSE;
   
   if (! WorkNeeded )
      return bBufMod;

   if ( WorkNeeded & DO_PATH )
      bPathFnd = FALSE;

   if ( WorkNeeded & DO_MOUSE )
      bMouseFnd = FALSE;

   if ( WorkNeeded & DO_TEMP )
      bTempFnd = FALSE;

   /* Initial seek to beginning of first line in autoexec.bat buffer. */

   while ( ISWHITE(*lpFileOffset) && *lpFileOffset != '\0' ) 
      ++lpFileOffset;
   if (*lpFileOffset == NULL)
      return FALSE;

   /* Allocate new buff big enough for the following new items: */

   len = (fnLstrlen(lpFileOffset) + /* Length of current autoexec.bat    */
   (strlen(szSetupPath))          + /* path, 1 for path.                 */
   7 );                             /* 2 CR, 2 LF, 2 NULL chars, 1 ';'   */

   if ( bIsNetSetup )               // If were adding a net source dir to the
      len += strlen(szDiskPath);    // path allocate space for it too !

   if ( WorkNeeded & DO_MOUSE )     // If we have to add a mouse install ?
      len += MAXPATHLEN;

   if ( WorkNeeded & DO_TEMP )         // strlen of installation dir + length
      len += (strlen(szSetupPath)+16); // of "set temp=tempCR,LF,NULL"

   pTo = FALLOC(len);
   if (!pTo)
      return FALSE;
   lpAutoBuff = pTo;               // Save pointer to head of new buff.

   /* Now, start looking through the autoexec.bat file for relevent info */
   
   while ( !ISEOF(*lpFileOffset) )  {     // While not at end of autoexec.bat
      bTempFnd |= fnTempLine(lpFileOffset);        // check if line is temp ?
      if ( (WorkNeeded & DO_PATH) && !bPathFnd ) {
         if ( fnPathline(lpFileOffset) ) {  // check if path ?
            bPathFnd = TRUE;

            /* A somewhat dangerous but usually safe assumption: The path
               will be less than PHILSMAXPATH chars. */

            fnCopyBuf(lpFileOffset,(LPSTR)TmpWorkSpace,fnLinelenl(lpFileOffset));
            fnTerminate(TmpWorkSpace);           // Find end, NULL terminate.
            if ( fnEditPath(TmpWorkSpace) ) {    // Edit the path.
               pTo = fnCopyBuf(TmpWorkSpace,pTo,strlen(TmpWorkSpace));
               *pTo = CR,++pTo;
               *pTo = LF,++pTo;
               bNew = TRUE;
               bBufMod = TRUE;
            }
         }
      }
      if ( (WorkNeeded & DO_MOUSE) && !bMouseFnd ) {
         if ( fnMouseline(lpFileOffset,pszMOUSE) ) {  // Mouse Line ?
            bMouseFnd = TRUE;
            if (! stricmp(szMouseName,pszADD_Y) ) {
               fnCopyBuf(lpFileOffset,(LPSTR)TmpWorkSpace,fnLinelenl(lpFileOffset));
               fnTerminate(TmpWorkSpace);
               if (! fnMystrstr(TmpWorkSpace,"\Y") )
                  strcat(TmpWorkSpace," /Y");
            }
            else {
               strcpy(TmpWorkSpace,szSetupPath);
               catpath(TmpWorkSpace,szMouseName);
               strcat(TmpWorkSpace,pszCOMEXT);
               strcat(TmpWorkSpace," /Y");
            }
            pTo = fnCopyBuf(TmpWorkSpace,pTo,strlen(TmpWorkSpace));
            *pTo = CR,++pTo;
            *pTo = LF,++pTo;
            bNew = TRUE;
            bBufMod = TRUE;
         }
      }
      if (! bNew )
        pTo = fnCopyBuf(lpFileOffset,pTo,fnLinelenl(lpFileOffset));
      bNew = FALSE;
      lpFileOffset = fnNextConfigLine(lpFileOffset);
      if ( bPathFnd && bMouseFnd && bTempFnd) {      // If all work is done !
         fnCopyBuf(lpFileOffset,pTo,REMAINDER);
         break;                                     // Then were done !!!
      }
   }
   if (! bPathFnd ) {      // Now if a path was not found, add one.
      if ( ISEOF(*lpFileOffset) && !ISCRLF(*(lpFileOffset-1)) ) {
         *pTo = CR,++pTo;
         *pTo = LF,++pTo;
      }
      strcpy(TmpWorkSpace,pszPATH);
      strcat(TmpWorkSpace,"=");
      strcat(TmpWorkSpace,szSetupPath);
      pTo = fnCopyBuf((LPSTR)(TmpWorkSpace),pTo,strlen(TmpWorkSpace));
      *pTo = CR,++pTo;
      *pTo = LF,++pTo;
      bBufMod = TRUE;
      pTo = fnCopyBuf(lpFileOffset,pTo,REMAINDER);
   }

⌨️ 快捷键说明

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