diff --git a/base/applications/cmdutils/attrib/attrib.c b/base/applications/cmdutils/attrib/attrib.c index 141ad49728..768049e18f 100644 --- a/base/applications/cmdutils/attrib/attrib.c +++ b/base/applications/cmdutils/attrib/attrib.c @@ -91,12 +91,14 @@ INT PrintAttribute( LPWSTR pszPath, LPWSTR pszFile, - BOOL bRecurse) + BOOL bRecurse, + BOOL bDirectories) { WIN32_FIND_DATAW findData; HANDLE hFind; WCHAR szFullName[MAX_PATH]; LPWSTR pszFileName; + BOOL bFound = FALSE; /* prepare full file name buffer */ wcscpy(szFullName, pszPath); @@ -111,22 +113,30 @@ PrintAttribute( hFind = FindFirstFileW(szFullName, &findData); if (hFind == INVALID_HANDLE_VALUE) { - ErrorMessage(GetLastError(), pszFile); - return 1; - } + /* prepare full file name buffer */ + wcscpy(szFullName, pszPath); + pszFileName = szFullName + wcslen(szFullName); + + /* append *.* */ + wcscpy(pszFileName, L"*.*"); + hFind = FindFirstFileW(szFullName, &findData); + if (hFind == INVALID_HANDLE_VALUE) + { + if ((GetLastError() != ERROR_DIRECTORY) && (GetLastError() != ERROR_SHARING_VIOLATION)) + ErrorMessage(GetLastError(), pszFile); + return 1; + } + } do { - if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) - continue; - if (!wcscmp(findData.cFileName, L".") || !wcscmp(findData.cFileName, L"..")) continue; wcscpy(pszFileName, findData.cFileName); wcscat(pszFileName, L"\\"); - PrintAttribute(szFullName, pszFile, bRecurse); + PrintAttribute(szFullName, pszFile, bRecurse, bDirectories); } while(FindNextFileW(hFind, &findData)); FindClose(hFind); @@ -139,14 +149,19 @@ PrintAttribute( hFind = FindFirstFileW(szFullName, &findData); if (hFind == INVALID_HANDLE_VALUE) { - ErrorMessage(GetLastError(), pszFile); + if (GetLastError() != ERROR_FILE_NOT_FOUND) + ErrorMessage(GetLastError(), pszFile); return 1; } do { - if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) - continue; + if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !bDirectories && (wcsicmp(findData.cFileName, pszFile) != 0)) + continue; + + if (!wcscmp(findData.cFileName, L".") || + !wcscmp(findData.cFileName, L"..")) + continue; wcscpy(pszFileName, findData.cFileName); @@ -157,14 +172,17 @@ PrintAttribute( (findData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? L'H' : L' ', (findData.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? L'R' : L' ', szFullName); + bFound = TRUE; } while(FindNextFileW(hFind, &findData)); FindClose(hFind); + if (!bFound && !bRecurse) + ErrorMessage(ERROR_FILE_NOT_FOUND, pszFile); + return 0; } - static INT ChangeAttribute( @@ -180,6 +198,7 @@ ChangeAttribute( DWORD dwAttribute; WCHAR szFullName[MAX_PATH]; LPWSTR pszFileName; + BOOL bFound = FALSE; /* prepare full file name buffer */ wcscpy(szFullName, pszPath); @@ -194,8 +213,20 @@ ChangeAttribute( hFind = FindFirstFileW(szFullName, &findData); if (hFind == INVALID_HANDLE_VALUE) { - ErrorMessage(GetLastError(), pszFile); - return 1; + /* prepare full file name buffer */ + wcscpy(szFullName, pszPath); + pszFileName = szFullName + wcslen(szFullName); + + /* append *.* */ + wcscpy(pszFileName, L"*.*"); + + hFind = FindFirstFileW(szFullName, &findData); + if (hFind == INVALID_HANDLE_VALUE) + { + if ((GetLastError() != ERROR_DIRECTORY) && (GetLastError() != ERROR_SHARING_VIOLATION)) + ErrorMessage(GetLastError(), pszFile); + return 1; + } } do @@ -223,13 +254,14 @@ ChangeAttribute( hFind = FindFirstFileW(szFullName, &findData); if (hFind == INVALID_HANDLE_VALUE) { - ErrorMessage(GetLastError(), pszFile); + if (GetLastError() != ERROR_FILE_NOT_FOUND) + ErrorMessage(GetLastError(), pszFile); return 1; } do { - if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (wcsicmp(findData.cFileName, pszFile) != 0)) continue; wcscpy(pszFileName, findData.cFileName); @@ -240,11 +272,15 @@ ChangeAttribute( { dwAttribute = (dwAttribute & ~dwMask) | dwAttrib; SetFileAttributes(szFullName, dwAttribute); + bFound = TRUE; } } while (FindNextFileW(hFind, &findData)); FindClose(hFind); + if (!bFound && !bRecurse) + ErrorMessage(ERROR_FILE_NOT_FOUND, pszFile); + return 0; } @@ -258,6 +294,7 @@ int wmain(int argc, WCHAR *argv[]) BOOL bDirectories = FALSE; DWORD dwAttrib = 0; DWORD dwMask = 0; + BOOL bFound = FALSE; /* Initialize the Console Standard Streams */ ConInitStdStreams(); @@ -364,7 +401,7 @@ int wmain(int argc, WCHAR *argv[]) szPath[len + 1] = UNICODE_NULL; } wcscpy(szFileName, L"*.*"); - PrintAttribute(szPath, szFileName, bRecurse); + PrintAttribute(szPath, szFileName, bRecurse, bDirectories); return 0; } @@ -381,10 +418,36 @@ int wmain(int argc, WCHAR *argv[]) *p = L'\0'; if (dwMask == 0) - PrintAttribute(szPath, szFileName, bRecurse); + { + PrintAttribute(szPath, szFileName, bRecurse, bDirectories); + bFound = TRUE; + } else + { ChangeAttribute(szPath, szFileName, dwMask, dwAttrib, bRecurse, bDirectories); + bFound = TRUE; + } + } + else + { + + if (!bFound && (dwMask != 0)) + { + DWORD len; + + len = GetCurrentDirectory(MAX_PATH, szPath); + if (szPath[len-1] != L'\\') + { + szPath[len] = L'\\'; + szPath[len + 1] = UNICODE_NULL; + } + wcscpy(szFileName, L"*.*"); + ChangeAttribute(szPath, szFileName, dwMask, + dwAttrib, bRecurse, bDirectories); + bFound = TRUE; + + } } }