diff --git a/dlls/msvcrt/file.c b/dlls/msvcrt/file.c index 9d75bc61e79..2db13f883ad 100644 --- a/dlls/msvcrt/file.c +++ b/dlls/msvcrt/file.c @@ -2159,10 +2159,20 @@ int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf) TRACE(":file (%s) buf(%p)\n",path,buf); + plen = strlen(path); + while (plen && path[plen-1]==' ') + plen--; + + if (plen && (path[plen-1]=='\\' || path[plen-1]=='/')) + { + *MSVCRT__errno() = MSVCRT_ENOENT; + return -1; + } + if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi)) { TRACE("failed (%d)\n",GetLastError()); - msvcrt_set_errno(ERROR_FILE_NOT_FOUND); + *MSVCRT__errno() = MSVCRT_ENOENT; return -1; } @@ -2178,11 +2188,8 @@ int CDECL MSVCRT_stat64(const char* path, struct MSVCRT__stat64 * buf) else buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1; - plen = strlen(path); - /* Dir, or regular file? */ - if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) || - (path[plen-1] == '\\')) + if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC); else { @@ -2252,10 +2259,20 @@ int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * bu TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf); + plen = strlenW(path); + while (plen && path[plen-1]==' ') + plen--; + + if(plen && (path[plen-1]=='\\' || path[plen-1]=='/')) + { + *MSVCRT__errno() = MSVCRT_ENOENT; + return -1; + } + if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi)) { TRACE("failed (%d)\n",GetLastError()); - msvcrt_set_errno(ERROR_FILE_NOT_FOUND); + *MSVCRT__errno() = MSVCRT_ENOENT; return -1; } @@ -2267,11 +2284,8 @@ int CDECL MSVCRT__wstat64(const MSVCRT_wchar_t* path, struct MSVCRT__stat64 * bu else buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1; - plen = strlenW(path); - /* Dir, or regular file? */ - if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) || - (path[plen-1] == '\\')) + if (hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) mode |= (MSVCRT__S_IFDIR | ALL_S_IEXEC); else { diff --git a/dlls/msvcrt/tests/file.c b/dlls/msvcrt/tests/file.c index e450b725147..0078c2a21cd 100644 --- a/dlls/msvcrt/tests/file.c +++ b/dlls/msvcrt/tests/file.c @@ -1444,6 +1444,11 @@ static void test_stat(void) ok(buf.st_nlink == 1, "st_nlink is %d, expected 1\n", buf.st_nlink); ok(buf.st_size == 0, "st_size is %d, expected 0\n", buf.st_size); + errno = 0xdeadbeef; + ret = stat("stat.tst\\", &buf); + ok(ret == -1, "stat returned %d\n", ret); + ok(errno == ENOENT, "errno = %d\n", errno); + close(fd); remove("stat.tst"); } @@ -1483,6 +1488,25 @@ static void test_stat(void) } else skip("pipe failed with errno %d\n", errno); + + /* Tests for directory */ + if(mkdir("stat.tst") == 0) + { + ret = stat("stat.tst ", &buf); + ok(!ret, "stat(directory) failed: errno=%d\n", errno); + ok((buf.st_mode & _S_IFMT) == _S_IFDIR, "bad format = %06o\n", buf.st_mode); + ok((buf.st_mode & 0777) == 0777, "bad st_mode = %06o\n", buf.st_mode); + ok(buf.st_dev == buf.st_rdev, "st_dev (%d) and st_rdev (%d) differ\n", buf.st_dev, buf.st_rdev); + ok(buf.st_nlink == 1, "st_nlink is %d, expected 1\n", buf.st_nlink); + + errno = 0xdeadbeef; + ret = stat("stat.tst\\ ", &buf); + ok(ret == -1, "stat returned %d\n", ret); + ok(errno == ENOENT, "errno = %d\n", errno); + rmdir( "stat.tst" ); + } + else + skip("mkdir failed with errno %d\n", errno); } static const char* pipe_string="Hello world";