Eryk Wieliczko : msvcrt: Fixed (_w)fopen_s with test.

Alexandre Julliard julliard at winehq.org
Thu Nov 11 12:05:51 CST 2010


Module: wine
Branch: master
Commit: c9bd269db11dcc7e3b0fe49135ba9d0d0a4f8eac
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=c9bd269db11dcc7e3b0fe49135ba9d0d0a4f8eac

Author: Eryk Wieliczko <ewdevel at gmail.com>
Date:   Wed Nov 10 12:34:42 2010 +0100

msvcrt: Fixed (_w)fopen_s with test.

---

 dlls/msvcrt/file.c       |    4 +-
 dlls/msvcrt/tests/file.c |   91 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+), 2 deletions(-)

diff --git a/dlls/msvcrt/file.c b/dlls/msvcrt/file.c
index 329cdeb..91cd448 100644
--- a/dlls/msvcrt/file.c
+++ b/dlls/msvcrt/file.c
@@ -2742,7 +2742,7 @@ int CDECL MSVCRT_fopen_s(MSVCRT_FILE** pFile,
         const char *filename, const char *mode)
 {
     if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
-        MSVCRT_CHECK_PMT(mode != NULL)) {
+        !MSVCRT_CHECK_PMT(mode != NULL)) {
         *MSVCRT__errno() = MSVCRT_EINVAL;
         return MSVCRT_EINVAL;
     }
@@ -2769,7 +2769,7 @@ int CDECL MSVCRT__wfopen_s(MSVCRT_FILE** pFile, const MSVCRT_wchar_t *filename,
         const MSVCRT_wchar_t *mode)
 {
     if (!MSVCRT_CHECK_PMT(pFile != NULL) || !MSVCRT_CHECK_PMT(filename != NULL) ||
-        MSVCRT_CHECK_PMT(mode != NULL)) {
+        !MSVCRT_CHECK_PMT(mode != NULL)) {
         *MSVCRT__errno() = MSVCRT_EINVAL;
         return MSVCRT_EINVAL;
     }
diff --git a/dlls/msvcrt/tests/file.c b/dlls/msvcrt/tests/file.c
index ef7b28d..488c219 100644
--- a/dlls/msvcrt/tests/file.c
+++ b/dlls/msvcrt/tests/file.c
@@ -36,6 +36,17 @@
 
 static HANDLE proc_handles[2];
 
+static int (__cdecl *p_fopen_s)(FILE**, const char*, const char*);
+static int (__cdecl *p__wfopen_s)(FILE**, const wchar_t*, const wchar_t*);
+
+static void init(void)
+{
+    HMODULE hmod = GetModuleHandleA("msvcrt.dll");
+
+    p_fopen_s = (void*)GetProcAddress(hmod, "fopen_s");
+    p__wfopen_s = (void*)GetProcAddress(hmod, "_wfopen_s");
+}
+
 static void test_filbuf( void )
 {
     FILE *fp;
@@ -1201,6 +1212,82 @@ static void test_fopen_fclose_fcloseall( void )
     ok(_unlink(fname3) == 0, "Couldn't unlink file named '%s'\n", fname3);
 }
 
+static void test_fopen_s( void )
+{
+    const char name[] = "empty1";
+    char buff[16];
+    FILE *file;
+    int ret;
+    int len;
+
+    if (!p_fopen_s)
+    {
+        win_skip("Skipping fopen_s test\n");
+        return;
+    }
+    /* testing fopen_s */
+    ret = p_fopen_s(&file, name, "w");
+    ok(ret == 0, "fopen_s failed with %d\n", ret);
+    ok(file != 0, "fopen_s failed to return value\n");
+    fwrite(name, sizeof(name), 1, file);
+
+    ret = fclose(file);
+    ok(ret != EOF, "File failed to close\n");
+
+    file = fopen(name, "r");
+    ok(file != 0, "fopen failed\n");
+    len = fread(buff, 1, sizeof(name), file);
+    ok(len == sizeof(name), "File length supposed to be %d, not %d\n", sizeof(name), len);
+    buff[sizeof(name)] = '\0';
+    ok(strcmp(name, buff) == 0, "File content mismatch! Got %s, expected %s\n", buff, name);
+
+    ret = fclose(file);
+    ok(ret != EOF, "File failed to close\n");
+
+    ok(_unlink(name) == 0, "Couldn't unlink file named '%s'\n", name);
+}
+
+static void test__wfopen_s( void )
+{
+    const char name[] = "empty1";
+    const WCHAR wname[] = {
+       'e','m','p','t','y','1',0
+    };
+    const WCHAR wmode[] = {
+       'w',0
+    };
+    char buff[16];
+    FILE *file;
+    int ret;
+    int len;
+
+    if (!p__wfopen_s)
+    {
+        win_skip("Skipping _wfopen_s test\n");
+        return;
+    }
+    /* testing _wfopen_s */
+    ret = p__wfopen_s(&file, wname, wmode);
+    ok(ret == 0, "_wfopen_s failed with %d\n", ret);
+    ok(file != 0, "_wfopen_s failed to return value\n");
+    fwrite(name, sizeof(name), 1, file);
+
+    ret = fclose(file);
+    ok(ret != EOF, "File failed to close\n");
+
+    file = fopen(name, "r");
+    ok(file != 0, "fopen failed\n");
+    len = fread(buff, 1, sizeof(name), file);
+    ok(len == sizeof(name), "File length supposed to be %d, not %d\n", sizeof(name), len);
+    buff[sizeof(name)] = '\0';
+    ok(strcmp(name, buff) == 0, "File content mismatch! Got %s, expected %s\n", buff, name);
+
+    ret = fclose(file);
+    ok(ret != EOF, "File failed to close\n");
+
+    ok(_unlink(name) == 0, "Couldn't unlink file named '%s'\n", name);
+}
+
 static void test_get_osfhandle(void)
 {
     int fd;
@@ -1424,6 +1511,8 @@ START_TEST(file)
     int arg_c;
     char** arg_v;
 
+    init();
+
     arg_c = winetest_get_mainargs( &arg_v );
 
     /* testing low-level I/O */
@@ -1450,6 +1539,8 @@ START_TEST(file)
     test_filbuf();
     test_fdopen();
     test_fopen_fclose_fcloseall();
+    test_fopen_s();
+    test__wfopen_s();
     test_fileops();
     test_asciimode();
     test_asciimode2();




More information about the wine-cvs mailing list