From b8780863c039a3ff8f1201c73126272f9e302ed2 Mon Sep 17 00:00:00 2001 From: Amine Khaldi Date: Mon, 12 May 2014 19:54:29 +0000 Subject: [PATCH] [CRT] * Update _write(). CORE-8080 svn path=/trunk/; revision=63264 --- reactos/lib/sdk/crt/stdio/file.c | 227 +++++++++++++++++++++---------- 1 file changed, 154 insertions(+), 73 deletions(-) diff --git a/reactos/lib/sdk/crt/stdio/file.c b/reactos/lib/sdk/crt/stdio/file.c index 64a4cc04b10..2274b92b96a 100644 --- a/reactos/lib/sdk/crt/stdio/file.c +++ b/reactos/lib/sdk/crt/stdio/file.c @@ -2194,89 +2194,170 @@ int CDECL _umask(int umask) */ int CDECL _write(int fd, const void* buf, unsigned int count) { - DWORD num_written; - HANDLE hand = fdtoh(fd); + DWORD num_written; + ioinfo *info = get_ioinfo(fd); + HANDLE hand = info->handle; - /* Don't trace small writes, it gets *very* annoying */ + /* Don't trace small writes, it gets *very* annoying */ #if 0 - if (count > 32) - TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count); + if (count > 32) + TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count); #endif - if (hand == INVALID_HANDLE_VALUE) + if (hand == INVALID_HANDLE_VALUE) { - *_errno() = EBADF; - return -1; + *_errno() = EBADF; + return -1; + } + + if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1) + { + *_errno() = EINVAL; + return -1; } - /* If appending, go to EOF */ - if (get_ioinfo(fd)->wxflag & WX_APPEND) - _lseek(fd, 0, FILE_END); + /* If appending, go to EOF */ + if (info->wxflag & WX_APPEND) + _lseek(fd, 0, FILE_END); - if (!(get_ioinfo(fd)->wxflag & WX_TEXT)) + if (!(info->wxflag & WX_TEXT)) { - if (WriteFile(hand, buf, count, &num_written, NULL) - && (num_written == count)) - return num_written; - TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd, - hand, GetLastError()); - *_errno() = ENOSPC; + if (WriteFile(hand, buf, count, &num_written, NULL) + && (num_written == count)) + return num_written; + TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd, + hand, GetLastError()); + *_errno() = ENOSPC; } - else - { - unsigned int i, j, nr_lf; - char *p = NULL; - const char *q; - const char *s = buf, *buf_start = buf; - /* find number of \n ( without preceding \r ) */ - for ( nr_lf=0,i = 0; i 1) && (s[i-1] == '\r')) nr_lf--; */ - } - } - if (nr_lf) - { - if ((q = p = malloc(count + nr_lf))) - { - for (s = buf, i = 0, j = 0; i < count; i++) - { - if (s[i]== '\n') - { - p[j++] = '\r'; - /*if ((i >1) && (s[i-1] == '\r'))j--;*/ - } - p[j++] = s[i]; - } - } - else - { - FIXME("Malloc failed\n"); - nr_lf =0; - q = buf; - } - } - else - q = buf; + else + { + unsigned int i, j, nr_lf, size; + char *p = NULL; + const char *q; + const char *s = buf, *buf_start = buf; - if ((WriteFile(hand, q, count+nr_lf, &num_written, NULL) == 0 ) || (num_written != count+nr_lf)) - { - TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n", - fd, hand, GetLastError(), num_written); - *_errno() = ENOSPC; - if(nr_lf) - free(p); - return s - buf_start; - } - else - { - if(nr_lf) - free(p); - return count; - } - } - return -1; + if (!(info->exflag & (EF_UTF8|EF_UTF16))) + { + /* find number of \n */ + for (nr_lf=0, i=0; iexflag & EF_UTF16) + { + for (nr_lf=0, i=0; i