📄 dir.c
字号:
break;
}
/* Copy results */
_tcscpy(lpDate, szDate);
_tcscpy(lpTime, szTime);
}
static VOID
GetUserDiskFreeSpace(LPCTSTR lpRoot,
PULARGE_INTEGER lpFreeSpace)
{
PGETFREEDISKSPACEEX pGetFreeDiskSpaceEx;
HINSTANCE hInstance;
DWORD dwSecPerCl;
DWORD dwBytPerSec;
DWORD dwFreeCl;
DWORD dwTotCl;
ULARGE_INTEGER TotalNumberOfBytes, TotalNumberOfFreeBytes;
lpFreeSpace->QuadPart = 0;
hInstance = LoadLibrary(_T("KERNEL32"));
if (hInstance != NULL)
{
pGetFreeDiskSpaceEx = (PGETFREEDISKSPACEEX)GetProcAddress(hInstance,
#ifdef _UNICODE
"GetDiskFreeSpaceExW");
#else
"GetDiskFreeSpaceExA");
#endif
if (pGetFreeDiskSpaceEx != NULL)
{
if (pGetFreeDiskSpaceEx(lpRoot, lpFreeSpace, &TotalNumberOfBytes, &TotalNumberOfFreeBytes) == TRUE)
return;
}
FreeLibrary(hInstance);
}
GetDiskFreeSpace(lpRoot,
&dwSecPerCl,
&dwBytPerSec,
&dwFreeCl,
&dwTotCl);
lpFreeSpace->QuadPart = dwSecPerCl * dwBytPerSec * dwFreeCl;
}
/*
* print_summary: prints dir summary
* Added by Rob Lake 06/17/98 to compact code
* Just copied Tim's Code and patched it a bit
*
*/
static INT
PrintSummary(LPTSTR szPath,
ULONG ulFiles,
ULONG ulDirs,
ULARGE_INTEGER u64Bytes,
LPINT pLine,
LPDIRSWITCHFLAGS lpFlags)
{
TCHAR szMsg[RC_STRING_MAX_SIZE];
TCHAR szBuffer[64];
ULARGE_INTEGER uliFree;
TCHAR szRoot[] = _T("A:\\");
/* Here we check if we didn't find anything */
if (!(ulFiles + ulDirs))
{
error_file_not_found();
return 1;
}
/* In bare format we don't print results */
if (lpFlags->bBareFormat)
return 0;
/* Print recursive specific results */
/* Take this code offline to fix /S does not print duoble info */
if (lpFlags->bRecursive)
{
ConvertULargeInteger(u64Bytes, szBuffer, sizeof(szBuffer), lpFlags->bTSeperator);
LoadString(CMD_ModuleHandle, STRING_DIR_HELP5, szMsg, RC_STRING_MAX_SIZE);
if(lpFlags->bPause)
ConOutPrintfPaging(FALSE,szMsg,ulFiles, szBuffer);
else
ConOutPrintf(szMsg,ulFiles, szBuffer);
}
else
{
/* Print File Summary */
/* Condition to print summary is:
If we are not in bare format and if we have results! */
if (ulFiles > 0)
{
ConvertULargeInteger(u64Bytes, szBuffer, 20, lpFlags->bTSeperator);
LoadString(CMD_ModuleHandle, STRING_DIR_HELP8, szMsg, RC_STRING_MAX_SIZE);
if(lpFlags->bPause)
ConOutPrintfPaging(FALSE,szMsg,ulFiles, szBuffer);
else
ConOutPrintf(szMsg,ulFiles, szBuffer);
}
}
/* Print total directories and freespace */
szRoot[0] = szPath[0];
GetUserDiskFreeSpace(szRoot, &uliFree);
ConvertULargeInteger(uliFree, szBuffer, sizeof(szBuffer), lpFlags->bTSeperator);
LoadString(CMD_ModuleHandle, STRING_DIR_HELP6, (LPTSTR) szMsg, RC_STRING_MAX_SIZE);
if(lpFlags->bPause)
ConOutPrintfPaging(FALSE,szMsg,ulDirs, szBuffer);
else
ConOutPrintf(szMsg,ulDirs, szBuffer);
return 0;
}
/*
* getExt
*
* Get the extension of a filename
*/
TCHAR* getExt(const TCHAR* file)
{
static TCHAR *NoExt = _T("");
TCHAR* lastdot = _tcsrchr(file, _T('.'));
return (lastdot != NULL ? lastdot + 1 : NoExt);
}
/*
* getName
*
* Get the name of the file without extension
*/
static LPTSTR
getName(const TCHAR* file, TCHAR * dest)
{
int iLen;
LPTSTR end;
/* Check for "." and ".." folders */
if ((_tcscmp(file, _T(".")) == 0) ||
(_tcscmp(file, _T("..")) == 0))
{
_tcscpy(dest,file);
return dest;
}
end = _tcsrchr(file, _T('.'));
if (!end)
iLen = _tcslen(file);
else
iLen = (end - file);
_tcsncpy(dest, file, iLen);
*(dest + iLen) = _T('\0');
return dest;
}
/*
* DirPrintNewList
*
* 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_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 */
/* Print directory header */
_tcscpy(szTemp, szCurPath);
/* We cut the trailing \ of the full path */
szTemp[_tcslen(szTemp)-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)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -