- Clean up code for DIR command

- Fix bug where "DIR directoryname /S" wouldn't list subdirectories (you needed to do directoryname\*.*)
- Get rid of extra * that appeared in DIR /B /S output

svn path=/trunk/; revision=39910
This commit is contained in:
Jeffrey Morlan 2009-03-08 17:37:11 +00:00
parent 357903327b
commit be7941b485

View file

@ -565,6 +565,21 @@ DirReadParam(LPTSTR Line, /* [IN] The line with the parameters & switches */
return TRUE;
}
/* Print either with or without paging, depending on /P switch */
static INT
DirPrintf(LPDIRSWITCHFLAGS lpFlags, LPTSTR szFormat, ...)
{
INT iReturn = 0;
va_list arg_ptr;
va_start(arg_ptr, szFormat);
if (lpFlags->bPause)
iReturn = ConPrintfPaging(FALSE, szFormat, arg_ptr, STD_OUTPUT_HANDLE);
else
ConPrintf(szFormat, arg_ptr, STD_OUTPUT_HANDLE);
va_end(arg_ptr);
return iReturn;
}
/*
* PrintDirectoryHeader
@ -605,78 +620,22 @@ PrintDirectoryHeader(LPTSTR szPath, LPDIRSWITCHFLAGS lpFlags)
if (szVolName[0] != _T('\0'))
{
LoadString(CMD_ModuleHandle, STRING_DIR_HELP2, szMsg, RC_STRING_MAX_SIZE);
//needs to have first paramter as TRUE because
//this is the first output and need to clear the static
if(lpFlags->bPause)
ConOutPrintfPaging(TRUE,szMsg, szRootName[0], szVolName);
else
ConOutPrintf(szMsg, szRootName[0], szVolName);
DirPrintf(lpFlags, szMsg, szRootName[0], szVolName);
}
else
{
LoadString(CMD_ModuleHandle, STRING_DIR_HELP3, szMsg, RC_STRING_MAX_SIZE);
if(lpFlags->bPause)
ConOutPrintfPaging(TRUE,szMsg, szRootName[0]);
else
ConOutPrintf(szMsg, szRootName[0]);
DirPrintf(lpFlags, szMsg, szRootName[0]);
}
/* print the volume serial number if the return was successful */
LoadString(CMD_ModuleHandle, STRING_DIR_HELP4, (LPTSTR) szMsg, RC_STRING_MAX_SIZE);
if(lpFlags->bPause)
ConOutPrintfPaging(FALSE,szMsg,
HIWORD(dwSerialNr),
LOWORD(dwSerialNr));
else
ConOutPrintf(szMsg,
HIWORD(dwSerialNr),
LOWORD(dwSerialNr));
DirPrintf(lpFlags, szMsg, HIWORD(dwSerialNr), LOWORD(dwSerialNr));
return TRUE;
}
/*
* convert
*
* insert commas into a number
*
*/
#if 0
static INT
ConvertULong (ULONG num, LPTSTR des, INT len)
{
TCHAR temp[32];
INT c = 0;
INT n = 0;
if (num == 0)
{
des[0] = _T('0');
des[1] = _T('\0');
n = 1;
}
else
{
temp[31] = 0;
while (num > 0)
{
if (((c + 1) % (nNumberGroups + 1)) == 0)
temp[30 - c++] = cThousandSeparator;
temp[30 - c++] = (TCHAR)(num % 10) + _T('0');
num /= 10;
}
for (n = 0; n <= c; n++)
des[n] = temp[31 - c + n];
}
return n;
}
#endif
static VOID
DirPrintFileDateTime(TCHAR *lpDate,
TCHAR *lpTime,
@ -685,8 +644,6 @@ DirPrintFileDateTime(TCHAR *lpDate,
{
FILETIME ft;
SYSTEMTIME dt;
TCHAR szDate[30];
TCHAR szTime[30];
WORD wYear;
/* Select the right time field */
@ -717,20 +674,20 @@ DirPrintFileDateTime(TCHAR *lpDate,
{
case 0: /* mmddyy */
default:
_stprintf (szDate, _T("%02d%c%02d%c%0*d"),
_stprintf(lpDate, _T("%02d%c%02d%c%0*d"),
dt.wMonth, cDateSeparator,
dt.wDay, cDateSeparator,
lpFlags->b4Digit?4:2, wYear);
break;
case 1: /* ddmmyy */
_stprintf (szDate, _T("%02d%c%02d%c%0*d"),
_stprintf(lpDate, _T("%02d%c%02d%c%0*d"),
dt.wDay, cDateSeparator, dt.wMonth,
cDateSeparator,lpFlags->b4Digit?4:2, wYear);
break;
case 2: /* yymmdd */
_stprintf (szDate, _T("%0*d%c%02d%c%02d"),
_stprintf(lpDate, _T("%0*d%c%02d%c%02d"),
lpFlags->b4Digit?4:2, wYear, cDateSeparator,
dt.wMonth, cDateSeparator, dt.wDay);
break;
@ -740,20 +697,17 @@ DirPrintFileDateTime(TCHAR *lpDate,
{
case 0: /* 12 hour format */
default:
_stprintf (szTime,_T("%02d%c%02u%c"),
_stprintf(lpTime,_T("%02d%c%02u%c"),
(dt.wHour == 0 ? 12 : (dt.wHour <= 12 ? dt.wHour : dt.wHour - 12)),
cTimeSeparator,
dt.wMinute, (dt.wHour <= 11 ? _T('a') : _T('p')));
break;
case 1: /* 24 hour format */
_stprintf (szTime, _T("%02d%c%02u"),
_stprintf(lpTime, _T("%02d%c%02u"),
dt.wHour, cTimeSeparator, dt.wMinute);
break;
}
/* Copy results */
_tcscpy(lpDate, szDate);
_tcscpy(lpTime, szTime);
}
@ -771,7 +725,7 @@ GetUserDiskFreeSpace(LPCTSTR lpRoot,
lpFreeSpace->QuadPart = 0;
hInstance = LoadLibrary(_T("KERNEL32"));
hInstance = GetModuleHandle(_T("KERNEL32"));
if (hInstance != NULL)
{
pGetFreeDiskSpaceEx = (PGETFREEDISKSPACEEX)GetProcAddress(hInstance,
@ -785,7 +739,6 @@ GetUserDiskFreeSpace(LPCTSTR lpRoot,
if (pGetFreeDiskSpaceEx(lpRoot, lpFreeSpace, &TotalNumberOfBytes, &TotalNumberOfFreeBytes) == TRUE)
return;
}
FreeLibrary(hInstance);
}
GetDiskFreeSpace(lpRoot,
@ -837,10 +790,7 @@ PrintSummary(LPTSTR szPath,
{
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);
DirPrintf(lpFlags, szMsg, ulFiles, szBuffer);
}
else
{
@ -849,22 +799,16 @@ PrintSummary(LPTSTR szPath,
If we are not in bare format and if we have results! */
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);
DirPrintf(lpFlags, szMsg, ulFiles, szBuffer);
}
/* Print total directories and freespace */
if (!lpFlags->bRecursive || (TotalSummary && lpFlags->bRecursive))
if (!lpFlags->bRecursive || TotalSummary)
{
GetUserDiskFreeSpace(szPath, &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);
DirPrintf(lpFlags, szMsg, ulDirs, szBuffer);
}
return 0;
@ -967,19 +911,7 @@ DirPrintNewList(LPWIN32_FIND_DATA ptrFiles[], /* [IN]Files' Info */
DirPrintFileDateTime(szDate, szTime, ptrFiles[i], lpFlags);
/* Print the line */
if(lpFlags->bPause)
{
if (ConOutPrintfPaging(FALSE,_T("%10s %-6s %*s%s %s\n"),
szDate,
szTime,
iSizeFormat,
szSize,
szShortName,
ptrFiles[i]->cFileName) == 1)
return ;
}
else
ConOutPrintf(_T("%10s %-6s %*s%s %s\n"),
DirPrintf(lpFlags, _T("%10s %-6s %*s%s %s\n"),
szDate,
szTime,
iSizeFormat,
@ -1065,17 +997,11 @@ DirPrintWideList(LPWIN32_FIND_DATA ptrFiles[], /* [IN] Files' Info */
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);
DirPrintf(lpFlags, _T("%-*s"), iLongestName + 1, szTempFname);
}
/* Add a new line after the last item in the column */
if(lpFlags->bPause)
ConOutPrintfPaging(FALSE,_T("\n"));
else
ConOutPrintf(_T("\n"));
DirPrintf(lpFlags, _T("\n"));
}
}
@ -1135,21 +1061,7 @@ ULARGE_INTEGER u64FileSize; /* The file size */
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"),
DirPrintf(lpFlags, _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 */
@ -1170,7 +1082,6 @@ DirPrintBareList(LPWIN32_FIND_DATA ptrFiles[], /* [IN] Files' Info */
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 && !bCtrlBreak; i++)
@ -1184,30 +1095,12 @@ DirPrintBareList(LPWIN32_FIND_DATA ptrFiles[], /* [IN] Files' Info */
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);
DirPrintf(lpFlags, _T("%s\\%s\n"), lpCurPath, ptrFiles[i]->cFileName);
}
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);
DirPrintf(lpFlags, _T("%s\n"), ptrFiles[i]->cFileName);
}
}
}
@ -1226,35 +1119,11 @@ DirPrintFiles(LPWIN32_FIND_DATA ptrFiles[], /* [IN] Files' Info */
{
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');
/* Print trailing backslash for root directory of drive */
_tcscpy(szTemp, szCurPath);
if (_tcslen(szTemp) == 2 && szTemp[1] == _T(':'))
_tcscat(szTemp, _T("\\"));
/* Condition to print header:
We are not printing in bare format
@ -1262,15 +1131,8 @@ DirPrintFiles(LPWIN32_FIND_DATA ptrFiles[], /* [IN] Files' Info */
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 (DirPrintf(lpFlags, szMsg, szTemp))
return;
}
if (lpFlags->bBareFormat)
@ -1400,10 +1262,7 @@ CompareFiles(LPWIN32_FIND_DATA lpFile1, /* [IN] A pointer to WIN32_FIND_DATA of
}
/* Translate the value of iComp to boolean */
if (iComp > 0)
return TRUE;
else
return FALSE;
return iComp > 0;
}
/*
@ -1491,18 +1350,17 @@ DirList(LPTSTR szPath, /* [IN] The path that dir starts */
if (GetFullPathName(szPath, sizeof(szFullPath) / sizeof(TCHAR), szFullPath, &pszFilePart) == 0)
{
_tcscpy (szFullPath, szPath);
if (szFullPath[_tcslen(szFullPath) - 1] != _T('\\'))
_tcscat (szFullPath, _T("\\"));
pszFilePart = NULL;
}
/* If no wildcard or file was specified and this is a directory, then
display all files in it */
wfdFileInfo.dwFileAttributes = GetFileAttributes(szFullPath);
if (wfdFileInfo.dwFileAttributes != INVALID_FILE_ATTRIBUTES &&
(wfdFileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
if (pszFilePart == NULL || IsExistingDirectory(szFullPath))
{
_tcscat(szFullPath, _T("\\*"));
pszFilePart = &szFullPath[_tcslen(szFullPath)];
if (pszFilePart[-1] != _T('\\'))
*pszFilePart++ = _T('\\');
_tcscpy(pszFilePart, _T("*"));
}
/* Prepare the linked list, first node is allocated */
@ -1520,9 +1378,9 @@ DirList(LPTSTR szPath, /* [IN] The path that dir starts */
/* Collect the results for the current folder */
hSearch = FindFirstFile(szFullPath, &wfdFileInfo);
do
if (hSearch != INVALID_HANDLE_VALUE)
{
if (hSearch != INVALID_HANDLE_VALUE)
do
{
/*If retrieved FileName has extension,and szPath doesnt have extension then JUMP the retrieved FileName*/
if(_tcschr(wfdFileInfo.cFileName,_T('.'))&&(fPoint==TRUE))
@ -1543,6 +1401,7 @@ DirList(LPTSTR szPath, /* [IN] The path that dir starts */
ptrStartNode = ptrNextNode;
dwCount --;
}
FindClose(hSearch);
return 1;
}
@ -1582,9 +1441,9 @@ DirList(LPTSTR szPath, /* [IN] The path that dir starts */
}
}
}
}
} while(FindNextFile(hSearch, &wfdFileInfo));
FindClose(hSearch);
} while (FindNextFile(hSearch, &wfdFileInfo));
FindClose(hSearch);
}
/* Terminate list */
ptrNextNode->ptrNext = NULL;
@ -1622,7 +1481,9 @@ DirList(LPTSTR szPath, /* [IN] The path that dir starts */
QsortFiles(ptrFileArray, 0, dwCount-1, lpFlags);
/* Print Data */
pszFilePart[-1] = _T('\0'); /* truncate to directory name only */
DirPrintFiles(ptrFileArray, dwCount, szFullPath, lpFlags);
pszFilePart[-1] = _T('\\');
if (lpFlags->bRecursive)
{
@ -1660,20 +1521,13 @@ DirList(LPTSTR szPath, /* [IN] The path that dir starts */
if (lpFlags->bRecursive)
{
/* The new search is involving any *.* file */
if (pszFilePart != NULL)
{
memcpy(szSubPath, szFullPath, (pszFilePart - szFullPath) * sizeof(TCHAR));
szSubPath[pszFilePart - szFullPath] = _T('\0');
}
else
_tcscpy(szSubPath, szFullPath);
_tcscat(szSubPath, _T("*.*"));
memcpy(szSubPath, szFullPath, (pszFilePart - szFullPath) * sizeof(TCHAR));
_tcscpy(&szSubPath[pszFilePart - szFullPath], _T("*.*"));
hRecSearch = FindFirstFile (szSubPath, &wfdFileInfo);
do
if (hRecSearch != INVALID_HANDLE_VALUE)
{
if (hRecSearch != INVALID_HANDLE_VALUE)
do
{
/* We search for directories other than "." and ".." */
if ((_tcsicmp(wfdFileInfo.cFileName, _T(".")) != 0) &&
@ -1681,27 +1535,20 @@ DirList(LPTSTR szPath, /* [IN] The path that dir starts */
(wfdFileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
/* Concat the path and the directory to do recursive */
if (pszFilePart != NULL)
{
memcpy(szSubPath, szFullPath, (pszFilePart - szFullPath) * sizeof(TCHAR));
szSubPath[pszFilePart - szFullPath] = _T('\0');
}
else
_tcscpy(szSubPath, szFullPath);
_tcscat(szSubPath, wfdFileInfo.cFileName);
memcpy(szSubPath, szFullPath, (pszFilePart - szFullPath) * sizeof(TCHAR));
_tcscpy(&szSubPath[pszFilePart - szFullPath], wfdFileInfo.cFileName);
_tcscat(szSubPath, _T("\\"));
if (pszFilePart != NULL)
_tcscat(szSubPath, pszFilePart);
_tcscat(szSubPath, pszFilePart);
/* We do the same for the folder */
if (DirList(szSubPath, lpFlags) != 0)
{
FindClose(hRecSearch);
return 1;
}
}
}
}while(FindNextFile(hRecSearch,&wfdFileInfo));
} while(FindNextFile(hRecSearch, &wfdFileInfo));
}
FindClose(hRecSearch);
}
@ -1776,6 +1623,10 @@ CommandDir(LPTSTR rest)
prev_volume[0] = _T('\0');
/* Reset paging state */
if (stFlags.bPause)
ConOutPrintfPaging(TRUE, _T(""));
for(loop = 0; loop < (UINT)entries; loop++)
{
if (CheckCtrlBreak(BREAK_INPUT))