msvcrt: Fix write() behaviour on "no console" file descriptors.

Signed-off-by: Piotr Caban <piotr@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Piotr Caban 2017-11-15 09:47:37 +01:00 committed by Alexandre Julliard
parent 9b2397d5c8
commit 7aa4b6d170
2 changed files with 26 additions and 5 deletions

View File

@ -3423,7 +3423,7 @@ int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
if (count > 32) if (count > 32)
TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count); TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
#endif #endif
if (hand == INVALID_HANDLE_VALUE) if (hand == INVALID_HANDLE_VALUE || fd == MSVCRT_NO_CONSOLE_FD)
{ {
*MSVCRT__errno() = MSVCRT_EBADF; *MSVCRT__errno() = MSVCRT_EBADF;
release_ioinfo(info); release_ioinfo(info);
@ -3451,14 +3451,14 @@ int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
} }
TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd, TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
hand, GetLastError()); hand, GetLastError());
*MSVCRT__errno() = MSVCRT_ENOSPC; msvcrt_set_errno(GetLastError());
} }
else else
{ {
unsigned int i, j, nr_lf, size; unsigned int i, j, nr_lf, size;
char *p = NULL; char *p = NULL;
const char *q; const char *q;
const char *s = buf, *buf_start = buf; const char *s = buf;
if (!(info->exflag & (EF_UTF8|EF_UTF16))) if (!(info->exflag & (EF_UTF8|EF_UTF16)))
{ {
@ -3577,8 +3577,8 @@ int CDECL MSVCRT__write(int fd, const void* buf, unsigned int count)
{ {
TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n", TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
fd, hand, GetLastError(), num_written); fd, hand, GetLastError(), num_written);
*MSVCRT__errno() = MSVCRT_ENOSPC; msvcrt_set_errno(GetLastError());
return s - buf_start; return -1;
} }
return count; return count;
} }
@ -3927,6 +3927,7 @@ int CDECL MSVCRT__flsbuf(int c, MSVCRT_FILE* file)
if(!(file->_flag & MSVCRT__IOWRT)) { if(!(file->_flag & MSVCRT__IOWRT)) {
if(!(file->_flag & MSVCRT__IORW)) { if(!(file->_flag & MSVCRT__IORW)) {
file->_flag |= MSVCRT__IOERR; file->_flag |= MSVCRT__IOERR;
*MSVCRT__errno() = MSVCRT_EBADF;
return MSVCRT_EOF; return MSVCRT_EOF;
} }
file->_flag |= MSVCRT__IOWRT; file->_flag |= MSVCRT__IOWRT;

View File

@ -1556,6 +1556,26 @@ static void test_invalid_stdin_child( void )
ok(ret == -1, "read(STDIN_FILENO) returned %d\n", ret); ok(ret == -1, "read(STDIN_FILENO) returned %d\n", ret);
ok(errno == EBADF, "errno = %d\n", errno); ok(errno == EBADF, "errno = %d\n", errno);
errno = 0xdeadbeef;
ret = _flsbuf('a', stdin);
ok(ret == EOF, "_flsbuf(stdin) returned %d\n", ret);
ok(errno == EBADF, "errno = %d\n", errno);
errno = 0xdeadbeef;
ret = fwrite(&c, 1, 1, stdin);
ok(!ret, "fwrite(stdin) returned %d\n", ret);
ok(errno == EBADF, "errno = %d\n", errno);
errno = 0xdeadbeef;
ret = write(-2, &c, 1);
ok(ret == -1, "write(-2) returned %d\n", ret);
ok(errno == EBADF, "errno = %d\n", errno);
errno = 0xdeadbeef;
ret = write(STDIN_FILENO, &c, 1);
ok(ret == -1, "write(STDIN_FILENO) returned %d\n", ret);
ok(errno == EBADF, "errno = %d\n", errno);
errno = 0xdeadbeef; errno = 0xdeadbeef;
ret = fclose(stdin); ret = fclose(stdin);
ok(ret == -1, "fclose(stdin) returned %d\n", ret); ok(ret == -1, "fclose(stdin) returned %d\n", ret);