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

📄 dir.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 4 页
字号:
 *
 * The function that prints in new style
 */
static VOID
DirPrintNewList(LPWIN32_FIND_DATA ptrFiles[],	/* [IN]Files' Info */
		DWORD dwCount,			/* [IN] The quantity of files */
		TCHAR *szCurPath,		/* [IN] Full path of current directory */
		LPDIRSWITCHFLAGS lpFlags)	/* [IN] The flags used */
{
  DWORD i;
  TCHAR szSize[30];
  TCHAR szShortName[15];
  TCHAR szDate[20];
  TCHAR szTime[20];
  INT iSizeFormat;
  ULARGE_INTEGER u64FileSize;

  for (i = 0;i < dwCount;i++)
  {
    /* Calculate size */
    if (ptrFiles[i]->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
    {
      /* Junction */
      iSizeFormat = -14;
      _tcscpy(szSize, _T("<JUNCTION>"));
    }
    else if (ptrFiles[i]->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
      /* Directory */
      iSizeFormat = -14;
      _tcscpy(szSize, _T("<DIR>"));
    }
    else
    {
      /* File */
      iSizeFormat = 14;
      u64FileSize.HighPart = ptrFiles[i]->nFileSizeHigh;
      u64FileSize.LowPart = ptrFiles[i]->nFileSizeLow;
      ConvertULargeInteger(u64FileSize, szSize, 20, lpFlags->bTSeperator);
    }

    /* Calculate short name */
    szShortName[0] = _T('\0');
    if (lpFlags->bShortName)
      _stprintf(szShortName, _T(" %-12s"), ptrFiles[i]->cAlternateFileName);

    /* Format date and time */
    DirPrintFileDateTime(szDate, szTime, ptrFiles[i], lpFlags);

    /* Print the line */
    if(lpFlags->bPause)
	{
		if (ConOutPrintfPaging(FALSE,_T("%10s  %-8s    %*s%s %s\n"),
							szDate,
							szTime,
							iSizeFormat,
							szSize,
							szShortName,
							ptrFiles[i]->cFileName) == 1)
			return ;
	}
	else
		ConOutPrintf(_T("%10s  %-8s    %*s%s %s\n"),
							szDate,
							szTime,
							iSizeFormat,
							szSize,
							szShortName,
							ptrFiles[i]->cFileName);
  }
}


/*
 *  DirPrintWideList
 *
 * The function that prints in wide list
 */
static VOID
DirPrintWideList(LPWIN32_FIND_DATA ptrFiles[],	/* [IN] Files' Info */
				 DWORD dwCount,			/* [IN] The quantity of files */
				 TCHAR *szCurPath,		/* [IN] Full path of current directory */
				 LPDIRSWITCHFLAGS lpFlags)	/* [IN] The flags used */
{
  SHORT iScreenWidth;
  USHORT iColumns;
  USHORT iLines;
  UINT iLongestName;
  TCHAR szTempFname[MAX_PATH];
  DWORD i;
  DWORD j;
  DWORD temp;

  /* Calculate longest name */
  iLongestName = 1;
  for (i = 0; i < dwCount; i++)
  {
    if (ptrFiles[i]->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
      /* Directories need 2 additinal characters for brackets */
      if ((_tcslen(ptrFiles[i]->cFileName) + 2) > iLongestName)
        iLongestName = _tcslen(ptrFiles[i]->cFileName) + 2;
    }
    else
    {
      if (_tcslen(ptrFiles[i]->cFileName) > iLongestName)
        iLongestName = _tcslen(ptrFiles[i]->cFileName);
    }
  }

  /* Count the highest number of columns */
  GetScreenSize(&iScreenWidth, 0);
  iColumns = iScreenWidth / iLongestName;

  /* Check if there is enough space for spaces between names */
  if (((iLongestName * iColumns) + iColumns) >= (UINT)iScreenWidth)
    iColumns --;

  /* A last check at iColumns to avoid division by zero */
  if (!(iColumns))
    iColumns = 1;

  /* Print Column sorted */
  if (lpFlags->bWideListColSort)
  {
    /* Calculate the lines that will be printed */
//    iLines = ceil((float)dwCount/(float)iColumns);
    iLines = (USHORT)(dwCount / iColumns);

    for (i = 0;i < iLines;i++)
    {
      for (j = 0; j < iColumns; j++)
      {
        temp = (j * iLines) + i;
        if (temp >= dwCount)
           break;

        if (ptrFiles[temp]->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
          _stprintf(szTempFname, _T("[%s]"), ptrFiles[temp]->cFileName);
        else
          _stprintf(szTempFname, _T("%s"), ptrFiles[temp]->cFileName);

        if(lpFlags->bPause)
		   ConOutPrintfPaging(FALSE,_T("%-*s"), iLongestName + 1 , szTempFname);
		else
		   ConOutPrintf(_T("%-*s"), iLongestName + 1 , szTempFname);
      }

      if(lpFlags->bPause)
		 ConOutPrintfPaging(FALSE,_T("\n"));
	  else
		 ConOutPrintf(_T("\n"));
    }
  }
  else
  {
    /* Print Line sorted */
    for (i = 0; i < dwCount; i++)
    {
      if (ptrFiles[i]->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        _stprintf(szTempFname, _T("[%s]"), ptrFiles[i]->cFileName);
      else
        _stprintf(szTempFname, _T("%s"), ptrFiles[i]->cFileName);

      if(lpFlags->bPause)
		 ConOutPrintfPaging(FALSE,_T("%-*s"), iLongestName + 1, szTempFname );
	  else
		 ConOutPrintf(_T("%-*s"), iLongestName + 1, szTempFname );

      /*
       * We print a new line at the end of each column
       * except for the case that it is the last item.
       */
	  if (!((i + 1) % iColumns) && (i < (dwCount - 1)))
	  {
		  if(lpFlags->bPause)
			 ConOutPrintfPaging(FALSE,_T("\n"));
		  else
		     ConOutPrintf(_T("\n"));
	  }
    }

    /* Add a new line after the last item */
    if(lpFlags->bPause)
	   ConOutPrintfPaging(FALSE,_T("\n"));
	else
	   ConOutPrintf(_T("\n"));
  }
}


/*
 *  DirPrintOldList
 *
 * The function that prints in old style
 */
static VOID
DirPrintOldList(LPWIN32_FIND_DATA ptrFiles[],	/* [IN] Files' Info */
				DWORD dwCount,					/* [IN] The quantity of files */
				TCHAR * szCurPath,				/* [IN] Full path of current directory */
				LPDIRSWITCHFLAGS lpFlags)		/* [IN] The flags used */
{
DWORD i;						/* An indexer for "for"s */
TCHAR szName[10];				/* The name of file */
TCHAR szExt[5];					/* The extension of file */
TCHAR szDate[30],szTime[30];	/* Used to format time and date */
TCHAR szSize[30];				/* The size of file */
int iSizeFormat;				/* The format of size field */
ULARGE_INTEGER u64FileSize;		/* The file size */

	for(i = 0;i < dwCount;i++)
	{
		/* Broke 8.3 format */
		if (*ptrFiles[i]->cAlternateFileName )
		{
			/* If the file is long named then we read the alter name */
			getName( ptrFiles[i]->cAlternateFileName, szName);
			_tcscpy(szExt, getExt( ptrFiles[i]->cAlternateFileName));
		}
		else
		{
			/* If the file is not long name we read its original name */
			getName( ptrFiles[i]->cFileName, szName);
			_tcscpy(szExt, getExt( ptrFiles[i]->cFileName));
		}

		/* Calculate size */
		if (ptrFiles[i]->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			/* Directory, no size it's a directory*/
			iSizeFormat = -17;
			_tcscpy(szSize, _T("<DIR>"));
		}
		else
		{
			/* File */
			iSizeFormat = 17;
			u64FileSize.HighPart = ptrFiles[i]->nFileSizeHigh;
			u64FileSize.LowPart = ptrFiles[i]->nFileSizeLow;
			ConvertULargeInteger(u64FileSize, szSize, 20, lpFlags->bTSeperator);
		}

		/* Format date and time */
		DirPrintFileDateTime(szDate,szTime,ptrFiles[i],lpFlags);

		/* Print the line */
		if(lpFlags->bPause)
		{
		   if (ConOutPrintfPaging(FALSE,_T("%-8s %-3s  %*s %s  %s\n"),
								szName,			/* The file's 8.3 name */
								szExt,			/* The file's 8.3 extension */
								iSizeFormat,	/* print format for size column */
								szSize,			/* The size of file or "<DIR>" for dirs */
								szDate,			/* The date of file/dir */
								szTime) == 1)		/* The time of file/dir */
			{
				return ;
			}
		}
		else
		   ConOutPrintf(_T("%-8s %-3s  %*s %s  %s\n"),
								szName,			/* The file's 8.3 name */
								szExt,			/* The file's 8.3 extension */
								iSizeFormat,	/* print format for size column */
								szSize,			/* The size of file or "<DIR>" for dirs */
								szDate,			/* The date of file/dir */
								szTime);		/* The time of file/dir */
	}
}

/*
 *  DirPrintBareList
 *
 * The function that prints in bare format
 */
static VOID
DirPrintBareList(LPWIN32_FIND_DATA ptrFiles[],	/* [IN] Files' Info */
				 DWORD dwCount,			/* [IN] The number of files */
				 LPTSTR lpCurPath,		/* [IN] Full path of current directory */
				 LPDIRSWITCHFLAGS lpFlags)	/* [IN] The flags used */
{
	TCHAR szFullName[MAX_PATH];
	DWORD i;

	for (i = 0; i < dwCount; i++)
	{
		if ((_tcscmp(ptrFiles[i]->cFileName, _T(".")) == 0) ||
		    (_tcscmp(ptrFiles[i]->cFileName, _T("..")) == 0))
		{
			/* at bare format we don't print "." and ".." folder */
			continue;
		}
		if (lpFlags->bRecursive)
		{
			/* at recursive mode we print full path of file */
			_tcscpy(szFullName, lpCurPath);
			_tcscat(szFullName, ptrFiles[i]->cFileName);
			if(lpFlags->bPause)
			{
			   if (ConOutPrintfPaging(FALSE,_T("%s\n"), szFullName) == 1)
				{
					return ;
				}
			}
			else
			   ConOutPrintf(_T("%s\n"), szFullName);
		}
		else
		{
			/* if we are not in recursive mode we print the file names */
			if(lpFlags->bPause)
			{
			   if (ConOutPrintfPaging(FALSE,_T("%s\n"),ptrFiles[i]->cFileName) == 1)
				{
					return ;
				}
			}
			else
			   ConOutPrintf(_T("%s\n"),ptrFiles[i]->cFileName);
		}
	}
}


/*
 * DirPrintFiles
 *
 * The functions that prints the files list
 */
static VOID
DirPrintFiles(LPWIN32_FIND_DATA ptrFiles[],	/* [IN] Files' Info */
			  DWORD dwCount,			/* [IN] The quantity of files */
			  TCHAR *szCurPath,			/* [IN] Full path of current directory */
			  LPDIRSWITCHFLAGS lpFlags)		/* [IN] The flags used */
{
	TCHAR szMsg[RC_STRING_MAX_SIZE];
	TCHAR szTemp[MAX_PATH];			/* A buffer to format the directory header */
	LPTSTR pszFilePart;
	SIZE_T len;

	/* We cut the trailing \ of the full path, unless the path is a drive */
	if (GetFullPathName(szCurPath, sizeof(szTemp) / sizeof(TCHAR), szTemp, &pszFilePart) == 0)
	{
		pszFilePart = NULL;
		_tcscpy(szTemp, szCurPath);
	}
	else if (pszFilePart != NULL)
		*pszFilePart = _T('\0');
	else
	{
		len = _tcslen(szTemp);
		if (len > 0 && szTemp[len - 1] != _T('\\') &&
		    GetFileAttributes(szTemp) == INVALID_FILE_ATTRIBUTES &&
		    GetLastError() == ERROR_PATH_NOT_FOUND)
		{
			/* Special case for some fake dos devices, such as con:
			   GetFullPathName doesn't return a pszFilePart pointer
			   so we're going to fix this ourselves */
			while (len > 0 && szTemp[len - 1] != _T('\\'))
				szTemp[--len] = _T('\0');
		}
	}

	len = _tcslen(szTemp);
	if ((len != 3 || szTemp[len - 2] != _T(':')) && szTemp[len - 1] == _T('\\'))
		szTemp[len-1] = _T('\0');

	/* Condition to print header:
	   We are not printing in bare format
	   and if we are in recursive mode... we must have results */
	if (!(lpFlags->bBareFormat ) && !((lpFlags->bRecursive) && (dwCount <= 0)))
	{
		LoadString(CMD_ModuleHandle, STRING_DIR_HELP7, szMsg, RC_STRING_MAX_SIZE);
		if(lpFlags->bPause)
		{
		   if (ConOutPrintfPaging(FALSE,szMsg, szTemp) == 1)
			{
				return ;
			}
		}
		else
		   ConOutPrintf(szMsg, szTemp);
	}

	if (lpFlags->bBareFormat)
	{
		/* Bare format */
		DirPrintBareList(ptrFiles, dwCount, szCurPath, lpFlags);
	}
	else if(lpFlags->bShortName)
	{
		/* New list style / Short names */
		DirPrintNewList(ptrFiles, dwCount, szCurPath, lpFlags);
	}
	else if(lpFlags->bWideListColSort || lpFlags->bWideList)
	{
		/* Wide list */
		DirPrintWideList(ptrFiles, dwCount, szCurPath, lpFlags);
	}
	else if (lpFlags->bNewLongList )
	{
		/* New list style*/
		DirPrintNewList(ptrFiles, dwCount, szCurPath, lpFlags);
	}
	else
	{
		/* If nothing is selected old list is the default */
		DirPrintOldList(ptrFiles, dwCount, szCurPath, lpFlags);
	}
}



/*
 * CompareFiles
 *
 * Compares 2 files based on the order criteria
 */
static BOOL
CompareFiles(LPWIN32_FIND_DATA lpFile1,	/* [IN] A pointer to WIN32_FIND_DATA of file 1 */
			 LPWIN32_FIND_DATA lpFile2,	/* [IN] A pointer to WIN32_FIND_DATA of file 2 */
			 LPDIRSWITCHFLAGS lpFlags)	/* [IN] The flags that we use to list */
{
  ULARGE_INTEGER u64File1;
  ULARGE_INTEGER u64File2;
  int i;
  long iComp = 0;					/* The comparison result */

	/* Calculate critiries by order given from user */
	for (i = 0;i < lpFlags->stOrderBy.sCriteriaCount;i++)
	{

		/* Calculate criteria */
		switch(lpFlags->stOrderBy.eCriteria[i])
		{
		case ORDER_SIZE:		/* Order by size /o:s */
			/* concat the 32bit integers to a 64bit */
			u64File1.LowPart = lpFile1->nFileSizeLow;
			u64File1.HighPart = lpFile1->nFileSizeHigh;
			u64File2.LowPart = lpFile2->nFileSizeLow;
			u64File2.HighPart = lpFile2->nFileSizeHigh;

			/* In case that differnce is too big for a long */
			if (u64File1.QuadPart < u64File2.QuadPart)
				iComp = -1;
			else if (u64File1.QuadPart > u64File2.QuadPart)
				iComp = 1;
			else
				iComp = 0;
			break;

		case ORDER_DIRECTORY:	/* Order by directory attribute /o:g */
			iComp = ((lpFile2->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)-
				(lpFile1->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
			break;

		case ORDER_EXTENSION:	/* Order by extension name /o:e */
			iComp = _tcsicmp(getExt(lpFile1->cFileName),getExt(lpFile2->cFileName));
			break;

		case ORDER_NAME:		/* Order by filename /o:n */
			iComp = _tcsicmp(lpFile1->cFileName, lpFile2->cFileName);
			break;

		case ORDER_TIME:		/* Order by file's time /o:t */
			/* We compare files based on the time field selected by /t */
			switch(lpFlags->stTimeField.eTimeField)
			{
			case TF_CREATIONDATE:
				/* concat the 32bit integers to a 64bit */
				u64File1.LowPart = lpFile1->ftCreationTime.dwLowDateTime;
				u64File1.HighPart = lpFile1->ftCreationTime.dwHighDateTime ;
				u64File2.LowPart = lpFile2->ftCreationTime.dwLowDateTime;
				u64File2.HighPart = lpFile2->ftCreationTime.dwHighDateTime ;
				break;
			case TF_LASTACCESSEDDATE :
				/* concat the 32bit integers to a 64bit */
				u64File1.LowPart = lpFile1->ftLastAccessTime.dwLowDateTime;
				u64File1.HighPart = lpFile1->ftLastAccessTime.dwHighDateTime ;
				u64File2.LowPart = lpFile2->ftLastAccessTime.dwLowDateTime;
				u64File2.HighPart = lpFile2->ftLastAccessTime.dwHighDateTime ;
				break;
			case TF_MODIFIEDDATE:
				/* concat the 32bit integers to a 64bit */
				u64File1.LowPart = lpFile1->ftLastWriteTime.dwLowDateTime;
				u64File1.HighPart = lpFile1->ftLastWriteTime.dwHighDateTime ;
				u64File2.LowPart = lpFile2->ftLastWriteTime.dwLowDateTime;

⌨️ 快捷键说明

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