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 errno_t (__cdecl *p__get_fmode)(int*);
+static errno_t (__cdecl *p__set_fmode)(int);
static const char* get_base_name(const char *path)
{
p_fopen_s = (void*)GetProcAddress(hmod, "fopen_s");
p__wfopen_s = (void*)GetProcAddress(hmod, "_wfopen_s");
__pioinfo = (void*)GetProcAddress(hmod, "__pioinfo");
+ p__get_fmode = (void*)GetProcAddress(hmod, "_get_fmode");
+ p__set_fmode = (void*)GetProcAddress(hmod, "_set_fmode");
}
static void test_filbuf( void )
fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
file = fdopen (fd, "rb");
setvbuf(file,NULL,bufmodes[bufmode],2048);
+ if(bufmodes[bufmode] == _IOFBF)
+ ok(file->_bufsiz == 2048, "file->_bufsiz = %d\n", file->_bufsiz);
+ ok(file->_base != NULL, "file->_base = NULL\n");
ok(strlen(outbuffer) == (sizeof(outbuffer)-1),"strlen/sizeof error for bufmode=%x\n", bufmodes[bufmode]);
ok(fgets(buffer,sizeof(buffer),file) !=0,"fgets failed unexpected for bufmode=%x\n", bufmodes[bufmode]);
ok(fgets(buffer,sizeof(buffer),file) ==0,"fgets didn't signal EOF for bufmode=%x\n", bufmodes[bufmode]);
ok(tempfh->_cnt == 0, "_cnt on freshly opened file was %d\n", tempfh->_cnt);
setbuf(tempfh, NULL);
ok(tempfh->_cnt == 0, "_cnt on unbuffered file was %d\n", tempfh->_cnt);
+ ok(tempfh->_bufsiz == 2, "_bufsiz = %d\n", tempfh->_bufsiz);
/* Inlined putchar sets _cnt to -1. Native seems to ignore the value... */
tempfh->_cnt = 1234;
ret = _flsbuf('Q',tempfh);
const char name[] = "empty1";
char buff[16];
unsigned char *ubuff = (unsigned char*)buff;
- FILE *file;
+ FILE *file, *file2;
int ret;
int len;
ubuff[0], ubuff[1], ubuff[2]);
fclose(file);
+ /* test initial FILE values */
+ memset(file, 0xfe, sizeof(*file));
+ file->_flag = 0;
+ ret = p_fopen_s(&file2, name, "r");
+ ok(!ret, "fopen_s failed with %d\n", ret);
+ ok(file == file2, "file != file2 %p %p\n", file, file2);
+ ok(!file->_ptr, "file->_ptr != NULL\n");
+ ok(!file->_cnt, "file->_cnt != 0\n");
+ ok(!file->_base, "file->_base != NULL\n");
+ ok(file->_flag == 1, "file->_flag = %x\n", file->_flag);
+ ok(file->_file, "file->_file == 0\n");
+ ok(file->_charbuf == 0xfefefefe, "file->_charbuf = %x\n", file->_charbuf);
+ ok(file->_bufsiz == 0xfefefefe, "file->_bufsiz = %x\n", file->_bufsiz);
+ ok(!file->_tmpfname, "file->_tmpfname != NULL\n");
+ fclose(file2);
+
ok(_unlink(name) == 0, "Couldn't unlink file named '%s'\n", name);
}
return;
}
+ errno = 0xdeadbeef;
+ ret = _setmode(-2, 0);
+ ok(ret == -1, "_setmode returned %x, expected -1\n", ret);
+ ok(errno == EINVAL, "errno = %d\n", errno);
+
+ errno = 0xdeadbeef;
+ ret = _setmode(-2, _O_TEXT);
+ ok(ret == -1, "_setmode returned %x, expected -1\n", ret);
+ ok(errno == EBADF, "errno = %d\n", errno);
+
fd = _open(name, _O_CREAT|_O_WRONLY, _S_IWRITE);
ok(fd != -1, "failed to open file\n");
_close(fd);
_unlink(fname);
+
+ errno = 0xdeadbeef;
+ handle = (HANDLE)_get_osfhandle(fd);
+ ok(handle == INVALID_HANDLE_VALUE, "_get_osfhandle returned %p\n", handle);
+ ok(errno == EBADF, "errno = %d\n", errno);
}
static void test_setmaxstdio(void)
CloseHandle(tmp);
}
+static void test_write_flush_size(FILE *file, int bufsize)
+{
+ char *inbuffer;
+ char *outbuffer;
+ int size, fd;
+ fpos_t pos, pos2;
+
+ fd = fileno(file);
+ inbuffer = calloc(1, bufsize + 1);
+ outbuffer = calloc(1, bufsize + 1);
+ _snprintf(outbuffer, bufsize + 1, "0,1,2,3,4,5,6,7,8,9");
+
+ for (size = bufsize + 1; size >= bufsize - 1; size--) {
+ rewind(file);
+ ok(file->_cnt == 0, "_cnt should be 0 after rewind, but is %d\n", file->_cnt);
+ fwrite(outbuffer, 1, size, file);
+ /* lseek() below intentionally redirects the write in fflush() to detect
+ * if fwrite() has already flushed the whole buffer or not.
+ */
+ lseek(fd, 1, SEEK_SET);
+ fflush(file);
+ ok(file->_cnt == 0, "_cnt should be 0 after fflush, but is %d\n", file->_cnt);
+ fseek(file, 0, SEEK_SET);
+ ok(fread(inbuffer, 1, bufsize, file) == bufsize, "read failed\n");
+ if (size == bufsize)
+ ok(memcmp(outbuffer, inbuffer, bufsize) == 0, "missing flush by %d byte write\n", size);
+ else
+ ok(memcmp(outbuffer, inbuffer, bufsize) != 0, "unexpected flush by %d byte write\n", size);
+ }
+ rewind(file);
+ fwrite(outbuffer, 1, bufsize / 2, file);
+ fwrite(outbuffer + bufsize / 2, 1, bufsize / 2, file);
+ lseek(fd, 1, SEEK_SET);
+ fflush(file);
+ fseek(file, 0, SEEK_SET);
+ ok(fread(inbuffer, 1, bufsize, file) == bufsize, "read failed\n");
+ ok(memcmp(outbuffer, inbuffer, bufsize) != 0, "unexpected flush by %d/2 byte double write\n", bufsize);
+
+ ok(!fseek(file, -1, SEEK_END), "fseek failed\n");
+ ok(!fgetpos(file, &pos), "fgetpos failed\n");
+ ok(fread(inbuffer, 1, 1, file) == 1, "fread failed\n");
+ ok(file->_flag & _IOREAD, "file->_flag = %x\n", file->_flag);
+ ok(!file->_cnt, "file->_cnt = %d\n", file->_cnt);
+ ok(file->_ptr != file->_base, "file->_ptr == file->_base\n");
+ ok(fwrite(outbuffer, 1, bufsize, file), "fwrite failed\n");
+ ok(file->_flag & _IOREAD, "file->_flag = %x\n", file->_flag);
+ ok(!file->_cnt, "file->_cnt = %d\n", file->_cnt);
+ ok(file->_ptr == file->_base, "file->_ptr == file->_base\n");
+ ok(!fgetpos(file, &pos2), "fgetpos failed\n");
+ ok(pos+bufsize+1 == pos2, "pos = %d (%d)\n", (int)pos, (int)pos2);
+ free(inbuffer);
+ free(outbuffer);
+}
+
+static void test_write_flush(void)
+{
+ char iobuf[1024];
+ char *tempf;
+ FILE *file;
+
+ tempf = _tempnam(".","wne");
+ file = fopen(tempf, "wb+");
+ ok(file != NULL, "unable to create test file\n");
+ iobuf[0] = 0;
+ ok(file->_bufsiz == 4096, "incorrect default buffer size: %d\n", file->_bufsiz);
+ test_write_flush_size(file, file->_bufsiz);
+ setvbuf(file, iobuf, _IOFBF, sizeof(iobuf));
+ test_write_flush_size(file, sizeof(iobuf));
+ fclose(file);
+ unlink(tempf);
+ free(tempf);
+}
+
+static void test_close(void)
+{
+ ioinfo *stdout_info, stdout_copy, *stderr_info, stderr_copy;
+ int fd1, fd2, ret1, ret2, ret3, ret4;
+ DWORD flags;
+ HANDLE h;
+
+ /* test close on fds that use the same handle */
+ h = CreateFileA("fdopen.tst", GENERIC_READ|GENERIC_WRITE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
+ ok(h != INVALID_HANDLE_VALUE, "error opening fdopen.tst file\n");
+
+ fd1 = _open_osfhandle((intptr_t)h, 0);
+ ok(fd1 != -1, "_open_osfhandle failed (%d)\n", errno);
+ fd2 = _open_osfhandle((intptr_t)h, 0);
+ ok(fd2 != -1, "_open_osfhandle failed (%d)\n", errno);
+ ok(fd1 != fd2, "fd1 == fd2\n");
+
+ ok((HANDLE)_get_osfhandle(fd1) == h, "handles mismatch (%p != %p)\n",
+ (HANDLE)_get_osfhandle(fd1), h);
+ ok((HANDLE)_get_osfhandle(fd2) == h, "handles mismatch (%p != %p)\n",
+ (HANDLE)_get_osfhandle(fd2), h);
+ ret1 = close(fd1);
+ ok(!ret1, "close(fd1) failed (%d)\n", errno);
+ ok(!GetHandleInformation(h, &flags), "GetHandleInformation succeeded\n");
+ ok(close(fd2), "close(fd2) succeeded\n");
+
+ /* test close on stdout and stderr that use the same handle */
+ h = CreateFileA("fdopen.tst", GENERIC_READ|GENERIC_WRITE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
+ ok(h != INVALID_HANDLE_VALUE, "error opening fdopen.tst file\n");
+
+ /* tests output will not be visible from now on */
+ stdout_info = &__pioinfo[STDOUT_FILENO/MSVCRT_FD_BLOCK_SIZE][STDOUT_FILENO%MSVCRT_FD_BLOCK_SIZE];
+ stderr_info = &__pioinfo[STDERR_FILENO/MSVCRT_FD_BLOCK_SIZE][STDERR_FILENO%MSVCRT_FD_BLOCK_SIZE];
+ stdout_copy = *stdout_info;
+ stderr_copy = *stderr_info;
+ stdout_info->handle = h;
+ stderr_info->handle = h;
+
+ ret1 = close(STDOUT_FILENO);
+ ret2 = GetHandleInformation(h, &flags);
+ ret3 = close(STDERR_FILENO);
+ ret4 = GetHandleInformation(h, &flags);
+
+ *stdout_info = stdout_copy;
+ *stderr_info = stderr_copy;
+ SetStdHandle(STD_OUTPUT_HANDLE, stdout_info->handle);
+ SetStdHandle(STD_ERROR_HANDLE, stderr_info->handle);
+ /* stdout and stderr restored */
+
+ ok(!ret1, "close(STDOUT_FILENO) failed\n");
+ ok(ret2, "GetHandleInformation failed\n");
+ ok(!ret3, "close(STDERR_FILENO) failed\n");
+ ok(!ret4, "GetHandleInformation succeeded\n");
+
+ DeleteFileA( "fdopen.tst" );
+}
+
+static void test__creat(void)
+{
+ int fd, pos, count, readonly, old_fmode = 0, have_fmode;
+ char buf[6], testdata[4] = {'a', '\n', 'b', '\n'};
+
+ have_fmode = p__get_fmode && p__set_fmode && !p__get_fmode(&old_fmode);
+ if (!have_fmode)
+ win_skip("_fmode can't be set, skipping mode tests\n");
+
+ if (have_fmode)
+ p__set_fmode(_O_TEXT);
+ fd = _creat("_creat.tst", 0);
+ ok(fd > 0, "_creat failed\n");
+ _write(fd, testdata, 4);
+ if (have_fmode) {
+ pos = _tell(fd);
+ ok(pos == 6, "expected pos 6 (text mode), got %d\n", pos);
+ }
+ ok(_lseek(fd, SEEK_SET, 0) == 0, "_lseek failed\n");
+ count = _read(fd, buf, 6);
+ ok(count == 4, "_read returned %d, expected 4\n", count);
+ count = count > 0 ? count > 4 ? 4 : count : 0;
+ ok(memcmp(buf, testdata, count) == 0, "_read returned wrong contents\n");
+ _close(fd);
+ readonly = GetFileAttributesA("_creat.tst") & FILE_ATTRIBUTE_READONLY;
+ ok(readonly, "expected read-only file\n");
+ SetFileAttributesA("_creat.tst", FILE_ATTRIBUTE_NORMAL);
+ DeleteFileA("_creat.tst");
+
+ if (have_fmode)
+ p__set_fmode(_O_BINARY);
+ fd = _creat("_creat.tst", _S_IREAD | _S_IWRITE);
+ ok(fd > 0, "_creat failed\n");
+ _write(fd, testdata, 4);
+ if (have_fmode) {
+ pos = _tell(fd);
+ ok(pos == 4, "expected pos 4 (binary mode), got %d\n", pos);
+ }
+ ok(_lseek(fd, SEEK_SET, 0) == 0, "_lseek failed\n");
+ count = _read(fd, buf, 6);
+ ok(count == 4, "_read returned %d, expected 4\n", count);
+ count = count > 0 ? count > 4 ? 4 : count : 0;
+ ok(memcmp(buf, testdata, count) == 0, "_read returned wrong contents\n");
+ _close(fd);
+ readonly = GetFileAttributesA("_creat.tst") & FILE_ATTRIBUTE_READONLY;
+ ok(!readonly, "expected rw file\n");
+ SetFileAttributesA("_creat.tst", FILE_ATTRIBUTE_NORMAL);
+ DeleteFileA("_creat.tst");
+
+ if (have_fmode)
+ p__set_fmode(old_fmode);
+}
+
START_TEST(file)
{
int arg_c;
test_stdin();
test_mktemp();
test__open_osfhandle();
+ test_write_flush();
+ test_close();
+ test__creat();
/* Wait for the (_P_NOWAIT) spawned processes to finish to make sure the report
* file contains lines in the correct order